如何使用 torch.stack
堆叠两个形状为 a.shape = (2, 3, 4)
和 b.shape = ( 2, 3)
没有就地操作?
最佳答案
堆叠需要相同数量的维度。一种方法是取消挤压和堆叠。例如:
a.size() # 2, 3, 4
b.size() # 2, 3
b = torch.unsqueeze(b, dim=2) # 2, 3, 1
# torch.unsqueeze(b, dim=-1) does the same thing
torch.stack([a, b], dim=2) # 2, 3, 5
关于python - 我如何使用 torch.stack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288635/