千家信息网

pytorch中常用的乘法运算有哪些

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"pytorch中常用的乘法运算有哪些",在日常操作中,相信很多人在pytorch中常用的乘法运算有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"p
千家信息网最后更新 2026年11月02日pytorch中常用的乘法运算有哪些

这篇文章主要介绍"pytorch中常用的乘法运算有哪些",在日常操作中,相信很多人在pytorch中常用的乘法运算有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"pytorch中常用的乘法运算有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

总结放前面:

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。

1、torch.mm

import torcha = 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])"""

2、torch.bmm

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])"""

3、torch.mul

a = torch.ones(2, 3) * 2print(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])"""

4、torch.mv

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])"""

5、torch.matmul

# 其作用包含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])"""

6、@运算符

# @运算符:其作用类似于torch.matmula = 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])"""

7、*运算符

# *运算符:其作用类似于torch.mula = torch.ones(2, 3) * 2print(a)b = torch.ones(2, 3) * 3print(b)output = torch.mul(a, b)print(output)print(output.size())output1 = a * bprint(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 torchinput = 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.]])

到此,关于"pytorch中常用的乘法运算有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

运算 乘法 维度 矩阵 函数 作用 张量 运算符 两个 向量 广播 常用 学习 更多 结果 帮助 支持 复杂 实用 接下来 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网是不是新科技 互联网扶贫科技 狮子座互联网科技 互联网科技小贷 云电互联网科技 互联网金融科技平台 互联网科技公司介绍 成都玖崟互联网科技有限公司乐商圈怎么样 成都铂石互联网科技做什么的 福建科技产业互联网发展 千方科技属于互联网企业吗 哈尔滨屹滨互联网科技有限公司 河南住房之家互联网科技 四川慕妮娅互联网科技有限公司 国大互联网教育科技有限公司薪资 上海创道互联网络科技有限公司 西安左小互联网科技有限公司 青岛易易互联网科技 10位科技互联网大佬 利用互联网等科技手段 新华互联网科技学校 互联网核心科技是什么 湖北码尼互联网科技有限公司电话 昊天互联网科技有限公司 互联网5g黑科技有哪些 群盈汇互联网科技有限公司 中国互联网有什么科技 中国新注册互联网科技 北京军民互联网科技公司 现代互联网科技公司
0