目录

  • *位置乘
  • torch.mul():数乘
  • torch.mv():矩阵向量乘法
  • torch.mm() 矩阵乘法
  • torch.dot() 点乘积
  • @操作
  • torch.matmul()

*位置乘

符号*在pytorch中是按位置相乘,存在广播机制。

例子:

vec1 = torch.arange(4)
vec2 = torch.tensor([4,3,2,1])
mat1 = torch.arange(12).reshape(4,3)
mat2 = torch.arange(12).reshape(3,4)print(vec1 * vec2)
print(mat2 * vec1)
print(mat1 * mat1)Output:
tensor([0, 3, 4, 3])
tensor([[ 0,  1,  4,  9],[ 0,  5, 12, 21],[ 0,  9, 20, 33]])
tensor([[  0,   1,   4],[  9,  16,  25],[ 36,  49,  64],[ 81, 100, 121]])

torch.mul():数乘

官方解释:

就是两个变量对应元素相乘,other可以为一个数,也可以为一个tensor变量

torch.mul()支持广播机制

例子1:
‘’‘python
In[1]: vec = torch.randn(3)
In[2]: vec
Out[1]: tensor([0.3550, 0.0975, 1.3870])
In[3]: torch.mul(vec, 5)
Out[2]: tensor([1.7752, 0.4874, 6.9348])
‘’’

例子2:
‘’'python
In[1]: vec = torch.randn(3)
In[2]: vec
Out[1]: tensor([1.7752, 0.4874, 6.9348])
In[3]: mat = torch.randn(4).view(-1,1)
In[4]: mat
Out[2]: tensor([[-1.5181],
[ 0.4905],
[-0.3388],
[ 0.5626]])

In[5]:torch.mul(vec,mat)
Out[3]:tensor([[-0.5390, -0.1480, -2.1055],
[ 0.1741, 0.0478, 0.6803],
[-0.1203, -0.0330, -0.4699],
[ 0.1998, 0.0548, 0.7803]])
‘’’

torch.mv():矩阵向量乘法

官方文档写道:Performs a matrix-vector product of the matrix input and the vector vec.
说明torch.mv(input, vec, *, out=None)->tensor只支持矩阵向量乘法,如果input为 n × m n\times m n×m的,vec向量的长度为m,那么输出为 n × 1 n\times 1 n×1的向量。
torch.mv()不支持广播机制
例子:

In[1]: vec = torch.arange(4)
In[2]: mat = torch.arange(12).reshape(3,4)
In[3]: torch.mv(mat, vec)
Out[1]: tensor([14, 38, 62])

torch.mm() 矩阵乘法

官方文档写道:Performs a matrix multiplication of the matrices input and mat2.
torch.mm(input , mat2, *, out=None) → Tensor
对矩阵input 和mat2进行相乘。 如果input 是一个n×m张量,mat2 是一个 m×p张量,将会输出一个 n×p张量out。
torch.mm()不支持广播机制
这个就是线性代数中的矩阵乘法。

例子:

In[1]: mat1 = torch.arange(12).reshape(3,4)
In[2]: mat2 = torch.arange(12).reshape(4,3)
In[3]: torch.mm(mat1, mat2)
Out[1]: tensor([[ 42,  48,  54],[114, 136, 158],[186, 224, 262]])

torch.dot() 点乘积

官方文档写道:Computes the dot product of two 1D tensors.
只能支持两个一维向量,与numpy中dot()方法不同。
torch.dot(input, other, *, out=None) → Tensor

例子:

In[1]: torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1]))
Out[1]: tensor[7]

@操作

torch中的@操作是可以实现前面几个函数,是一种强大的操作。
mat1 @ mat2

  • 若mat1和mat2都是两个一维向量,那么对应操作就是torch.dot()
  • 若mat1是二维向量,mat2是一维向量,那么对应操作就是torch.mv()
  • 若mat1和mat2都是两个二维向量,那么对应操作就是torch.mm()
vec1 = torch.arange(4)
vec2 = torch.tensor([4,3,2,1])
mat1 = torch.arange(12).reshape(4,3)
mat2 = torch.arange(12).reshape(3,4)print(vec1 @ vec2) # 两个一维向量
print(mat2 @ vec1) # 一个二维和一个一维
print(mat1 @ mat2) # 两个二维向量Output:
tensor(10)
tensor([14, 38, 62])
tensor([[ 20,  23,  26,  29],[ 56,  68,  80,  92],[ 92, 113, 134, 155],[128, 158, 188, 218]])

