Overblog Tous les blogs Top blogs Entreprenariat Tous les blogs Entreprenariat
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU
http://ynoih.over-blog.com/

ynoih.over-blog.com/

Publicité

Permute 2 1



  1. Rearrange the dimensions of a multidimensional array. B = permute(A,order) Description. B = permute(A,order) rearranges the dimensions of A so that they are in the order specified by the vector order. B has the same values of A but the order of the subscripts needed to access any particular element is rearranged as specified.
  2. Permute(dims)将tensor的维度换位。参数:参数是一系列的整数,代表原来张量的维度。比如三维就有0,1,2这些dimension。例:import torchimport numpy as npa=np.array(1,2,3,4,5,6)unpermuted=torch.tensor(a)print(unpermuted.siz.
  3. Well, version 2.X for the most part was great. Transcoded fast, and more often than not well. I pretty much only go from.TS files to.MP4 and I've done 5-6 with the new version 3 - first 3.0.1 all they way up to 3.0.4 and all of them resulted in nothing but an hour and a half black screen with no sound.
  4. If n is the number of dimensions in x, x.T is equivalent to x.permute(n-1, n-2., 0). Real¶ Returns a new tensor containing real values of the self tensor. The returned tensor and self share the same underlying storage.

For example, the array 3, 2, 1, 0 represents the permutation that maps the element at index 0 to index 3, the element at index 1 to index 2, the element at index 2 to index 1 and the element at index 3 to index 0.

PyTorch provides a lot of methods for the Tensor type. Some of these methodsmay be confusing for new users. Here, I would like to talk aboutview() vsreshape(),transpose() vspermute().

view() vs transpose()

Both view() and reshape() can be used to change the size or shape oftensors. But they are slightly different.

The 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.

On the other hand, it seems that reshape()has been introduced in version0.4. According to thedocument, thismethod will

Permute 1

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 originaltensor. You can not count on that to return a view or a copy. According to thedeveloper:

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.

Permute 2 1

As a side note, I found that torch version 0.4.1 and 1.0.1 behaves differentlywhen you print the id of original tensor and viewing tensor:

You see that id of a.storage() and b.storage() is not the same. Isn'tthat their underlying data the same? Why this difference?

Permute

I filed an issue in thePyTorch repo and got answers from the developer. It turns out that to find thedata pointer, we have to use the data_ptr() method. You will find that theirdata pointers are the same.

view() vs transpose()

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:

Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.

Permute 2 1/2

The resulting out tensor shares it's underlying storage with the input tensor, so changing the content of one would change the content of the other.

One difference is that 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.

But what does contiguous mean?

There is a good answer on SOwhich discusses the meaning of contiguous in Numpy. It also applies toPyTorch.

As I understand, contiguous in PyTorch means if the neighboring elements inthe tensor are actually next to each other in memory. Let's take a simpleexample:

Tensor x and y in the above example share the same memory space1.

Permute(0 2 3 1)

If you check their contiguity withis_contiguous(),you will find that x is contiguous but y is not.

Creative edge software ic3d suite 5 5 6. Adobe acrobat reader premium free apk. Since x is contiguous, x[0][0] and x[0][1] are next to each other in memory.But y[0][0] and y[0][1] is not.

A lot of tensor operations requires that the tensor should be contiguous,otherwise, an error will be thrown. To make a non-contiguous tensor becomecontiguous, use call thecontiguous(),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:

Note that, in permute(), you must provide the new order of all thedimensions. How to format an external drive for mac and pc. 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 pointers.
  • view after transpose raises non-contiguous error.
  • When to use which, permute, view, transpose.
  • Difference between reshape() and view().
  1. To show a tensor's memory address, use tensor.data_ptr(). ↩︎





Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article