作者: | 来源:互联网 | 2023-08-26 08:48
torch.cat官方文档解释:Concatenatesthegivensequenceofseqtensorsinthegivendimension.torch.s
torch.cat 官方文档解释:Concatenates the given sequence of seq tensors in the given dimension .torch.stack 官方文档解释:Concatenates sequence of tensors along a new dimension . 例子如下: 如果 A ,B 的 shape 都是 [3,4], 那么 C = torch.cat ([A, B], dim = 0) 得到的 C shape 是 [6,4 ] C = torch.stack ([A,B], dim = 0) 得到的 C shape 是 [2,3,4 ]
附:本人关于 Pytorch 一些 methods 关于 dim 操作理解的思考。我们为了明白 这些 methods,可以对methods操作前后,被操作对象的 shape 变化来得到启发。 在本文中,tensor shape 如果用 torch.cat: [3, 4] -> [6,4],dim 0 由 3 -> 6 如果用 torch.stack: [3, 4]->[2,3,4], dim 0 是 新 insert 进来的。 对于这两者 shape 上的 改变,不难看出都是对原 tensor dim = 0进行了改变。