1

I have a pytorch tensor that has shape [n1, n2, n3]. I need to make the shape [n1, n2, n3, 1].

So I know I can use either unsqueeze or view. Is there a difference in what each one would do in this case?

24n8
  • 1,898
  • 1
  • 12
  • 25
  • Well the main difference is that view is closer to reshape the entire array, while unsqueeze is just a appending an extra dimension onto the array. But at the end you can use both to achieve the same effect – Edwin Cheong May 10 '22 at 03:08
  • Not sure about the performance difference though but usually for simplicity best to use unsqueeze if ur just adding an extra dimension – Edwin Cheong May 10 '22 at 03:08

1 Answers1

1

You can achieve this with four different solutions. There are slight differences between those. More precisely you can:

  • insert a new singleton dimension with torch.Tensor.unqueeze:

    >>> x.unsqueeze(-1) # grad_fn=<UnsqueezeBackward0>
    
  • use fancy indexing to add a new dimension which is identical:

    >>> x[..., None] # grad_fn=<UnsqueezeBackward0>
    
  • or similarly with torch.Tensor.view:

    >>> x.view(*x.shape, 1) # grad_fn=<ViewBackward0>
    
  • add a new dimension with torch.Tensor.reshape:

    >>> x.reshape(*x.shape, 1) # grad_fn=<ReshapeAliasBackward0>
    

I have added the backward gradient function name as a line comment next to each method. You can see how indexing and unsqueezing are the same, while view and reshape rely on two different methods.

All three methods: indexing, unsqueeze, and view will return a view of the tensor while reshape can return a copy of the tensor if needed (i.e. when the data is not contiguous).

You can read more about the differences between torch.view and torch.reshape on this thread.

Ivan
  • 34,531
  • 8
  • 55
  • 100