如何在 PyTorch 中做矩阵的乘积 [英] How to do product of matrices in PyTorch

查看:46
本文介绍了如何在 PyTorch 中做矩阵的乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 numpy 中,我可以做一个简单的矩阵乘法,如下所示:

In numpy I can do a simple matrix multiplication like this:

a = numpy.arange(2*3).reshape(3,2)
b = numpy.arange(2).reshape(2,1)
print(a)
print(b)
print(a.dot(b))

但是,当我使用 PyTorch Tensors 尝试此操作时,这不起作用:

However, when I am trying this with PyTorch Tensors, this does not work:

a = torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
b = torch.Tensor([[2, 1]]).view(2, -1)
print(a)
print(a.size())

print(b)
print(b.size())

print(torch.dot(a, b))

此代码抛出以下错误:

运行时错误:张量大小不一致/Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:503

RuntimeError: inconsistent tensor size at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:503

任何想法如何在 PyTorch 中进行矩阵乘法?

Any ideas how matrix multiplication can be conducted in PyTorch?

推荐答案

您正在寻找

torch.mm(a,b)

请注意,torch.dot() 的行为与 np.dot() 不同.在这里,有一些关于什么是可取的讨论.具体来说,torch.dot()ab 都视为一维向量(无论它们的原始形状如何)并计算它们的内积.抛出错误,因为这种行为使您的 a 成为长度为 6 的向量,而您的 b 成为长度为 2 的向量;因此无法计算它们的内积.对于 PyTorch 中的矩阵乘法,使用 torch.mm().相比之下Numpy的np.dot()更加灵活;它计算一维数组的内积并为二维数组执行矩阵乘法.

Note that torch.dot() behaves differently to np.dot(). There's been some discussion about what would be desirable here. Specifically, torch.dot() treats both a and b as 1D vectors (irrespective of their original shape) and computes their inner product. The error is thrown, because this behaviour makes your a a vector of length 6 and your b a vector of length 2; hence their inner product can't be computed. For matrix multiplication in PyTorch, use torch.mm(). Numpy's np.dot() in contrast is more flexible; it computes the inner product for 1D arrays and performs matrix multiplication for 2D arrays.

根据大众需求,如果两个参数都是 2D,函数 torch.matmul 执行矩阵乘法,如果两个参数都是 1D,则计算它们的点积>.对于此类维度的输入,其行为与 np.dot 相同.它还允许您批量进行广播或matrix x matrixmatrix x vectorvector x vector 操作.有关详细信息,请参阅其文档.

By popular demand, the function torch.matmul performs matrix multiplications if both arguments are 2D and computes their dot product if both arguments are 1D. For inputs of such dimensions, its behaviour is the same as np.dot. It also lets you do broadcasting or matrix x matrix, matrix x vector and vector x vector operations in batches. For more info, see its docs.

# 1D inputs, same as torch.dot
a = torch.rand(n)
b = torch.rand(n)
torch.matmul(a, b) # torch.Size([])

# 2D inputs, same as torch.mm
a = torch.rand(m, k)
b = torch.rand(k, j)
torch.matmul(a, b) # torch.Size([m, j])

这篇关于如何在 PyTorch 中做矩阵的乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