matmul 和通常的张量乘法有区别吗

is there any difference between matmul and usual multiplication of tensors

我对使用 * 和 matmul 的两个张量之间的乘法感到困惑。 下面是我的代码

import torch
torch.manual_seed(7)
features = torch.randn((2, 5))
weights = torch.randn_like(features)

在这里,我想乘以权重和特征。所以,一种方法如下

print(torch.sum(features * weights))

输出:

tensor(-2.6123)

另一种方法是使用 matmul

print(torch.mm(features,weights.view((5,2))))

但是,这里的输出是

tensor([[ 2.8089,  4.6439],
        [-2.3988, -1.9238]])

我在这里不明白的是为什么 matmul 和通常的乘法在两者相同时给出不同的输出。我做错了什么吗?

编辑:当我使用形状特征 (1,5) * 和 matmul 输出相同时。 但是,当形状为 (2,5).

时就不一样了

当你使用*时,乘法是逐元素的,当你使用torch.mm时,它是矩阵乘法。

示例:

a = torch.rand(2,5)
b = torch.rand(2,5)
result = a*b 

result 的形状与 ab 相同,即 (2,5) 而考虑操作

result = torch.mm(a,b)

它会给出大小不匹配的错误,因为这是适当的矩阵乘法(正如我们在线性代数中学习的那样)和 a.shape[1] != b.shape[0]。当您在 torch.mm 中应用视图操作时,您正在尝试匹配尺寸。

在某些特定维度的形状为 1 的特殊情况下,它成为点积,因此 sum (a*b)mm(a, b.view(5,1))

相同