How to stack 1-dimensional vectors in pytorch

Viewed 678

I am trying to stack 1-dimensional tensors in pytorch but the stack function seems to be interpreting them as 2-d square matrices. Any ideas how to stack 1-d tensors into a new 1-d tensor?

Reproducibility:

a = torch.randn([2])
b = torch.randn([3])
c = torch.stack([a, b]) # want a (5,) tensor

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 2 and 3 in dimension 1 at c:\new-builder_3\win-wheel\pytorch\aten\src\th\generic/THTensorMath.cpp:3616

If I unsqueeze a and b to (2,1) and (3,1) tensors or squeeze them (should have no effect) the error is still present. This seems odd too though because they should both be size 1 in dimension 1 when unsqueezed (and printing their sizes will accurately reflect this), but the error still appears the same, character-for-character.

Additionally, stacking in dimension 1 just makes it say "...2 and 3 in dimension 0...".

This is all in python 3.5.4, pytorch 0.4.1

1 Answers

You can try cat (official docs)

a = torch.randn([2])
b = torch.randn([3])
c = torch.cat([a, b], dim=0)
Related