
PyTorch乘法
本文最后更新于 2024-06-07,文章内容可能已经过时。
笔记:torch 乘法总结
一、乘号(*) 和 torch.mul()
element-wise 即对应元素相乘
例子:
echo "H HJ JKL!"
pwd
>>> a = torch.randn(2,3)
>>> b = torch.randn(2,1)
>>> res = a * b
>>> res
tensor([[-0.9672, -0.1052, 0.1392],
[-0.8552, 0.8967, -0.6433]])
特别地,如果是(2 × 1 × 3) 和 (2 × 4 × 3)这种情况,也可以相乘,结果是((2 × 4 × 3)。相当于用前一个tensor沿着后一个tensor的第1维度expand(broadcast机制)。
例子:
>>> a = torch.randn(2,3)
>>> b = torch.randn(2,1)
>>> res1 = a * b
>>> res1
tensor([[ 0.9199, 0.4053, 0.0789],
[ 2.1330, -0.5653, 0.4760]])
>>> res2 = torch.mul(a,b)
>>> res2
tensor([[ 0.9199, 0.4053, 0.0789],
[ 2.1330, -0.5653, 0.4760]]) # torch.mul() 和 * 效果相同
>>> res3 = a * b.expand(2,3)
>>> res3
tensor([[ 0.9199, 0.4053, 0.0789], # 两个tensor维度不一致时,
[ 2.1330, -0.5653, 0.4760]]) # 会自动进行expand
二、torch.mm() 与 torch.bmm()
矩阵乘法。
torch.mm(mat1, mat2) 对 mat1 和 mat2 进行矩阵乘法,要求输入只能是2维矩阵。
torch.bmm(mat1, mat2) 专门进行batch形式的矩阵乘法。要求(1)输入只能是3维矩阵;(2)第0维度相同。
三、torch.matmul()
矩阵乘法,支持broadcast。
下面是torch.matmul(mat1, mat2)的适用情形:
若 mat1, mat2 都是一维向量,则结果返回的是一个标量。
若mat1和mat2是二维矩阵,返回矩阵乘积(前提是满足二维矩阵乘法的条件)
若mat1是一位矩阵(shape:[n]),mat2是二维矩阵(shape:[n × d]),则会把mat1添加一个维度,变成[1×n],然后执行二位矩阵乘法。
若mat1,mat2只要有一个大于二维度,则执行Batch矩阵乘法:把输入tensor的最后两维视为矩阵维度,其他高维度视为batch维度
四、@运算符
在python3.5以上的版本,引入了一个新的运算符 '@'。
pytorch中, @表示两个tensor之间做矩阵乘,@和torch.mm(a, b)
等同,即线性代数中标准的两维矩阵乘法。
@
表示常规的数学上定义的矩阵相乘;*
表示两个矩阵对应位置处的两个元素相乘。
实现方法是:pytorch的 Tensor类实现了python内置的 matmul方法。
>>> x1 = torch.randn(3,4)
>>> x2 = torch.randn(4,3)
>>> x1 @ x2
tensor([[ 0.8704, 0.5328, -1.4012],
[-4.3911, -3.2537, -0.8944],
[-0.5218, 4.2137, -3.7538]])
>>> torch.mm(x1, x2)
tensor([[ 0.8704, 0.5328, -1.4012],
[-4.3911, -3.2537, -0.8944],
[-0.5218, 4.2137, -3.7538]])
- 感谢你赐予我前进的力量