How do I multiply every row of a tensor with every row of the other tensor?

Suppose I have a tensor X of dimension a x a, and another tensor Y of the same dimension. How do I multiply every row of X with every row of Y and get an $a x a$ tensor? Thanks!

What do you want is just a matrix multiplication with the transpose.

import torch
x = torch.Tensor([[1, 2], [3, 4]])
y = torch.Tensor([[1, 2], [3, 4]]) + 1
x.matmul(y.transpose(0, 1))
1 Like