相关阅读Pytorch基础https://blog.csdn.net/weixin_45791458/category_12457644.html?spm1001.2014.3001.5482Pytorch中含有很多种张量乘法本文旨在帮助理解它们的不同。下面将分小节进行详细阐述包括torch.mul、torch.matmul、torch.mm、torch.bmm、torch.dot、torch.tensordot、*、。torch.multorch.mul函数用于执行两个张量的逐元素乘法又称哈德玛积返回张量的元素是两个张量对应位置元素相乘如果两个张量的阶数维度数不一样或维度的大小不一样会首先进行广播(broadcast)再进行乘法运算。下面是一个简单的例子展示了使用torch.mul函数计算哈德玛积的过程。# 例1 import torch A torch.arange(12).reshape(3, 4) B torch.arange(12).reshape(3, 4) # 两个矩阵形状相同 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 16, 25, 36, 49], [ 64, 81, 100, 121]]) torch.Size([3, 4])下面的例2展示了两个张量的阶数一样但维度的大小不一样会首先进行广播随后计算哈德玛积的过程。# 例2 import torch A torch.arange(3).reshape(3, 1) # 最后一维的大小为1需要广播 B torch.arange(12).reshape(3, 4) # 最后一维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([[0], [1], [2]]) torch.Size([3, 1]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 0, 0, 0], [ 4, 5, 6, 7], [16, 18, 20, 22]]) torch.Size([3, 4])下面的例3展示了两个张量的阶数不一样也会首先进行广播随后计算哈德玛积。# 例3 import torch A torch.arange(4).reshape(4) # 最后一维的大小为4但是一阶张量需要广播 B torch.arange(12).reshape(3, 4) # 最后一维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([0, 1, 2, 3]) torch.Size([4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 0, 5, 12, 21], [ 0, 9, 20, 33]]) torch.Size([3, 4])下面的例4展示了两个张量在计算哈德玛积前同时进行了阶数和维度两个方向的广播。# 例4 import torch A torch.tensor([2]) # 需要广播成与B张量形状相同 B torch.arange(12).reshape(3, 4) print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([2]) torch.Size([1]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 2, 4, 6], [ 8, 10, 12, 14], [16, 18, 20, 22]]) torch.Size([3, 4])torch.mvtorch.mv函数用于执行一个二阶张量矩阵和一阶张量向量的乘法这要求矩阵的最后一维的长度和向量的长度相等这个函数不支持广播。矩阵和向量的乘法会导致矩阵降维成向量下面是一个简单的例子。# 例5 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(4).reshape(4) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mv(A, B), torch.mv(A, B).shape) # 输出 tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) torch.Size([2, 4]) tensor([0, 1, 2, 3]) torch.Size([4]) tensor([14, 38]) torch.Size([2]) #输出降维torch.matmultorch.matmul函数可以用于执行两个张量最后两维的矩阵乘注意它也可以进行张量与向量一阶张量的乘法与向量之间的点积这里我们不讨论具体见Pytorch文档这对两个张量最后两维的大小有一定要求即左张量最后一维的大小等于右张量倒数第二维的大小如果两个张量的阶数不一样或维度的大小不一样除最后两维外会首先进行广播再进行矩阵乘法运算。下面是一个简单的例子展示了使用torch.matmul函数计算两个张量最后两维的矩阵乘的过程。# 例6 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) torch.Size([2, 4]) tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) torch.Size([4, 3]) tensor([[ 42, 48, 54], [114, 136, 158]]) torch.Size([2, 3])下面的例6展示了两个张量的阶数一样但维度的大小不一样会首先进行广播随后计算两个张量最后两维的矩阵乘。# 例7 import torch A torch.arange(12).reshape(1, 3, 4) # 第一维的大小为1需要广播 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) torch.Size([1, 3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [114, 136, 158], [186, 224, 262]], [[114, 120, 126], [378, 400, 422], [642, 680, 718]]]) torch.Size([2, 3, 3])下面的例8展示了两个张量的阶数不一样也会首先进行广播随后计算两个张量最后两维的矩阵乘。# 例8 import torch A torch.arange(12).reshape(3, 4) # 二阶张量需要广播 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [114, 136, 158], [186, 224, 262]], [[114, 120, 126], [378, 400, 422], [642, 680, 718]]]) torch.Size([2, 3, 3])下面的例9展示了两个张量在计算矩阵乘之前同时进行了阶数和维度两个方向的广播。# 例9 import torch A torch.arange(12).reshape(1, 3, 4) # 需要广播成除后两维外与B张量形状相同 B torch.arange(48).reshape(2, 2, 4, 3) print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) torch.Size([1, 3, 4]) tensor([[[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]], [[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[36, 37, 38], [39, 40, 41], [42, 43, 44], [45, 46, 47]]]]) torch.Size([2, 2, 4, 3]) tensor([[[[ 42, 48, 54], [ 114, 136, 158], [ 186, 224, 262]], [[ 114, 120, 126], [ 378, 400, 422], [ 642, 680, 718]]], [[[ 186, 192, 198], [ 642, 664, 686], [1098, 1136, 1174]], [[ 258, 264, 270], [ 906, 928, 950], [1554, 1592, 1630]]]]) torch.Size([2, 2, 3, 3])torch.mmtorch.mm函数也是用于执行矩阵乘的但是它只能对两个二阶张量即矩阵使用因此不支持广播。下面是一个简单的例子展示了使用torch.mm函数计算两个二阶张量的矩阵乘的过程。# 例10 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mm(A, B), torch.mm(A, B).shape)torch.bmmtorch.bmm函数与torch.mm函数类似但它不仅可以对计算两个二阶张量的矩阵乘也可以对两个三阶张量使用其中第一维是batch被称为维度它同样也不支持广播因此如果是两个三阶张量要求第一维大小相同。下面是一个简单的例子展示了使用torch.bmm函数计算两个三阶张量的矩阵乘的过程。# 例11 import torch A torch.arange(24).reshape(2, 3, 4) # 第一维的大小为2 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.bmm(A, B), torch.bmm(A, B).shape) 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) torch.Size([2, 3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [ 114, 136, 158], [ 186, 224, 262]], [[ 906, 960, 1014], [1170, 1240, 1310], [1434, 1520, 1606]]]) torch.Size([2, 3, 3])torch.dottorch.dot函数用于计算两个向量一阶张量的点积根据点积的定义点积结果应该是一个标量在Pytorch中也是如此。下面是一个简单的例子展示了使用torch.dot函数计算两个向量的点积的过程结果的形状显示其为一个标量零阶张量。torch.dot函数不支持广播因此这两个向量包含的元素数量必须相同。# 例12 import torch A torch.arange(4).reshape(4) # 一阶张量 B torch.arange(4).reshape(4) # 一阶张量 print(A, A.shape) print(B, B.shape) print(torch.dot(A, B), torch.dot(A, B).shape) 输出 tensor([0, 1, 2, 3]) torch.Size([4]) tensor([0, 1, 2, 3]) torch.Size([4]) tensor(14) torch.Size([]) # 结果为标量torch.tensordottorch.tensordot函数用于计算两个张量的矩阵点积阶数必须大于一在这里矩阵的点积是由向量点积的定义延伸而来。下面是一个简单的例子展示了使用torch.tensordot函数计算两个张量的点积的过程。torch.tensordot函数支持广播可以进行阶数和维度两个方向的广播。# 例13 import torch A torch.arange(4).reshape(2, 2) # 二阶张量 B torch.arange(4).reshape(2, 2) # 二阶张量 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor(14) torch.Size([]) # 结果为标量 A torch.arange(4).reshape(2, 2) # 二阶张量 B torch.arange(2).reshape(1, 2) # 第一维的大小为1需要广播 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([[0, 1]]) torch.Size([1, 2]) tensor(4) torch.Size([]) # 结果为标量 A torch.arange(8).reshape(2, 2, 2) # 三阶张量 B torch.arange(4).reshape(2, 2) # 二阶张量需要广播 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) torch.Size([2, 2, 2]) tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([14, 38]) torch.Size([2]) # 结果为一阶张量特别要注意最后一个例子即使张量的阶数大于二结果依旧是计算最后两维的张量点积分所以结果不是一个标量。*乘号直接使用乘号与使用torch.mul函数是类似的下面是一个简单的例子展示了直接使用乘号计算哈德玛积的过程它也拥有广播特性在此不重复演示。# 例14 import torch A torch.arange(12).reshape(3, 4) B torch.arange(12).reshape(3, 4) # 两个矩阵形状相同 print(A, A.shape) print(B, B.shape) print(A * B, (A * B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 16, 25, 36, 49], [ 64, 81, 100, 121]]) torch.Size([3, 4])是矩阵乘运算符与使用torch.matmul函数是类似的下面是一个简单的例子展示了直接使用计算矩阵乘的过程它也拥有广播特性在此不重复演示。# 例15 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print((A B), (A B).shape)
Pytorch基础:张量相关的乘法(torch.mul、torch.mv、torch.matmul、torch.mm、torch.bmm、torch.dot、torch.tensordot、*、@)
发布时间:2026/5/22 18:18:42
相关阅读Pytorch基础https://blog.csdn.net/weixin_45791458/category_12457644.html?spm1001.2014.3001.5482Pytorch中含有很多种张量乘法本文旨在帮助理解它们的不同。下面将分小节进行详细阐述包括torch.mul、torch.matmul、torch.mm、torch.bmm、torch.dot、torch.tensordot、*、。torch.multorch.mul函数用于执行两个张量的逐元素乘法又称哈德玛积返回张量的元素是两个张量对应位置元素相乘如果两个张量的阶数维度数不一样或维度的大小不一样会首先进行广播(broadcast)再进行乘法运算。下面是一个简单的例子展示了使用torch.mul函数计算哈德玛积的过程。# 例1 import torch A torch.arange(12).reshape(3, 4) B torch.arange(12).reshape(3, 4) # 两个矩阵形状相同 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 16, 25, 36, 49], [ 64, 81, 100, 121]]) torch.Size([3, 4])下面的例2展示了两个张量的阶数一样但维度的大小不一样会首先进行广播随后计算哈德玛积的过程。# 例2 import torch A torch.arange(3).reshape(3, 1) # 最后一维的大小为1需要广播 B torch.arange(12).reshape(3, 4) # 最后一维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([[0], [1], [2]]) torch.Size([3, 1]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 0, 0, 0], [ 4, 5, 6, 7], [16, 18, 20, 22]]) torch.Size([3, 4])下面的例3展示了两个张量的阶数不一样也会首先进行广播随后计算哈德玛积。# 例3 import torch A torch.arange(4).reshape(4) # 最后一维的大小为4但是一阶张量需要广播 B torch.arange(12).reshape(3, 4) # 最后一维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([0, 1, 2, 3]) torch.Size([4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 0, 5, 12, 21], [ 0, 9, 20, 33]]) torch.Size([3, 4])下面的例4展示了两个张量在计算哈德玛积前同时进行了阶数和维度两个方向的广播。# 例4 import torch A torch.tensor([2]) # 需要广播成与B张量形状相同 B torch.arange(12).reshape(3, 4) print(A, A.shape) print(B, B.shape) print(torch.mul(A, B), torch.mul(A, B).shape) # 输出 tensor([2]) torch.Size([1]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 2, 4, 6], [ 8, 10, 12, 14], [16, 18, 20, 22]]) torch.Size([3, 4])torch.mvtorch.mv函数用于执行一个二阶张量矩阵和一阶张量向量的乘法这要求矩阵的最后一维的长度和向量的长度相等这个函数不支持广播。矩阵和向量的乘法会导致矩阵降维成向量下面是一个简单的例子。# 例5 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(4).reshape(4) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mv(A, B), torch.mv(A, B).shape) # 输出 tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) torch.Size([2, 4]) tensor([0, 1, 2, 3]) torch.Size([4]) tensor([14, 38]) torch.Size([2]) #输出降维torch.matmultorch.matmul函数可以用于执行两个张量最后两维的矩阵乘注意它也可以进行张量与向量一阶张量的乘法与向量之间的点积这里我们不讨论具体见Pytorch文档这对两个张量最后两维的大小有一定要求即左张量最后一维的大小等于右张量倒数第二维的大小如果两个张量的阶数不一样或维度的大小不一样除最后两维外会首先进行广播再进行矩阵乘法运算。下面是一个简单的例子展示了使用torch.matmul函数计算两个张量最后两维的矩阵乘的过程。# 例6 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) torch.Size([2, 4]) tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) torch.Size([4, 3]) tensor([[ 42, 48, 54], [114, 136, 158]]) torch.Size([2, 3])下面的例6展示了两个张量的阶数一样但维度的大小不一样会首先进行广播随后计算两个张量最后两维的矩阵乘。# 例7 import torch A torch.arange(12).reshape(1, 3, 4) # 第一维的大小为1需要广播 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) torch.Size([1, 3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [114, 136, 158], [186, 224, 262]], [[114, 120, 126], [378, 400, 422], [642, 680, 718]]]) torch.Size([2, 3, 3])下面的例8展示了两个张量的阶数不一样也会首先进行广播随后计算两个张量最后两维的矩阵乘。# 例8 import torch A torch.arange(12).reshape(3, 4) # 二阶张量需要广播 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [114, 136, 158], [186, 224, 262]], [[114, 120, 126], [378, 400, 422], [642, 680, 718]]]) torch.Size([2, 3, 3])下面的例9展示了两个张量在计算矩阵乘之前同时进行了阶数和维度两个方向的广播。# 例9 import torch A torch.arange(12).reshape(1, 3, 4) # 需要广播成除后两维外与B张量形状相同 B torch.arange(48).reshape(2, 2, 4, 3) print(A, A.shape) print(B, B.shape) print(torch.matmul(A, B), torch.matmul(A, B).shape) # 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) torch.Size([1, 3, 4]) tensor([[[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]], [[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[36, 37, 38], [39, 40, 41], [42, 43, 44], [45, 46, 47]]]]) torch.Size([2, 2, 4, 3]) tensor([[[[ 42, 48, 54], [ 114, 136, 158], [ 186, 224, 262]], [[ 114, 120, 126], [ 378, 400, 422], [ 642, 680, 718]]], [[[ 186, 192, 198], [ 642, 664, 686], [1098, 1136, 1174]], [[ 258, 264, 270], [ 906, 928, 950], [1554, 1592, 1630]]]]) torch.Size([2, 2, 3, 3])torch.mmtorch.mm函数也是用于执行矩阵乘的但是它只能对两个二阶张量即矩阵使用因此不支持广播。下面是一个简单的例子展示了使用torch.mm函数计算两个二阶张量的矩阵乘的过程。# 例10 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print(torch.mm(A, B), torch.mm(A, B).shape)torch.bmmtorch.bmm函数与torch.mm函数类似但它不仅可以对计算两个二阶张量的矩阵乘也可以对两个三阶张量使用其中第一维是batch被称为维度它同样也不支持广播因此如果是两个三阶张量要求第一维大小相同。下面是一个简单的例子展示了使用torch.bmm函数计算两个三阶张量的矩阵乘的过程。# 例11 import torch A torch.arange(24).reshape(2, 3, 4) # 第一维的大小为2 B torch.arange(24).reshape(2, 4, 3) # 第一维的大小为2 print(A, A.shape) print(B, B.shape) print(torch.bmm(A, B), torch.bmm(A, B).shape) 输出 tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) torch.Size([2, 3, 4]) tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) torch.Size([2, 4, 3]) tensor([[[ 42, 48, 54], [ 114, 136, 158], [ 186, 224, 262]], [[ 906, 960, 1014], [1170, 1240, 1310], [1434, 1520, 1606]]]) torch.Size([2, 3, 3])torch.dottorch.dot函数用于计算两个向量一阶张量的点积根据点积的定义点积结果应该是一个标量在Pytorch中也是如此。下面是一个简单的例子展示了使用torch.dot函数计算两个向量的点积的过程结果的形状显示其为一个标量零阶张量。torch.dot函数不支持广播因此这两个向量包含的元素数量必须相同。# 例12 import torch A torch.arange(4).reshape(4) # 一阶张量 B torch.arange(4).reshape(4) # 一阶张量 print(A, A.shape) print(B, B.shape) print(torch.dot(A, B), torch.dot(A, B).shape) 输出 tensor([0, 1, 2, 3]) torch.Size([4]) tensor([0, 1, 2, 3]) torch.Size([4]) tensor(14) torch.Size([]) # 结果为标量torch.tensordottorch.tensordot函数用于计算两个张量的矩阵点积阶数必须大于一在这里矩阵的点积是由向量点积的定义延伸而来。下面是一个简单的例子展示了使用torch.tensordot函数计算两个张量的点积的过程。torch.tensordot函数支持广播可以进行阶数和维度两个方向的广播。# 例13 import torch A torch.arange(4).reshape(2, 2) # 二阶张量 B torch.arange(4).reshape(2, 2) # 二阶张量 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor(14) torch.Size([]) # 结果为标量 A torch.arange(4).reshape(2, 2) # 二阶张量 B torch.arange(2).reshape(1, 2) # 第一维的大小为1需要广播 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([[0, 1]]) torch.Size([1, 2]) tensor(4) torch.Size([]) # 结果为标量 A torch.arange(8).reshape(2, 2, 2) # 三阶张量 B torch.arange(4).reshape(2, 2) # 二阶张量需要广播 print(A, A.shape) print(B, B.shape) print(torch.tensordot(A, B), torch.tensordot(A, B).shape) 输出 tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) torch.Size([2, 2, 2]) tensor([[0, 1], [2, 3]]) torch.Size([2, 2]) tensor([14, 38]) torch.Size([2]) # 结果为一阶张量特别要注意最后一个例子即使张量的阶数大于二结果依旧是计算最后两维的张量点积分所以结果不是一个标量。*乘号直接使用乘号与使用torch.mul函数是类似的下面是一个简单的例子展示了直接使用乘号计算哈德玛积的过程它也拥有广播特性在此不重复演示。# 例14 import torch A torch.arange(12).reshape(3, 4) B torch.arange(12).reshape(3, 4) # 两个矩阵形状相同 print(A, A.shape) print(B, B.shape) print(A * B, (A * B).shape) # 输出 tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) torch.Size([3, 4]) tensor([[ 0, 1, 4, 9], [ 16, 25, 36, 49], [ 64, 81, 100, 121]]) torch.Size([3, 4])是矩阵乘运算符与使用torch.matmul函数是类似的下面是一个简单的例子展示了直接使用计算矩阵乘的过程它也拥有广播特性在此不重复演示。# 例15 import torch A torch.arange(8).reshape(2, 4) # 最后一维的大小为4 B torch.arange(12).reshape(4, 3) # 倒数第二维的大小为4 print(A, A.shape) print(B, B.shape) print((A B), (A B).shape)