详解torch.Tensor的4种乘法
作者:da_kao_la 时间:2023-08-11 21:09:03
torch.Tensor有4种常见的乘法:*, torch.mul, torch.mm, torch.matmul. 本文抛砖引玉,简单叙述一下这4种乘法的区别,具体使用还是要参照官方文档。
点乘
a与b做*乘法,原则是如果a与b的size不同,则以某种方式将a或b进行复制,使得复制后的a和b的size相同,然后再将a和b做element-wise的乘法。
下面以*标量和*一维向量为例展示上述过程。
* 标量
Tensor与标量k做*乘法的结果是Tensor的每个元素乘以k(相当于把k复制成与lhs大小相同,元素全为k的Tensor).
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> a * 2
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]])
* 一维向量
Tensor与行向量做*乘法的结果是每列乘以行向量对应列的值(相当于把行向量的行复制,成为与lhs维度相同的Tensor). 注意此时要求Tensor的列数与行向量的列数相等。
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> b = torch.Tensor([1,2,3,4])
>>> b
tensor([1., 2., 3., 4.])
>>> a * b
tensor([[1., 2., 3., 4.],
[1., 2., 3., 4.],
[1., 2., 3., 4.]])
Tensor与列向量做*乘法的结果是每行乘以列向量对应行的值(相当于把列向量的列复制,成为与lhs维度相同的Tensor). 注意此时要求Tensor的行数与列向量的行数相等。
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> b = torch.Tensor([1,2,3]).reshape((3,1))
>>> b
tensor([[1.],
[2.],
[3.]])
>>> a * b
tensor([[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.]])
* 矩阵
经Arsmart在评论区提醒,增补一个矩阵 * 矩阵的例子,感谢Arsmart的热心评论!
如果两个二维矩阵A与B做点积A * B,则要求A与B的维度完全相同,即A的行数=B的行数,A的列数=B的列数
>>> a = torch.tensor([[1, 2], [2, 3]])
>>> a * a
tensor([[1, 4],
[4, 9]])
broadcast
点积是broadcast的。broadcast是torch的一个概念,简单理解就是在一定的规则下允许高维Tensor和低维Tensor之间的运算。broadcast的概念稍显复杂,在此不做展开,可以参考官方文档关于broadcast的介绍. 在torch.matmul里会有关于broadcast的应用的一个简单的例子。
这里举一个点积broadcast的例子。在例子中,a是二维Tensor,b是三维Tensor,但是a的维度与b的后两位相同,那么a和b仍然可以做点积,点积结果是一个和b维度一样的三维Tensor,运算规则是:若c = a * b
, 则c[i,*,*] = a * b[i, *, *]
,即沿着b的第0维做二维Tensor点积,或者可以理解为运算前将a沿着b的第0维也进行了expand操作,即a = a.expand(b.size()); a * b
。
>>> a = torch.tensor([[1, 2], [2, 3]])
>>> b = torch.tensor([[[1,2],[2,3]],[[-1,-2],[-2,-3]]])
>>> a * b
tensor([[[ 1, 4],
[ 4, 9]],
[[-1, -4],
[-4, -9]]])
>>> b * a
tensor([[[ 1, 4],
[ 4, 9]],
[[-1, -4],
[-4, -9]]])
其实,上面提到的二维Tensor点积标量、二维Tensor点积行向量,都是发生在高维向量和低维向量之间的,也可以看作是broadcast.
torch.mul
官方文档关于torch.mul的介绍. 用法与*乘法相同,也是element-wise的乘法,也是支持broadcast的。
下面是几个torch.mul的例子.
乘标量
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> a * 2
tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]])
乘行向量
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> b = torch.Tensor([1,2,3,4])
>>> b
tensor([1., 2., 3., 4.])
>>> torch.mul(a, b)
tensor([[1., 2., 3., 4.],
[1., 2., 3., 4.],
[1., 2., 3., 4.]])
乘列向量
>>> a = torch.ones(3,4)
>>> a
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> b = torch.Tensor([1,2,3]).reshape((3,1))
>>> b
tensor([[1.],
[2.],
[3.]])
>>> torch.mul(a, b)
tensor([[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.]])
乘矩阵
例1:二维矩阵 mul 二维矩阵
>>> a = torch.tensor([[1, 2], [2, 3]])
>>> torch.mul(a,a)
tensor([[1, 4],
[4, 9]])
例2:二维矩阵 mul 三维矩阵(broadcast)
>>> a = torch.tensor([[1, 2], [2, 3]])
>>> b = torch.tensor([[[1,2],[2,3]],[[-1,-2],[-2,-3]]])
>>> torch.mul(a,b)
tensor([[[ 1, 4],
[ 4, 9]],
[[-1, -4],
[-4, -9]]])
torch.mm
官方文档关于torch.mm的介绍. 数学里的矩阵乘法,要求两个Tensor的维度满足矩阵乘法的要求.
例子:
>>> a = torch.ones(3,4)
>>> b = torch.ones(4,2)
>>> torch.mm(a, b)
tensor([[4., 4.],
[4., 4.],
[4., 4.]])
torch.matmul
官方文档关于torch.matmul的介绍. torch.mm的broadcast版本.
例子:
>>> a = torch.ones(3,4)
>>> b = torch.ones(5,4,2)
>>> torch.matmul(a, b)
tensor([[[4., 4.],
[4., 4.],
[4., 4.]],
[[4., 4.],
[4., 4.],
[4., 4.]],
[[4., 4.],
[4., 4.],
[4., 4.]],
[[4., 4.],
[4., 4.],
[4., 4.]],
[[4., 4.],
[4., 4.],
[4., 4.]]])
同样的a和b,使用torch.mm相乘会报错
>>> torch.mm(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: matrices expected, got 2D, 3D tensors at /pytorch/aten/src/TH/generic/THTensorMath.cpp:2065
来源:https://blog.csdn.net/da_kao_la/article/details/87484403
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python装饰器代码深入讲解
python如何发送带有附件、正文为HTML的邮件
![](https://img.aspxhome.com/file/2023/2/107022_0s.jpg)
详解Python中字典的增删改查
![](https://img.aspxhome.com/file/2023/5/131025_0s.jpg)
SQL Server误区30日谈 第7天 一个实例多个镜像和日志传送延迟
django框架model orM使用字典作为参数,保存数据的方法分析
如何关掉pycharm中的python console(图解)
![](https://img.aspxhome.com/file/2023/2/135292_0s.jpg)
Windows系统下mysql5.7.21安装详细教程
![](https://img.aspxhome.com/file/2023/9/124509_0s.jpg)
如何将服务器上的python代码通过QQ发送回传信息(附实现方法)
![](https://img.aspxhome.com/file/2023/8/109518_0s.jpg)
python控制台打印log输出重复的解决方法
![](https://img.aspxhome.com/file/2023/8/105578_0s.jpg)
黄相如:如何做好用户体验
自定义PyCharm快捷键的设置方式
![](https://img.aspxhome.com/file/2023/4/112454_0s.png)
Mysql删除重复数据并且只保留一条(附实例!)
![](https://img.aspxhome.com/file/2023/8/122348_0s.png)
MySQL数据库基础篇SQL窗口函数示例解析教程
![](https://img.aspxhome.com/file/2023/1/128081_0s.png)
python机器学习Sklearn实战adaboost算法示例详解
![](https://img.aspxhome.com/file/2023/9/109359_0s.jpg)
Python3.x+pyqtgraph实现数据可视化教程
![](https://img.aspxhome.com/file/2023/5/69515_0s.jpg)
python3.6的venv模块使用详解
![](https://img.aspxhome.com/file/2023/8/68388_0s.png)
js+html5通过canvas指定开始和结束点绘制线条的方法
Python批量修改xml的坐标值全部转为整数的实例代码
![](https://img.aspxhome.com/file/2023/8/65548_0s.jpg)
安装sql server 2008时的4个常见错误和解决方法
![](https://img.aspxhome.com/file/2023/8/98018_0s.png)
如何用Axure制作Tab页签
![](https://img.aspxhome.com/file/UploadPic/20092/8/1-19s.jpg)