For beginners: Do not use view() or reshape() to swap dimensions of tensors!

Thanks for the answer! This seems really useful.

I am curious, what would be uses cases for .view then? (perhaps making a list would be useful)

The only places I’ve ever seen it used without it causing issues is when flattening (as you mentioned) and when doing a squeeze (despite a squeeze function already existing). e.g.

x = torch.randn(3, 4, 5, 6)
x.view(batch_size, -1)  # flat!

or (saw it on the imagenet code for compute topk accuracy I believe)

x.view(1,-1)

It just seems that .view is weird since it only arranges the tensor to the right view but starts from the beginning and enumerates “in order” (ref: Tensor.view is misleading - #2 by rasbt).

1 Like