简体   繁体   中英

how to concate two tensors with different dimensions in pytorch

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?

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM