Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch batch matrix-matrix outer product

Similarly to the question in Pytorch batch matrix vector outer product I have two matrices and would like to compute their outer product, or in other words the pairwise elementwise product.

Shape example: If we have X1 and X2 of shapes of torch.Size([32, 300, 8]) The result should be of size torch.Size([32, 300, 300, 8])

like image 881
Codevan Avatar asked Aug 26 '26 14:08

Codevan


1 Answers

You can add singleton dimensions:

X1[:, None, ...] * X1[..., None, :]

But Usman Ali's comment is also a good idea. Use torch.einsum:

torch.einsum('bik,bjk->bijk', X1, X2)
like image 184
Shai Avatar answered Aug 29 '26 04:08

Shai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!