permute将tensor的维度进行转换。 举例: import torch import numpy as np a=np.array([[[1,2,3],[4,5,6]]]) b=torch.tensor(a) print(b) print(b.size()) # ——> torch.Size([1, 2, 3]) c=b.permute(2,0,1) print(c.size()) # ——> torch.Size([3, 1, 2]) print(c) c = b.view(3,1,2) print(c)
输出:
tensor([[[1, 2, 3],
[4, 5, 6]]], dtype=torch.int32)
torch.Size([1, 2, 3])
torch.Size([3, 1, 2])
tensor([[[1, 4]],
[[2, 5]],
[[3, 6]]], dtype=torch.int32)
tensor([[[1, 2]],
[[3, 4]],
[[5, 6]]], dtype=torch.int32)