5

Are view() in torch and reshape() in Numpy similar?

view() is applied on torch tensors to change their shape and reshape() is a numpy function to change shape of ndarrays.

n1k31t4
  • 14,663
  • 2
  • 28
  • 49
Kailash Ahirwar
  • 151
  • 1
  • 5

1 Answers1

3

Yes, for most intents and purposes, they can do the same job. From this link, an example:

>>> import torch
>>> t = torch.ones((2, 3, 4))
>>> t.size()
torch.Size([2, 3, 4])
>>> t.view(-1, 12).size()
torch.Size([2, 12])

If you are concerned with memory allocation, here is another answer on StackOverflow with a little more information. PyTorch's view function actually does what the name suggests - returns a view to the data. The data is not altered in memory as far as I can see.

In numpy, the reshape function does not guarantee that a copy of the data is made or not. It will depend on the original shape of the array and the target shape. Have a look here for further information.

n1k31t4
  • 14,663
  • 2
  • 28
  • 49