其他分享
首页 > 其他分享> > pytorch中的transpose()函数

pytorch中的transpose()函数

作者:互联网

torch.transpose(Tensor,dim0,dim1)是pytorch中的ndarray矩阵进行转置的操作

例如:x = ([[0,1,2],[3,4,5],[6,7,8]])

 

 

 

 

我们先把它转为矩阵

import torch
import numpy as ny x = ([[0,1,2],[3,4,5],[6,7,8]]) x = ny.matrix(x) print (x) ''' [[0 1 2] [3 4 5] [6 7 8]] ''' #默认转置 y1 = x.transpose() print (y1) ''' [[0 3 6] [1 4 7] [2 5 8]] ''' #正常顺序显示 y2 = x.transpose(0,1) print (y2) '''
[[0 1 2]
[3 4 5]
[6 7 8]]
'''

#转置
y3 = x.transpose(1,0)
print (y3)
''' 
[[0 3 6]
[1 4 7]
[2 5 8]]
'''

如果您对转置不要理解

那你可以这样去理解他的操作

原矩阵
0 1 x [0 , 0] = 0 x [0 , 1] = 1 x [0 , 2] = 2 x [1 , 0] = 3 x [1 , 1] = 4 x [1 , 2] = 5 x [2 , 0] = 6 x [2 , 1] = 7 x [2 , 2] = 8
变换后 1 0 x [0 , 0] = 0 x [1 , 0] = 3 x [2 , 0] = 6 x [0 , 1] = 1 x [1 , 1] = 4 x [2 , 1] = 7 x [0 , 2] = 2 x [1 , 2] = 5 x [2 , 2] = 8

 

标签:函数,转置,transpose,矩阵,y1,pytorch,print,y3
来源: https://www.cnblogs.com/sakela/p/16481375.html