'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?



Solution 1:[1]

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.

Solution 2:[2]

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill Psomas
Solution 2 starriet