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

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

The causing code is

This error didn't happen before, so it's introduced by new version of pytorch (now my current used pytorch is 1.8.1).

And printing the array, I found it's a boolean array.

Solution

Add .contiguous() before view() or use reshape to replace view

So change the line

to

or