What is the difference between view() and unsqueeze()?

What is view()?

view() takes a tensor and reshapes it. A requirement being that the product of the lengths of each dimension in the new shape equals that of the original. Hence a tensor with shape (4,3) can be reshaped with view to one of shape:

  • (1,12), (2,6), (3,4), (6,2), (12,1)

but also, any number of superficial dimensions of length 1 can be removed (i.e. view(12)), or added (e.g. (2,6,1), (3,1,1,4), (1,4,1,3,1) etc).

squeeze and unsqueeze are convenient synonyms for these latter two special cases where that is the only change in shape.

Likewise flatten is a convenient synonym for view(-1).

When to use unsqueeze()?

Use view (or reshape) when you want to generically reshape a tensor.

If you want to specifically add a superficial dimension (e.g. for treating a single element like a batch, or to concatenate with another tensor), unsqueeze is a more convenient (and explicit) synonym, but the underlying operation is the same.


Note: view(1, -1, -1, -1) will not work (-1 can only be used once to infer one dimension’s size). What you want to do can be achieved with view(1, *input.shape).

4 Likes