把维度进行压缩A*1*B就会变成A*B也可以使用torch.squeeze去压缩指定维度,如果压缩的指定维度不为1则返回原数组。
import torch import numpy as np x=np.arange(24).reshape((2,1,3,4)) x = torch.tensor(x) print(x) print(x.squeeze())tensor([[[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]],
[[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]], dtype=torch.int32)
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]], dtype=torch.int32)