当前位置: 首页 > news >正文

pytorch中各种乘法操作

文章目录

  • 各种乘法操作
  • 广播机制
  • torch.matmul
  • torch.mul
  • torch.mm
  • torch.bmm
  • dot()
  • @

各种乘法操作

torch.matmul 可广播,确保(…,N,M),(…,M,P) --> (…,N,P) 【前面其他维广播成相同维度】

torch.mul 可广播,保证维度相同,然后逐个元素相乘

torch.mm 不可广播,(M×N) × (N×P) → (M×P),二维矩阵相乘
torch.mm(a,b) == a@b == torch.matmul(a,b)

torch.bmm 不可广播,(B×M×N) × (B×N×P) → (B×M×P),二维矩阵相乘

dot() 必须保持维度一致,相乘相加
在这里插入图片描述

@ 等价于 torch.matmul()

广播机制

广播的执行过程:
1.如果维度个数不同,则在维度较少的左边补1,使得维度的个数相同。

2.各维度的维度大小不同时,如果有维度为1的,直接将该维拉伸至维度相同

torch.matmul

vector * vector = 相加相乘,最后得到一个数.

在 PyTorch 中,torch.matmul(或用 @ 运算符)支持 广播式矩阵乘法 。它的基本规则是:
如果 a.shape == (…, M, N), b.shape == (…, N, P)
那么 torch.matmul(a, b).shape == (…, M, P)
其中前面的 … 表示任意数量的前导维度,只要它们可以广播对齐即可。

#vector * vector = 相加相乘,最后得到一个数
a = torch.randn(3)
b = torch.randn(3)
c = torch.matmul(a,b)
"""
a: tensor([ 2.6767, -0.8028,  4.1741])
b: tensor([-1.0552,  0.2841,  0.8013])
torch.matmul: tensor(0.2923)
"""
#matrix * vector = 矩阵相乘,matrix第二维需要与vector维度相同
a = torch.randn(3,4)
b = torch.randn(4)
c = torch.matmul(a,b)
"""
a: tensor([[-1.2726,  0.6925, -0.3536, -0.2233],[-0.5659,  1.5294,  0.1152, -0.9903],[-0.2644,  0.5090,  0.7059,  0.2046]])
b: tensor([ 0.7085, -0.0952,  1.6654, -0.8139])
torch.matmul: tensor([-1.3747,  0.4513,  0.7733])
****************************************************
"""
# batched matrix x broadcasted vector
a = torch.randn(10, 3, 4)
b = torch.randn(4)
c = torch.matmul(a,b)
"""
在 PyTorch 中,torch.matmul(或用 @ 运算符)支持 广播式矩阵乘法 。它的基本规则是:
如果 a.shape == (..., M, N)
b.shape == (..., N, P)
那么 torch.matmul(a, b).shape == (..., M, P)
其中前面的 ... 表示任意数量的前导维度,只要它们可以广播对齐即可。
"""# batched matrix x batched matrix
a = torch.randn(10,1,3,4)
b = torch.randn(10,3,4,5)
c = torch.matmul(a,b)
"""
a: torch.Size([10, 1, 3, 4])
b: torch.Size([10, 3, 4, 5])
torch.matmul: torch.Size([10, 3, 3, 5])
"""
a = torch.randn(10,3,4)
b = torch.randn(10,4,5)
c = torch.matmul(a,b)
# torch.matmul: torch.Size([10, 3, 5])a = torch.randn(10,1,3,4)
b = torch.randn(10,4,5)
c = torch.matmul(a,b)
#  c:torch.Size([10, 1, 3, 5])# batched matrix x broadcasted matrix
a = torch.randn(10,3,4)
b = torch.randn(4, 5)
c = torch.matmul(a,b)
#  c:torch.Size([10, 3, 5])

torch.mul

若a是tensor,b是标量,则torch.mul(a, b)=b乘a中每个元素,得到与a一样的tensor;

若a是tensor,b是tensor,则torch.mul(a, b)会先对a、b进行广播,保持a、b维数一致,然后实现a和b elem-wise相乘;

