2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.