Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 407
Bill Psomas Avatar asked Mar 22 '21 16:03

Bill Psomas


2 Answers

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.

like image 178
Bill Psomas Avatar answered Oct 10 '22 03:10

Bill Psomas


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

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

like image 45
starriet Avatar answered Oct 10 '22 02:10

starriet