是否有一种矢量化的方法来创建一个矩阵,其中每个元素都是矩阵的 row-wise 点积?

Is there a vectorized way to create a matrix in which each element is the the row-wise dot product of a matrix?

抱歉标题含糊不清,我花了一些时间重新措辞,但不太好。

比如我在Pytorch张量中有一个2*3的矩阵

test = torch.tensor([[1, 10, 100],
                    [2, 20, 200]])

我想要的最终矩阵是

torch.tensor([10101, 20202],
             [20202, 40404])

这里可以看到,(0,0)位置是第一行和自己的点积,(0,1)(1,0)是第一行和第二行的点积,(1 ,1) 第二行与自身的点积。

谢谢!

您是否正在寻找矩阵及其转置的点积?

test.matmul(test.transpose(0,1))
print(test)
>>> tensor([[10101, 20202],
            [20202, 40404]])

你可以做矩阵乘法:

test @ test.T

或者 torch.einsum:

torch.einsum('ij,kj->ik', test, test)