1a.size() # 2, 3, 4
2b.size() # 2, 3
3b = torch.unsqueeze(b, dim=2) # 2, 3, 1
4# torch.unsqueeze(b, dim=-1) does the same thing
5
6torch.stack([a, b], dim=2) # 2, 3, 5
7
1>>> import torch
2>>> a = torch.randn([2, 3, 4])
3>>> b = torch.randn([2, 3])
4>>> b = b.unsqueeze(dim=2)
5>>> b.shape
6torch.Size([2, 3, 1])
7>>> torch.cat([a, b], dim=2).shape
8torch.Size([2, 3, 5])
9
1>>> a = torch.randn([2, 3, 4])
2>>> b = torch.randn([2, 3, 4])
3>>> torch.stack([a, b]).shape
4torch.Size([2, 2, 3, 4])
5