Bill Psomas
Bill Psomas

Reputation: 491

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces)

I am using Pytorch. I got this RuntimeError while evaluating a model. Any idea how to solve this?

Upvotes: 21

Views: 38811

Answers (2)

starriet 차주녕
starriet 차주녕

Reputation: 3968

SOLUTION2: change the .view(...) to .contiguous().view(...).

Actually, 'reshape()' does a similar job as contiguous() under the hood, if it needs.

Upvotes: 18

Bill Psomas
Bill Psomas

Reputation: 491

SOLUTION: Just replace the view() function with reshape() function as suggested in the error and it works.

I guess this has to do with how the tensor is stored in memory.

Upvotes: 27

Related Questions