In numpy, we use ndarray.reshape()
for reshaping an array.
I noticed that in PyTorch, people use torch.view()
for the same purpose, but at the same time, there is also a torch.reshape()
existing.
So I am wondering what the differences are between them and when I should use either of them?
torch.view
has existed for a long time. It will return a tensor with the new shape. The returned tensor will share the underling data with the original tensor.
See the documentation here.
On the other hand, it seems that torch.reshape
has been introduced recently in version 0.4. According to the document, this method will
Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.
It means that torch.reshape
may return a copy or a view of the original tensor. You can not count on that to return a view or a copy. According to the developer:
if you need a copy use clone() if you need the same storage use view(). The semantics of reshape() are that it may or may not share the storage and you don't know beforehand.
Another difference is that reshape()
can operate on both contiguous and non-contiguous tensor while view()
can only operate on contiguous tensor. Also see here about the meaning of contiguous
.