Stacking example problem. What am I doing wrong?

print(torch.__version__) # 1.0.1.post2
#stacking example
a = torch.randn(2,3,4)
b = torch.randn(2,3)
print(a.size()) # 2, 3, 4
print(b.size()) # 2, 3
b = torch.unsqueeze(b, dim=2)  
print(b.size()) # 2, 3, 1
r = torch.stack([a, b], dim=2 )

Outputs the error like this:
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 2. Got 4 and 1 in dimension 3 at /pytorch/aten/src/TH/generic/THTensorMoreMath.cpp:1307

I was trying to achieve r.size() = 2, 3, 5
according to this.


You could use,b), dim=2) to achieve it.

Yes, cat works fine MariosOreo. Have I found a bug using the stack method as per above?

I am thinking if things like this happens maybe I should better use np.stack() to prepare the data instead torch.stack(). I guess it is still early days…

Any feedback?