Reputation: 5
I am trying to insert tensor y into the tensors x final dimension (y_dim). The final tensor should be of size (100, 16, 16, 1) where the values of y are placed in each of 100 x 0dimension
import torch
y_dim = 1
x = torch.randn(100, 16, 16, y_dim)
#OR x = torch.randn(100, 16, 16)
y = torch.randn(100)
Xy = torch.cat((x, y), dim=3)
Upvotes: 0
Views: 749
Reputation: 40618
I think you are missing something in your understanding of tensors and dimensions. The easiest thing is to consider your tensor x
as a batch containing 100
maps of width and height 16
, i.e. 100
16x16
-maps. So you are manipulating a tensor containing 100*16*16
elements. Your y
, on the other hand, contains 100
scalar values, it has 100
elements.
I'm turning the question back to you:
How would you concatenate
100
16x16
-maps with100
scalar values?
The above question has no answer. There are certain things that can be done though, assumptions that can be made on y
in order to perform a concatenation:
If you had a tensor y
containing 16x16
maps as well, then yes this operation would be achievable:
>>> x = torch.rand(100, 16, 16)
>>> y = torch.rand(100, 16, 16)
>>> torch.cat((x, y)).shape
torch.Size([200, 16, 16])
If you consider the y
in your question, you could expand the 100
scalar values to 16x16
maps. And, then concatenate with x
:
>>> x = torch.rand(100, 16, 16)
>>> y = torch.rand(100)
>>> y_r = y[:, None, None].repeat(1, 16, 16)
>>> torch.cat((x, y_r))
torch.Size([200, 16, 16])
Upvotes: 1