How to concatenate a 0-D tensor into a 3-D tensor in PyTorch?
I have the following tensor:
X = torch.randn(30,1,2)  # [batch_size, dim_1, dim_2]

t = torch.Tensor([0]) 

I am trying to concatenate the t tensor into X tensor that results [30,1,3] tensor. However, I tried couple of methods even with torch.stack. I still have not figured out how to do this properly. I tried both and they gave errors.
result = torch.cat((X,t), dim = -1) # first try
result = torch.stack([X,t], dim = -1) # second try. 

Is there a way I can concatenate these tensors?

You can't concatenate the two described tensors, the shape of tensor X is [30, 1 , 2], which means it has 30 positions in the first dimension, 1 position in the second dimension, and 2 positions in the last dimension, totalling 30*1*2 = 60 elements. A tensor of shape [30,1,3] has 90 elements, meaning you need to add 30 elements to get the desired result.
You can do this by changing the code to:
&gt;&gt;&gt; X = torch.randn(30,1,2)
&gt;&gt;&gt; t = torch.zeros(30,1,1)
&gt;&gt;&gt; r = torch.cat((X,t), dim=-1)
&gt;&gt;&gt; r.shape
torch.Size([30, 1, 3])

