a = torch.randn(2,3,4)
print(a)
>>>
tensor([[[-1.8377, 0.7000, 0.4378, -0.0805],
[ 0.4081, 0.9317, -0.1416, 0.3171],
[-0.3136, -0.2353, 0.6491, 1.9069]],
[[-0.7775, -0.7211, 1.5763, -1.0702],
[-0.3504, 0.8695, -0.5245, -0.9854],
[-0.8162, 0.4495, -0.8055, -0.6394]]])
a.transpose(1,0)
>>>
tensor([[[-1.8377, 0.7000, 0.4378, -0.0805],
[-0.7775, -0.7211, 1.5763, -1.0702]],
[[ 0.4081, 0.9317, -0.1416, 0.3171],
[-0.3504, 0.8695, -0.5245, -0.9854]],
[[-0.3136, -0.2353, 0.6491, 1.9069],
[-0.8162, 0.4495, -0.8055, -0.6394]]])
transpose函数的作用在于改变矩阵的维度,里边两个参数是指定要去交换的维度。a本来是(2,3,4)的维度,交换了第1和第2维度的值之后,就成了(3,2,4)的形状。