How to multiply vector on multi-dim Tensor?

I have a vector atten and a tensor feat. The shapes are:

>>> atten.shape
torch.Size([1, 13])
>>> feat.shape
torch.Size([13, 1024, 7, 7])

I want to do below calculation:
sum_i(atten[0][i] * feat[i])

I tried torch.matmul or * directly, but it didn’t work. Does anybody know how to make it right?

atten = torch.randn(1,13)
feat = torch.randn(13,1024,7,7)
atten = atten.view(13,1,1,1)
out = (feat * atten).sum(dim=0)
out.shape
torch.Size([1024, 7, 7])

1 Like

torch.einsum('i,ijkl', atten[0], feat)

Best regards

Thomas

thanks! but I’m using pytorch 0.3 which seems not having torch.einsum method

my solution should work

That’s a great occasion to upgrade! I don’t think its a reason in itself, but you’ll likely soon find yourself in a similar situation with something else. Just not having to fiddle with variables would be a good reason for me to upgrade.

Best regards

Thomas

yes, your solution works~谢了兄弟!