How to concatenate to a Tensor with a 0 dimension?

In Numpy I can do:

np.hstack((np.zeros((3, 0)), np.zeros((3, 3)))

and it would give me a 3x3 zero matrix.
But in pytorch,

torch.cat((torch.zeros(3, 0), torch.zeros(3, 3)), dim=1)

gives me a run time error RuntimeError: dim out of range - got 1 but the tensor is only 1D. Because torch.zeros(3, 0) is actually a 3-element Tensor (as opposed to Numpy, where it is empty).

Now, @smth has said before that there are no 0 dimensional Tensors in pytorch (For-loop with a 2D matrix of size 0) but does anyone know of a solution to this problem, where for example the 0 size of the dim is being calculated in a loop?

For example if I have

for i in xrange(5):
  print torch.cat((torch.zeros(3, i), torch.zeros(3, 3)), dim=1)

the first iteration would give an error…

2 Likes

Hi, have you found the solution?

Yes - apparently now (in version 0.3.0) you can create 0-dimensional tensors. For example,
torch.zeros(0, 0) will give [torch.FloatTensor with no dimension].

So now you can do torch.cat((torch.zeros(0, 0), torch.zeros(3, 3))).

3 Likes

You can also do torch.Tensor().

2 Likes

I do something like this to concatenate zero dimensional tensor to another tensor

A = torch.tensor([0]) # [0]
B = torch.tensor([1, 2, 3, 4]) # [1, 2, 3, 4]
C = torch.cat((A.view(1), B)) # [0, 1, 2, 3, 4]
1 Like

A = torch.tensor(0)
B = torch.tensor([1,2,3])
C = torch.cat((A.reshape(1), B))

this works best for me. thanks!

doesn’ work on gpu though… what would you suggest to use the tensor on the gpu?

This doesn’t work anymore in the latest pytorch :frowning: any alternative? thanks

If you do torch.empty(10,0), then you can concatenate it to torch.rand(10,24)

Thanks I’ll try that