Analysis of numpy.transpose and torch.transpose functions

question

Today, I encountered some problems when trying to convert a three- [3, x, y]dimensional to , and the final solution is to convert to , and then use the solution. So analyze the transpose function in torch and numpy.tensor[x, y, 3]tensornumpynumpy.transpose(mytensor, [1, 2, 0])

reference

numpy.transpose

Usage is as follows:
numpy.transpose(a, axes=None)

  • If a two-dimensional matrix, the transpose of the matrix is ​​returned.
a = np.random.randint(0, 10, (3, 2))
print(a, a.shape)
a = np.transpose(a)
print(a, a.shape)
a = np.transpose(a, (1, 0))
print(a, a.shape)

The result is:

[[5 7]
 [2 3]
 [9 1]]
 (3, 2)

[[5 2 9]
 [7 3 1]] 
(2, 3)

[[5 7]
 [2 3]
 [9 1]]
(3, 2)

It can be seen that the two-dimensional matrix is ​​transposed every time .

  • If it is a multidimensional matrix:
a = np.random.randint(0, 10, (3, 2, 4))
print(a, a.shape)
a = np.transpose(a, (1, 2, 0))
print(a, a.shape)

result:

[[[4 3 6 8]
  [7 0 1 1]]

 [[0 2 6 4]
  [4 0 6 2]]

 [[3 3 4 6]
  [5 6 6 2]]] 
(3, 2, 4)


[[[4 0 3]
  [3 2 3]
  [6 6 4]
  [8 4 6]]

 [[7 4 5]
  [0 0 6]
  [1 6 6]
  [1 2 2]]]
(2, 4, 3)

It can be seen that the original shape of the matrix is pre = [3, 2, 4]​​, the parameters passed in during the transformation are (1, 2, 0), and then the matrix becomes [2, 4, 3]that is [pre[1], pre[2], pre[0]].
But the specific transformation process is not yet understood.

  • For the parameters axes, the default is: range(a.ndim)[::-1], which is shapethe reverse order of the original matrix.
  • You can also use it like this:
a = np.random.randint(0, 10, (3, 2, 4))
print(a, a.shape)
b = a.transpose([1, 2, 0])
print(b, b.shape)

result:

[[[5 0 8 0]
  [5 4 5 8]]

 [[6 8 8 8]
  [8 0 4 5]]

 [[2 6 8 3]
  [0 8 3 4]]] 
(3, 2, 4)

[[[5 6 2]
  [0 8 6]
  [8 8 8]
  [0 8 3]]

 [[5 8 0]
  [4 0 8]
  [5 4 3]
  [8 5 4]]]
(2, 4, 3)

The effect is the same .

does not create a new object

a = np.random.randint(0, 10, (2, 4))
print(a)
b = a.transpose()
print(b)

result:

[[6 5 4 8]
 [3 2 1 2]]

[[6 3]
 [5 2]
 [4 1]
 [8 2]]
b[0][0] = 15
print(a)
print(b)

result:

[[15  5  4  8]
 [ 3  2  1  2]]
[[15  3]
 [ 5  2]
 [ 4  1]
 [ 8  2]]

As one changes, the other changes too .

torch.transpose

Usage is as follows:
torch.transpose(input, dim0, dim1)
Return a tensor, yes inputtranspose. And also sharing an actual tensor, changing one also changes the other.

import torch
a = torch.randint(0, 10, (2, 4))
print(a)
b = torch.transpose(a, 1, 0)
print(b)
c = torch.transpose(a, 0, 1)
print(c)

result:

tensor([[2, 7, 0, 9],
        [8, 2, 8, 7]])
tensor([[2, 8],
        [7, 2],
        [0, 8],
        [9, 7]])
tensor([[2, 8],
        [7, 2],
        [0, 8],
        [9, 7]])

It can be seen that the transposition of the matrix is ​​being performed.
In this function, dim0the sum dim1will be interchanged (transposed), so the effect is consistent, transpose(a, 1, 0)and dim[0] and dim[1] are interchanged . This is different from numpy's functions.transpose(a, 0, 1)

a = torch.randint(0, 10, (2, 3, 4))
print(a)
b = torch.transpose(a, 1, 2)
print(b)

result:

tensor([[[2, 0, 6, 7],
         [8, 8, 0, 2],
         [6, 7, 6, 6]],

        [[9, 1, 6, 4],
         [8, 3, 2, 8],
         [0, 0, 4, 9]]])
tensor([[[2, 8, 6],
         [0, 8, 7],
         [6, 0, 6],
         [7, 2, 6]],

        [[9, 8, 0],
         [1, 3, 0],
         [6, 2, 4],
         [4, 8, 9]]])

However, the conversion formula for a certain two dimensions or certain dimensions of a multidimensional matrix is ​​not very clear.

Guess you like

Origin blog.csdn.net/qq_43219379/article/details/123388746