Pytorch tensor 矩阵转置方法
直接贴官方文档啦
多维矩阵转置:
torch.transpose(input, dim0, dim1) → Tensor
- input (Tensor) – the input tensor.
- dim0 (int) – the first dimension to be transposed
- dim1 (int) – the second dimension to be transposed
例子:
x = torch.randn(2, 3)
x
'''
tensor([[ 1.0028, -0.9893, 0.5809],
[-0.1669, 0.7299, 0.4942]])
'''
torch.transpose(x, 0, 1)
'''
tensor([[ 1.0028, -0.1669],
[-0.9893, 0.7299],
[ 0.5809, 0.4942]])
'''
二维及以下矩阵转置:
torch.t(input) → Tensor
- input (Tensor) – the input tensor.
例子:
x = torch.randn(())
x
'''
tensor(0.1995)
'''
torch.t(x)
x = torch.randn(3)
x
'''
tensor(0.1995)
'''
torch.t(x)
'''
tensor([ 2.4320, -0.4608, 0.7702])
'''
x = torch.randn(2, 3)
x
'''
tensor([[ 0.4875, 0.9158, -0.5872],
[ 0.3938, -0.6929, 0.6932]])
'''
torch.t(x)
'''
tensor([[ 0.4875, 0.3938],
[ 0.9158, -0.6929],
[-0.5872, 0.6932]])
'''
1) https://pytorch.org/docs/stable/generated/torch.transpose.html
2) https://pytorch.org/docs/stable/generated/torch.t.html#torch.t