pytorch中各种乘法操作

各种乘法操作

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 torch

a = 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!

在这里插入图片描述

@

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值