torch.stack()用法

例子
准备2个tensor数据,每个的shape都是[3,3]

# 假设是时间步T1的输出
T1 = torch.tensor([[1, 2, 3],
        		[4, 5, 6],
        		[7, 8, 9]])
# 假设是时间步T2的输出
T2 = torch.tensor([[10, 20, 30],
        		[40, 50, 60],
        		[70, 80, 90]])

不同维度进行stack操作,结果如下:

print(torch.stack((T1,T2),dim=0).shape)
print(torch.stack((T1,T2),dim=1).shape)
print(torch.stack((T1,T2),dim=2).shape)
print(torch.stack((T1,T2),dim=3).shape)
# outputs:
torch.Size([2, 3, 3])
torch.Size([3, 2, 3])
torch.Size([3, 3, 2])

‘选择的dim>len(outputs),所以报错’
IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

详见:https://blog.csdn.net/xinjieyuan/article/details/105205326

猜你喜欢

转载自blog.csdn.net/qq_35037684/article/details/120656311