pytorch常用的乘法运算有哪些,如何使用
Admin 2022-09-30 群英技术资讯 496 次浏览
总结放前面:
torch.mm : 用于两个矩阵(不包括向量)的乘法。如维度为(l,m)和(m,n)相乘
torch.bmm : 用于带batch的三维向量的乘法。如维度为(b,l,m)和(b,m,n)相乘
torch.mul : 用于两个同维度矩阵的逐像素点相乘(点乘)。如维度为(l,m)和(l,m)相乘
torch.mv : 用于矩阵和向量之间的乘法(矩阵在前,向量在后)。如维度为(l,m)和(m)相乘,结果的维度为(l)。
torch.matmul : 用于两个张量(后两维满足矩阵乘法的维度)相乘或者是矩阵与向量间的乘法,因为其具有广播机制(broadcasting,自动补充维度)。如维度为(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)相乘等。【其作用包含torch.mm、torch.bmm和torch.mv】
@运算符 : 其作用类似于torch.matmul。
*运算符 : 其作用类似于torch.mul。
import torch a = torch.ones(1, 2) print(a) b = torch.ones(2, 3) print(b) output = torch.mm(a, b) print(output) print(output.size()) """ tensor([[1., 1.]]) tensor([[1., 1., 1.], [1., 1., 1.]]) tensor([[2., 2., 2.]]) torch.Size([1, 3]) """
a = torch.randn(2, 1, 2) print(a) b = torch.randn(2, 2, 3) print(b) output = torch.bmm(a, b) print(output) print(output.size()) """ tensor([[[-0.1187, 0.2110]], [[ 0.7463, -0.6136]]]) tensor([[[-0.1186, 1.5565, 1.3662], [ 1.0199, 2.4644, 1.1630]], [[-1.9483, -1.6258, -0.4654], [-0.1424, 1.3892, 0.7559]]]) tensor([[[ 0.2293, 0.3352, 0.0832]], [[-1.3666, -2.0657, -0.8111]]]) torch.Size([2, 1, 3]) """
a = torch.ones(2, 3) * 2 print(a) b = torch.randn(2, 3) print(b) output = torch.mul(a, b) print(output) print(output.size()) """ tensor([[2., 2., 2.], [2., 2., 2.]]) tensor([[-0.1187, 0.2110, 0.7463], [-0.6136, -0.1186, 1.5565]]) tensor([[-0.2375, 0.4220, 1.4925], [-1.2271, -0.2371, 3.1130]]) torch.Size([2, 3]) """
mat = torch.randn(3, 4) print(mat) vec = torch.randn(4) print(vec) output = torch.mv(mat, vec) print(output) print(output.size()) print(torch.mm(mat, vec.unsqueeze(1)).squeeze(1)) """ tensor([[-0.1187, 0.2110, 0.7463, -0.6136], [-0.1186, 1.5565, 1.3662, 1.0199], [ 2.4644, 1.1630, -1.9483, -1.6258]]) tensor([-0.4654, -0.1424, 1.3892, 0.7559]) tensor([ 0.5982, 2.5024, -5.2481]) torch.Size([3]) tensor([ 0.5982, 2.5024, -5.2481]) """
# 其作用包含torch.mm、torch.bmm和torch.mv。其他类似,不一一举例。 a = torch.randn(2, 1, 2) print(a) b = torch.randn(2, 2, 3) print(b) output = torch.bmm(a, b) print(output) output1 = torch.matmul(a, b) print(output1) print(output1.size()) """ tensor([[[-0.1187, 0.2110]], [[ 0.7463, -0.6136]]]) tensor([[[-0.1186, 1.5565, 1.3662], [ 1.0199, 2.4644, 1.1630]], [[-1.9483, -1.6258, -0.4654], [-0.1424, 1.3892, 0.7559]]]) tensor([[[ 0.2293, 0.3352, 0.0832]], [[-1.3666, -2.0657, -0.8111]]]) tensor([[[ 0.2293, 0.3352, 0.0832]], [[-1.3666, -2.0657, -0.8111]]]) torch.Size([2, 1, 3]) """
# 维度为(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)等 a = torch.randn(2, 3, 4) b = torch.randn(2, 4, 5) print(torch.matmul(a, b).size()) a = torch.randn(3, 4) b = torch.randn(2, 4, 5) print(torch.matmul(a, b).size()) a = torch.randn(2, 3, 3, 4) b = torch.randn(2, 3, 4, 5) print(torch.matmul(a, b).size()) a = torch.randn(2, 3) b = torch.randn(3) print(torch.matmul(a, b).size()) """ torch.Size([2, 3, 5]) torch.Size([2, 3, 5]) torch.Size([2, 3, 3, 5]) torch.Size([2]) """
# @运算符:其作用类似于torch.matmul a = torch.randn(2, 3, 4) b = torch.randn(2, 4, 5) print(torch.matmul(a, b).size()) print((a @ b).size()) a = torch.randn(3, 4) b = torch.randn(2, 4, 5) print(torch.matmul(a, b).size()) print((a @ b).size()) a = torch.randn(2, 3, 3, 4) b = torch.randn(2, 3, 4, 5) print(torch.matmul(a, b).size()) print((a @ b).size()) a = torch.randn(2, 3) b = torch.randn(3) print(torch.matmul(a, b).size()) print((a @ b).size()) """ torch.Size([2, 3, 5]) torch.Size([2, 3, 5]) torch.Size([2, 3, 5]) torch.Size([2, 3, 5]) torch.Size([2, 3, 3, 5]) torch.Size([2, 3, 3, 5]) torch.Size([2]) torch.Size([2]) """
# *运算符:其作用类似于torch.mul a = torch.ones(2, 3) * 2 print(a) b = torch.ones(2, 3) * 3 print(b) output = torch.mul(a, b) print(output) print(output.size()) output1 = a * b print(output1) print(output1.size()) """ tensor([[2., 2., 2.], [2., 2., 2.]]) tensor([[3., 3., 3.], [3., 3., 3.]]) tensor([[6., 6., 6.], [6., 6., 6.]]) torch.Size([2, 3]) tensor([[6., 6., 6.], [6., 6., 6.]]) torch.Size([2, 3]) """
神经网络中包含大量的 2D 张量矩阵乘法运算,而使用 torch.matmul 函数比较复杂,因此 PyTorch 提供了更为简单方便的 torch.mm(input, other, out = None)
函数。下表是 torch.matmul 函数和 torch.mm 函数的简单对比。
torch.matmul 函数支持广播,主要指的是当参与矩阵乘积运算的两个张量中其中有一个是 1D 张量,torch.matmul 函数会将其广播成 2D 张量参与运算,最后将广播添加的维度删除作为最终 torch.matmul 函数的返回结果。torch.mm 函数不支持广播,相对应的输入的两个张量必须为 2D。
import torch input = torch.tensor([[1., 2.], [3., 4.]]) other = torch.tensor([[5., 6., 7.], [8., 9., 10.]]) result = torch.mm(input, other) print(result) # tensor([[21., 24., 27.], # [47., 54., 61.]])
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PyNaCI能够提供数字签名、密钥加密、公钥加密、哈希和消息身份验证、基于密码的密钥派生和密码散列功能,这篇文章主要介绍了python密码学库pynacl,感兴趣的朋友一起看看吧
这篇文章介绍了Python使用email、smtplib、poplib、imaplib模块收发邮件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要给大家分享的是Python技巧之变长和定长序列拆分,Python中的任何序列(可迭代的对象)都可以通过赋值操作进行拆分,包括但不限于元组、列表、字符串、文件、迭代器、生成器等。想了解更多详细的小伙伴请参考下面文章内容
这篇文章主要介绍了python通过Seq2Seq实现闲聊机器人,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好的帮助,需要的朋友可以参考下
word、excel、PPT,虽然说是特殊文件,其实也是实际工作中我们经常会用到的文件类型。本文将为大家详解Python读取Word文件和文件内容的方法,感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008