I would like to concatenate tensors, not along a dimension, but by creating a new dimension.
For example:
x = torch.randn(2, 3)
x.shape # (2, 3)
torch.cat([x,x,x,x], 0).shape # (8, 3)
# This concats along dim 0, not what I want
torch.cat([x,x,x,x], -1).shape # (2, 10)
# This concats along dim 1, not what I want
torch.cat([x[None, :, :],x[None, :, :],x[None, :, :],x[None, :, :]], 0).shape
# => (4, 2, 3)
# This is what I want, but unwieldy
Is there a simpler way?
Just use torch.stack:
torch.stack([x,x,x,x]).shape # (4, 2, 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With