Pyorch 里的矩阵乘法flyfish输入运算1D × 1Ddot product2D × 2Dmatrix multiply1D × 2Dvector × matrix2D × 1Dmatrix × vectorND × NDbatch matrix multiplytorch.mm / matmul / bmm 区别函数作用torch.mm只支持2D矩阵乘法torch.matmul通用乘法torch.bmmbatch矩阵乘法mm matrix multiplybmm batch matrix multiplymatmul matrix multiply矩阵乘法其实是行 × 列例如A [a1 a2 a3] B [b1 b2 b3]结果a1*b1 a2*b2 a3*b3就是dot product矩阵乘法 行向量和列向量的点乘集合dot product。概念是否矩阵乘法dot product矩阵乘法的基本单元cross product不是矩阵乘法A B等价 torch.matmultorch.mm2D矩阵torch.bmmbatch矩阵torch.matmul通用importtorch## 规则11D 1D → 点积标量 # 展示逐元素相乘 → 全部相加 → 最终结果 #print(*60)print(规则11维向量 1维向量 点积)a1torch.tensor([1,2,3])b1torch.tensor([4,5,6])print(f向量a1: {a1})print(f向量b1: {b1})# 手动计算过程print(【逐元素相乘 求和过程】)print(f1 × 4 4)print(f2 × 5 10)print(f3 × 6 18)print(f总和4 10 18 32)# 代码运算 res_ata1 b1 res_eqtorch.dot(a1,b1)print(f 运算结果: {res_at})print(f等价torch.dot结果: {res_eq})print(f结果形状: {res_at.shape}\n)## 规则22D 2D → 标准矩阵乘法 # 展示每行 × 每列 → 对应元素相乘 → 相加 → 矩阵元素 #print(*60)print(规则22维矩阵 2维矩阵 矩阵乘法)a2torch.tensor([[1,2],[3,4]])b2torch.tensor([[5,6],[7,8]])print(f矩阵a2:\n{a2})print(f矩阵b2:\n{b2})# 手动计算过程逐个元素计算print(【逐元素计算过程行 × 列 相乘后相加】)print(第1行第1列1×5 2×7 5 14 19)print(第1行第2列1×6 2×8 6 16 22)print(第2行第1列3×5 4×7 15 28 43)print(第2行第2列3×6 4×8 18 32 50)# 代码运算 res_ata2 b2 res_eqtorch.mm(a2,b2)print(f 运算结果:\n{res_at})print(f等价torch.mm结果:\n{res_eq})print(f结果形状: {res_at.shape}\n)## 规则31D 2D → 自动升维计算 # 展示1维升维 → 行×列相乘相加 → 降维输出 #print(*60)print(规则31维向量 2维矩阵 自动升维计算)a3torch.tensor([1,2])b3torch.tensor([[1,0],[0,1]])print(f向量a3: {a3})print(f矩阵b3:\n{b3})# 手动计算过程print(【计算过程a3升维为(1,2)再行×列相乘相加】)print(第1列1×1 2×0 1 0 1)print(第2列1×0 2×1 0 2 2)# 代码运算 res_ata3 b3print(f 运算结果: {res_at})print(f结果形状: {res_at.shape}\n)## 规则42D 1D → 矩阵-向量乘法 # 展示矩阵每行 × 向量 → 相乘相加 → 输出向量 #print(*60)print(规则42维矩阵 1维向量 矩阵-向量乘法)a4torch.tensor([[1,2],[3,4]])b4torch.tensor([1,1])print(f矩阵a4:\n{a4})print(f向量b4: {b4})# 手动计算过程print(【逐行 × 向量 相乘相加】)print(第1个结果1×1 2×1 1 2 3)print(第2个结果3×1 4×1 3 4 7)# 代码运算 res_ata4 b4print(f 运算结果: {res_at})print(f结果形状: {res_at.shape}\n)## 规则53D 3D → 批量矩阵乘法 # 展示每个批次独立计算矩阵乘法 #print(*60)print(规则53维批量矩阵 3维批量矩阵 批量矩阵乘法)a5torch.tensor([[[1,2],[3,4]],[[5,6],[7,8]]])b5torch.tensor([[[1,0],[0,1]],[[1,0],[0,1]]])print(f批量矩阵a5 形状: {a5.shape})print(f批量矩阵b5 形状: {b5.shape})# 手动计算过程分批次计算print(【第1个批次计算】)print(1×1 2×0 1 | 1×0 2×1 2)print(3×1 4×0 3 | 3×0 4×1 4)print(【第2个批次计算】)print(5×1 6×0 5 | 5×0 6×1 6)print(7×1 8×0 7 | 7×0 8×1 8)# 代码运算 res_ata5 b5 res_eqtorch.bmm(a5,b5)print(f 运算结果:\n{res_at})print(f等价torch.bmm结果:\n{res_eq})print(f结果形状: {res_at.shape})输出 规则11维向量 1维向量 点积 向量a1: tensor([1, 2, 3]) 向量b1: tensor([4, 5, 6]) 【逐元素相乘 求和过程】 1 × 4 4 2 × 5 10 3 × 6 18 总和4 10 18 32 运算结果: 32 等价torch.dot结果: 32 结果形状: torch.Size([]) 规则22维矩阵 2维矩阵 矩阵乘法 矩阵a2: tensor([[1, 2], [3, 4]]) 矩阵b2: tensor([[5, 6], [7, 8]]) 【逐元素计算过程行 × 列 相乘后相加】 第1行第1列1×5 2×7 5 14 19 第1行第2列1×6 2×8 6 16 22 第2行第1列3×5 4×7 15 28 43 第2行第2列3×6 4×8 18 32 50 运算结果: tensor([[19, 22], [43, 50]]) 等价torch.mm结果: tensor([[19, 22], [43, 50]]) 结果形状: torch.Size([2, 2]) 规则31维向量 2维矩阵 自动升维计算 向量a3: tensor([1, 2]) 矩阵b3: tensor([[1, 0], [0, 1]]) 【计算过程a3升维为(1,2)再行×列相乘相加】 第1列1×1 2×0 1 0 1 第2列1×0 2×1 0 2 2 运算结果: tensor([1, 2]) 结果形状: torch.Size([2]) 规则42维矩阵 1维向量 矩阵-向量乘法 矩阵a4: tensor([[1, 2], [3, 4]]) 向量b4: tensor([1, 1]) 【逐行 × 向量 相乘相加】 第1个结果1×1 2×1 1 2 3 第2个结果3×1 4×1 3 4 7 运算结果: tensor([3, 7]) 结果形状: torch.Size([2]) 规则53维批量矩阵 3维批量矩阵 批量矩阵乘法 批量矩阵a5 形状: torch.Size([2, 2, 2]) 批量矩阵b5 形状: torch.Size([2, 2, 2]) 【第1个批次计算】 1×1 2×0 1 | 1×0 2×1 2 3×1 4×0 3 | 3×0 4×1 4 【第2个批次计算】 5×1 6×0 5 | 5×0 6×1 6 7×1 8×0 7 | 7×0 8×1 8 运算结果: tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) 等价torch.bmm结果: tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) 结果形状: torch.Size([2, 2, 2])以上代码测试环境 PyTorch 版本: 2.8.0cu128单词英文短语词性含义关键词matrix multiply动词短语verb phrase执行矩阵相乘的动作「做乘法」matrix multiplication名词短语noun phrase矩阵乘法这一数学运算的概念「乘法运算本身」matrix product名词短语noun phrase矩阵乘法的计算结果「矩阵积」示例In PyTorch, semi-structured sparsity is implemented via a Tensor subclass. By subclassing, we can override__torch_dispatch__, allowing us to use faster sparse kernels when performingmatrix multiplication. We can also store the tensor in it’s compressed form inside the subclass to reduce memory overhead.Matrix productof two tensors.If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of thematrix multiply. After thematrix multiply, the prepended dimension is removed.