Let x
and emb
be 2D matrices of size (bsz, n)
and (n, m)
respectively.
x = torch.FloatTensor([[0,1,2], [3,4,5]])
emb = torch.FloatTensor([[0,1,2,3], [4,5,6,7], [8,9,10,11]])
# x
# tensor([[ 0., 1., 2.],
# [ 3., 4., 5.]])
# emb
# tensor([[ 0., 1., 2., 3.],
# [ 4., 5., 6., 7.],
# [ 8., 9., 10., 11.]])
I want the result to be a 3D tensor of size (bsz, n, m)
where out[j, i, :] = x[j, i] * emb[i, :]
. I am using a loop for now as below but I thought there might be a better way?
out = torch.zeros(bsz, n, m)
for i in range(bsz):
out[i] = x[i].view(-1, 1) * emb
# out
# tensor([[[ 0., 0., 0., 0.],
# [ 4., 5., 6., 7.],
# [ 16., 18., 20., 22.]],
#
# [[ 0., 3., 6., 9.],
# [ 16., 20., 24., 28.],
# [ 40., 45., 50., 55.]]])​