import torch
a = torch.randn((2,3))
print('a:',a) 
# a: tensor([[ 0.5374,  0.5964, -0.9717], [ 0.8812,  0.0650,  0.5432]])
b = 100
c = torch.mul(a,b)
print('torch.mul:',c)
# torch.mul: tensor([[ 53.7366,  59.6445, -97.1669], [ 88.1228,   6.5001,  54.3242]])b = torch.randn((1,3)) #b的第二维度与a的第二维度若其中有一个不为1,则二者必须相等
print('b:',b)
# b: tensor([[-0.5949, -1.2634,  0.5233]])
c = torch.mul(a,b)
print('torch.mul:',c)
# 0.5374*-0.5949=-0.3197
# torch.mul: tensor([[-0.3197, -0.7535, -0.5084], [-0.5243, -0.0821,  0.2843]])a = torch.randn(4, 1)
b = torch.randn(1,4)
c = torch.mul(a,b) # (4,4)
print('torch.mul:',c)
"""
a: tensor([[ 1.1754],[ 0.0786],[-0.4219],[ 0.4158]])
b: tensor([[1.4896, 0.3157, 0.0024, 0.6818]])
###c[0][0]=1.1754*1.4896=1.7509e+00
###c[-1][-1]= 0.4158*0.6818=2.8348e-01
torch.mul: tensor([[ 1.7509e+00,  3.7115e-01,  2.7752e-03,  8.0140e-01],[ 1.1702e-01,  2.4805e-02,  1.8548e-04,  5.3562e-02],[-6.2843e-01, -1.3321e-01, -9.9606e-04, -2.8763e-01],[ 6.1936e-01,  1.3129e-01,  9.8170e-04,  2.8348e-01]])
"""

torch.mm

矩阵相乘,不会进行广播,必须满足矩阵相乘维数条件,两矩阵最多是2维

a = torch.randn(2, 3)
b = torch.randn(3, 3)
c = torch.mm(a,b)
print('torch.mm:',c.shape)
# torch.mm: torch.Size([2, 3])

torch.bmm

a = torch.randn(10,2, 3)
b = torch.randn(10,3, 3)
c = torch.bmm(a,b)
print('torch.bmm:',c.shape)
# torch.bmm: torch.Size([10, 2, 3])

dot()

在 PyTorch 中,torch.dot() 是一个用于计算 两个一维张量(向量)的点积(dot product) 的函数。它与矩阵乘法不同,仅适用于一维张量。

import torcha = torch.tensor([2, 3])
b = torch.tensor([4, 5])result = torch.dot(a, b)
print(result)  # 输出: 2*4 + 3*5 = 8 + 15 = 23
# 正确代码
a = torch.randn(3)
b = torch.randn(3)
c = torch.dot(a, b)  # 合法
# 错误代码
a = torch.randn(2, 3)
b = torch.randn(3)
c = torch.dot(a, b)  # 报错:Expected both vectors to be 1D!

在这里插入图片描述

@

在这里插入图片描述

相关文章:

  • OpenCV CUDA模块中逐元素操作------数学函数
  • HDMI信号采集器连OBS没有声音的问题
  • 盒带自编教材《软件工程》目录
  • 计算机网络:蜂窝网络和WiFi网络使用的射频信号有什么区别?
  • 计算机网络:什么是电磁波以及有什么危害?
  • Spring框架的事务管理
  • 每日算法-250515
  • ProfibusDP主站转ModbusRTU/TCP与横河AXG电磁流量计通讯案例
  • 麒麟系统ARM64架构部署mysql、jdk和java项目
  • docker(四)使用篇一:docker 镜像仓库
  • 【办公类-100-01】20250515手机导出教学照片,自动上传csdn+最大化、最小化Vs界面
  • 2025长三角杯数学建模A题:智能手机产品设计优化与定价问题,赛题发布与思路分析
  • 搭建Centos环境安装禅道
  • 深度解析 Meta 开源 MR 项目《North Star》:从交互到渲染的沉浸式体验设计
  • Python 类变量与实例变量完全指南:区别、使用场景及常见陷阱
  • 兰亭妙微B端UI设计:融合多元风格,点亮品牌魅力
  • 什么是接口文档,如何使用,注意事项有哪些
  • 51c~C语言~合集5
  • SQL:MySQL函数:空值处理函数(NULL Handling Functions)
  • Uniapp中小程序调用腾讯地图(获取定位地址)
  • 刘强东坐镇京东一线:管理层培训1800人次,最注重用户体验
  • 德国总理默茨发表首份政府声明:将提升国防能力,全力发展经济
  • 马上评|“衣服越来越难买”,对市场是一个提醒
  • 前四个月人民币贷款增加10.06万亿元,4月末M2余额同比增长8%
  • 沙青青评《通勤梦魇》︱“人机组合”的通勤之路
  • 4月份全国企业销售收入同比增长4.3%