view()
vsreshape()
,transpose()
vspermute()
.view()
and reshape()
can be used to change the size or shape oftensors. But they are slightly different.view()
has existed for a long time. It will return a tensor with the newshape. The returned tensor shares the underling data with the original tensor.If you change the tensor value in the returned tensor, the corresponding valuein the viewed tensor also changes.reshape()
has been introduced in version0.4. According to thedocument, thismethod willtorch.reshape
may return a copy or a view of the originaltensor. You can not count on that to return a view or a copy. According to thedeveloper:id
of original tensor and viewing tensor:id
of a.storage()
and b.storage()
is not the same. Isn’tthat their underlying data the same? Why this difference?data_ptr()
method. You will find that theirdata pointers are the same.transpose()
, like view()
can also be used to change the shape of a tensorand it also returns a new tensor sharing the data with the original tensor:view()
can only operate on contiguous tensor and thereturned tensor is still contiguous. transpose()
can operate both oncontiguous and non-contiguous tensor. Unlike view()
, the returned tensor maybe not contiguous any more.contiguous
in Numpy. It also applies toPyTorch.contiguous
in PyTorch means if the neighboring elements inthe tensor are actually next to each other in memory. Let’s take a simpleexample:x
and y
in the above example share the same memory space1.is_contiguous()
,you will find that x
is contiguous but y
is not.contiguous()
,which will return a new contiguous tensor. In plain words, it will create a newmemory space for the new tensor and copy the value from the non-contiguoustensor to the new tensor.permute()
and tranpose()
are similar. transpose()
can only swap twodimension. But permute()
can swap all the dimensions. For example:permute()
, you must provide the new order of all thedimensions. In transpose()
, you can only provide two dimensions. tranpose()
can be thought as a special case of permute()
method in for 2D tensors.tensor.data_ptr()
. ↩︎