a = tensor([ [101, 103],
[101, 1045]
])
b = tensor([ [101, 777, 227],
[101, 888, 228]
])
How to I get this tensor c from a and b:
c = a b = tensor([ [101, 103, 0],
[101, 1045, 0],
[101, 777, 227],
[101, 888, 228]
])
I tried with c = torch.cat((a, b), dim=0) but does not work.
CodePudding user response:
In general you could try to pad the first tensor to the shape of the other. For this exact problem you could do that the following way:
import torch
import torch.nn.functional as F
a = torch.tensor([
[101, 103],
[101, 1045]
])
b = torch.tensor([
[101, 777, 227],
[101, 888, 228]
])
a = F.pad(a, pad=(0, b.size()[1] - a.size()[1], 0, b.size()[0] - a.size()[0]), value=0)
print(a)
This extends the first and second dimension of the a
tensor with zeros to match the b
tensor.
You can then concat them:
c = torch.cat((a, b), dim=0)