torch.matmul()

torch.matmul()@操作类似,但是torch.matmul()不止局限于一维和二维,可以进行高维张量的乘法。
torch.matmul(input, other, *, out=None) → Tensor 支持广播

torch.matmul()运算取决于inputother张量的大小:

    1. 如果输入的两个张量都是一维的,那么返回点积,对应的操作就是torch.dot()
    1. 如果输入的两个张量都是二维的,那么返回矩阵乘积,对应的操作就是torch.mm()
    1. 如果输入的第一个张量是二维的,第二个张量是一维的,则返回矩阵向量乘积,对应torch.mv()
    1. 如果输入的第一个张量是一维的,第二个参数是二维的,那么torch.matmul()操作会先将第一个张量的维度前面添加1,在执行矩阵相乘后,再将添加的维度移除。
    1. 如果两个参数至少是维一维的且至少一个参数是N维(N>2),则进行批处理矩阵乘法,如果第一个参数是一维,则在第一个参数的维度前面加1,在进行批处理矩阵相乘后在删除。如果第二个参数是一维的,则在第二个参数的维度后面加1,在进行批处理矩阵相乘后再删除。

例子1(对应1到4运算):


vec1 = torch.tensor([1,2,3,4])
vec2 = torch.tensor([4,3,2,1])
mat1 = torch.arange(12).reshape(3,4)
mat2 = torch.arange(12).reshape(4,3)
print(torch.matmul(vec1, vec2)) # 两个向量都是 一维的, 类似torch.dot()操作
print(torch.matmul(mat1, mat2)) # 两个向量都是 二维的, 类似torch.mm()操作
print(torch.matmul(mat1, vec1)) # 第一个向量是 二维的,第二个向量是 一维的,类似 torch.mv()操作
print(torch.matmul(vec1, mat2)) # 第一个向量是 一维的,第二个向量是二维的Output:
tensor(20)
tensor([[ 42,  48,  54],[114, 136, 158],[186, 224, 262]])
tensor([ 20,  60, 100])
tensor([60, 70, 80])

第五种情况,当出现多维的情况:
记住一点,如果多维,永远是最后面两维度相乘,然后再将前面的维度补上

例子2: 如果第一个向量是一维的,第二个向量是多维的。

tensor1 = torch.randn(2)
tensor2 = torch.randn(10,2,3)
print(torch.matmul(tensor1, tensor2).size())Output:
torch.Size([10, 3])

先将tensor2前面维度10作为batch提出来,使其变成二维(2 * 3),将tensor1的维度前面加1,那么就变成了1*2(二维),然后维度1*2和维度2 * 3做矩阵乘法,得到1 * 3,再将tensor1添加的维度1删除,最终得到维度10*3

例子3:如果第一个向量是多维的,第二个向量是一维的

tensor1 = torch.randn(10,3,4)
tensor2 = torch.randn(4)
print(torch.matmul(tensor1, tensor2).shape)Output:
torch.Size([10, 3])

先将tensor2的维度后面加1,那么tensor2维度就变成了4*1(二维),接着,把tensor1前面的维度10作为batch提出来,使其变成二维(3 * 4),然后维度3 * 4和维度4 * 1做矩阵乘法,得到3 * 1,再将tensor2添加的维度1删掉,最终加上batch的维度,得到维度10 * 3

例子4:第一个向量3维,第二个向量2维

tensor1 = torch.randn(2,5,3)
tensor2 = torch.randn(3,4)
print(torch.matmul(tensor1, tensor2).shape)Output:
torch.Size([2, 5, 4])

先将tensor1中多出的一维提取出来,其余部分做矩阵乘法

例子5: 第一个向量2维,第二个向量3维

tensor1 = torch.randn(5,3)
tensor2 = torch.randn(2,3,4)
print(torch.matmul(tensor1, tensor2).shape)Output:
torch.Size([2, 5, 4])

先将tensor2中多出的一维提取出来,其余部分做矩阵乘法

例子6: 当两个都是多维

tensor1 = torch.randn(5,1,5,3)
tensor2 = torch.randn(2,3,4)
print(torch.matmul(tensor1, tensor2).shape)Output:
torch.Size([5, 2, 5, 4])

