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.
Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.Sign up to join this community
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.