RuntimeError: The expanded size of the tensor must match the existing size

I have a tensor with shape [2,3] and I want to expand it to [4,3].
If I use torch.expand directly, a error occurs.

>>>x
tensor([[4., 5., 6.],
        [1., 2., 3.]])
>>>x.size()
torch.Size([2, 3])
>>>x.expand(4,3)
RuntimeError: The expanded size of the tensor (4) must match the existing size (2) at non-singleton dimension 0.  Target sizes: [4, 3].  Tensor sizes: [2, 3]

However, if I add a new axis before expanding it, it works.

>>>x[None,...].expand(2,-1,-1).reshape(4,3)
tensor([[4., 5., 6.],
        [1., 2., 3.],
        [4., 5., 6.],
        [1., 2., 3.]])

Does it mean we can only expand the axis with size 1?

Yes, from the docs:

Returns a new view of the self tensor with singleton dimensions expanded to a larger size.
[…]
Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.

You could use x.repeat(2, 1) for your use case. Note that this operation would allocate new memory for the tensor.

2 Likes

Note that this operation would allocate new memory for the tensor

Any way to work around it?

No, you cannot expand a dimension with a different size than 1 and would need to create a new tensor.

2 Likes