Reputation: 515
I have two tensors in pytorch with these shapes:
torch.Size([64, 100]) and torch.Size([64, 100, 256])
I want to concate them by torch.cat
but they should be in the same shape and size. So I get this error:
RuntimeError: Tensors must have same number of dimensions: got 2 and 3
what should I do to fix this problem? how can I convert 2d PyTorch tensor into 3d tensor OR how can I convert 3d PyTorch tensor to 2d tensor without losing any data? or any other idea?
Upvotes: 0
Views: 2197
Reputation: 40618
Depending on what you are looking to do with those two tensors, you could consider concatenating on the last axis such that the resulting tensor is shaped (64, 100, 257)
. This requires you first unsqueeze a singleton dimensions on the first tensor:
>>> x, y = torch.rand(64, 100), torch.rand(64, 100, 256)
>>> z = torch.cat((x[..., None], y), -1)
>>> z.shape
torch.Size([64, 100, 257])
Upvotes: 1