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?