示例文件 test.py
import torch
cor1 = torch.arange(5).float()
cor2 = torch.arange(4).float()
print(cor1)
print(cor2)
X, Y = torch.meshgrid(cor1, cor2)
print(X)
print(X.permute(0, 1))
print(X.permute(1, 0))
print(X.transpose(0, 1))
print(X.transpose(1, 0))
终端命令行及运行结果
<user>python test.py
tensor([0., 1., 2., 3., 4.])
tensor([0., 1., 2., 3.])
tensor([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.]])
tensor([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.]])
tensor([[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.]])
tensor([[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.]])
tensor([[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.],
[0., 1., 2., 3., 4.]])
有运行结果来看:
tensor.permute()是指定式的参数,参数顺序决定转置后的张量,可以设置多维。
transpose()是交换式的参数,参数顺序不决定转置后的张量,只能设置二维。
本文通过示例代码详细解析了PyTorch中张量的permute()和transpose()方法的区别与用法,permute()允许指定多维张量的维度顺序,而transpose()仅用于交换两个维度。
1519

被折叠的 条评论
为什么被折叠?



