What is the difference between view() and unsqueeze() in Torch? What is the difference between view() and unsqueeze() in Torch? python python

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


view() can only take a single -1 argument.

So, if you want to add a singleton dimension, you would need to provide all the dimensions as arguments. For e.g., if A is a 2x3x4 tensor, to add a singleton dimension, you would need to do A:view(2, 1, 3, 4).

However, sometimes, the dimensionality of the input is unknown when the operation is being used. Thus, we dont know that A is 2x3x4, but we would still like to insert a singleton dimension. This happens a lot when using minibatches of tensors, where the last dimension is usually unknown. In these cases, the nn.Unsqueeze is useful and lets us insert the dimension without explicitly being aware of the other dimensions when writing the code.


unsqueeze() is a special case of view()

For convenience, many python libraries have short-hand aliases for common uses of more general functions.

  • view() reshapes a tensor to the specified shape
  • unsqueeze() reshapes a tensor by adding a new dimension of depth 1
    (i.e. turning an n.d tensor into an n+1.d tensor)

When to use unsqueeze()?

Some example use cases:

  • You have a model designed to intake RGB image tensors (3d: CxHxW), but your data is 2d greyscale images (HxW)
  • Your model is designed to intake batches of data (batch_size x dim1 x dim2 x ...), and you want to feed it a single sample (i.e. a batch of size 1).