先将tensor1中多余的一维提取出来,剩下三维,将tensor1中做广播机制,变成2 * 5 * 3,接着将tensor1和tensor2中最后两维做矩阵乘法,得到5 * 4, 最终得到维度(5 * 2 * 5 * 4)

torch中乘法整理,*torch.mul()torch.mv()torch.mm()torch.dot()@torch.mutmal()相关推荐

  1. 【增强学习】Torch中的增强学习层

    要想在Torch框架下解决计算机视觉中的增强学习问题(例如Visual Attention),可以使用Nicholas Leonard提供的dpnn包.这个包对Torch中原有nn包进行了强大的扩展, ...

  2. torch中的乘法符号(*),torch.mm()和torch.matmul(),torch.mul(), torch.bmm()

    前言 torch中常见的一些矩阵乘法和元素乘积,说白了无非就是以下四种,为了避免忘了,做个笔记 乘法符号 * torch.mul() torch.mm torch.matmul torch.dot 1 ...

  3. torch中的spmm

    系列文章目录 本系列记录自己的代码学习知识 torch.matmul的前后两个矩阵维度不同的小结 torch中的transpose和view的不同 torch中的spmm 系列文章目录 前言 一.to ...

  4. Torch中的矩阵相乘分类

    矩阵相乘在torch中的几种情况 1.矩阵逐元素(Element-wise)乘法 torch.mul(mat1, other) mat和other可以是标量也可以是任意维度的矩阵,只要满足最终相乘是可 ...

  5. Torch 中添加自己的 nn Modules:以添加 Dropout、 Triplet Loss 为例

    Preface 因为要复现前面阅读的一篇论文:<论文笔记:Deep Relative Distance Learning: Tell the Difference Between Similar ...

  6. torch中的copy()和clone()

    torch中的copy()和clone() 1.torch中的copy()和clone() y = torch.Tensor(2,2):copy(x) --- 1 修改y并不改变原来的x y = x: ...

  7. torch中的topk()函数

    torch中的topk()函数 In [2]: import torchIn [3]: a=torch.randn((4,6))In [4]: a Out

  8. einops包中的rearrange,reduce, repeat及einops.layers.torch中的Rearrange,Reduce。对高维数据的处理方式

    from einops import rearrange, reduce, repeat from einops.layers.torch import Rearrange, Reduce 一.rea ...

  9. (原)torch中微调某层参数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221664.html 参考网址: https://github.com/torch/nn/issues ...

最新文章

  1. vasp算表面吸附流程_VASP实例分析表面吸附计算
  2. VMware 克隆Linux后找不到eth0
  3. 构建之法阅读笔记01
  4. 前端笔记——获取url里面的参数值
  5. 电气接线+线号管正确方向=电工接线好习惯!你有吗?
  6. HDU 2502 月之数(简单递推)
  7. leetcode 152. Maximum Product Subarry
  8. 在Java中将时间单位转换为持续时间
  9. access 文本转换数字_LabVIEW访问Access数据库教程
  10. 恶意软件借手机游戏强行吸取流量,使用代码签名证书验明证身
  11. poj 3256 Cow Picnic 优化深搜
  12. java 基本语法 二_java基础语法2
  13. matlab求解数学题,实验二Matlab求解数学问题
  14. 用HTML5做一个个人网站,此文仅展示个人主页界面。内附源代码下载地址
  15. 100套精品PPT模板免费拿!以后再也不用怕老板叫你制作PPT了
  16. ce修改植物大战僵尸之植物无冷却
  17. 2019CCPC湖南全国邀请赛(广东省赛、江苏省赛)重现赛
  18. yxc_第一章 基础算法(三)_离散化
  19. 为Eclipse安装lombok插件
  20. 职业生涯规划中的刺猬理念

热门文章

  1. 阿里云服务器,修改Apache2默认端口80
  2. Mac新手入门功能操作指南!!
  3. 微信公众号 测试号 申请
  4. 计算机开机慢怎么办,笔记本电脑开机很慢怎么办?五个小妙招来帮忙!
  5. Android NFC详解(高级)
  6. HTML+CSS期末大作业:动漫网站设计——悬崖上的金鱼姬(5页) / 动漫网页设计作业,网页设计作业 / 动漫网页设计成品 学生DW网页设计作业成品 web课程设计网页规划与设计...
  7. PPT打开出错/可尝试修复此演示文稿
  8. 《火车运煤问题》分析
  9. 综述:人工智能、数据科学、机器学习
  10. Qt For Android 屏幕常亮