Jordan
Jordan

Reputation: 1505

How do I change a torch tensor to concat with another tensor

I'm trying to concatenate a tensor of numerical data with the output tensor of a resnet-50 model. The output of that model is tensor shape torch.Size([10,1000]) and the numerical data is tensor shape torch.Size([10, 110528,8]) where the 10 is the batch size, 110528 is the number of observations in a data frame sense, and 8 is the number of columns (in a dataframe sense). I need to reshape the numerical tensor to torch.Size([10,8]) so it will concatenate properly.

How would I reshape the tensor?

Upvotes: 0

Views: 874

Answers (1)

conv3d
conv3d

Reputation: 2906

Starting tensors.

a = torch.randn(10, 1000)
b = torch.randn(10, 110528, 8)

New tensor to allow concatenate.

c = torch.zeros(10,1000,7)

Check shapes.

a[:,:,None].shape, c.shape
(torch.Size([10, 1000, 1]), torch.Size([10, 1000, 7]))

Alter tensor a to allow concatenate.

a = torch.cat([a[:,:,None],c], dim=2)

Concatenate in dimension 1.

torch.cat([a,b], dim=1).shape
torch.Size([10, 111528, 8])

Upvotes: 1

Related Questions