使用torch.mm(a, b)
或torch.matmul(a, b)
两者都一样。
>>> torch.mm
<built-in method mm of type object at 0x11712a870>
>>> torch.matmul
<built-in method matmul of type object at 0x11712a870>
还有另一种可能很了解的选项。那是@
运算符。 @西蒙·H。
>>> a = torch.randn(2, 3)
>>> b = torch.randn(3, 4)
>>> a@b
tensor([[ 0.6176, -0.6743, 0.5989, -0.1390],
[ 0.8699, -0.3445, 1.4122, -0.5826]])
>>> a.mm(b)
tensor([[ 0.6176, -0.6743, 0.5989, -0.1390],
[ 0.8699, -0.3445, 1.4122, -0.5826]])
>>> a.matmul(b)
tensor([[ 0.6176, -0.6743, 0.5989, -0.1390],
[ 0.8699, -0.3445, 1.4122, -0.5826]])
这三个给出相同的结果。
0
在numpy中,我可以像这样做一个简单的矩阵乘法:
但是,当我使用PyTorch Tensors尝试此操作时,这不起作用:
此代码引发以下错误:
有什么想法可以在PyTorch中进行矩阵乘法吗?