Pytorch基础:Torch.mul、Torch.mm与Torch.matmul的异同

编程入门 行业动态 更新时间:2024-10-09 00:47:20

Pytorch基础:Torch.mul、Torch.mm与Torch.matmul的<a href=https://www.elefans.com/category/jswz/34/1767543.html style=异同"/>

Pytorch基础:Torch.mul、Torch.mm与Torch.matmul的异同

Pytorch基础:Torch.mul、Torch.mm与Torch.matmul的异同

Torch.mul

torch.mul(input, other, ***, out=None) → Tensor

将输入的每个元素与另一个标量相乘,返回一个新的张量。
o u t i = o t h e r × i n p u t i out_i = other \times input_i outi​=other×inputi​
input是张量, other将会乘每个张量元素。输出是张量。

如果输入是FloatTensor或DoubleTensor类型,other应该是实数,否则应该是整数

示例

>>> a = torch.randn(3)
>>> a
tensor([ 0.2015, -0.4255,  2.6087])
>>> torch.mul(a, 100)
tensor([  20.1494,  -42.5491,  260.8663])

torch.mul(input, other, ***, out=None) → Tensor

张量input的每个元素必须乘张量other的每个元素, 结果会返回一个张量

inputother必须是符合广播机制的
o u t i = i n p u t i × o t h e r i out_i = input_i \times other_i outi​=inputi​×otheri​
input 和other都是张量。返回也是张量

示例

>>> a = torch.randn(4, 1)
>>> a
tensor([[ 1.1207],[-0.3137],[ 0.0700],[ 0.8378]])
>>> b = torch.randn(1, 4)
>>> b
tensor([[ 0.5146,  0.1216, -0.5244,  2.2382]])
>>> torch.mul(a, b)
tensor([[ 0.5767,  0.1363, -0.5877,  2.5083],[-0.1614, -0.0382,  0.1645, -0.7021],[ 0.0360,  0.0085, -0.0367,  0.1567],[ 0.4312,  0.1019, -0.4394,  1.8753]])

Torch.mm

torch.mm(input, mat2, ***, out=None) → Tensor

执行矩阵输入和mat2的矩阵乘法

如果input是 ( n × m ) (n \times m) (n×m)的张量, mat2是 ( m × p ) (m \times p) (m×p) 的张量, 输出将会是 ( n × p ) (n \times p) (n×p)的张量

这个函数没有广播机制, 如果要使用广播机制,需要torch.matmul()

支持strided和稀疏的二维张量作为输入,autograd with respect to strided inputs.

该操作符支持TensorFloat32。

>>> mat1 = torch.randn(2, 3)
>>> mat2 = torch.randn(3, 3)
>>> torch.mm(mat1, mat2)
tensor([[ 0.4851,  0.5037, -0.3633],[-0.0760, -3.6705,  2.4784]])

input是第一个张量矩阵, mat2是第二个张量矩阵。output是张量

Torch.matmul

torch.matmul(input, other, ***, out=None) → Tensor

两个张量的矩阵乘积。

其行为取决于张量的维数如下:

  • 如果两个张量都是一维的,则返回点积(标量)。

  • 如果两个参数都是二维的,则返回矩阵-矩阵乘积。

  • 如果第一个参数是一维的,第二个参数是二维的,为了使矩阵相乘,在它的维数前面加了一个1。在矩阵相乘之后,附加的维度被删除。

  • 如果第一个参数是二维的,第二个参数是一维的,则返回矩阵-向量乘积。

  • 如果两个参数至少是一维的,且至少一个参数是N维的(其中N > 2),则返回一个批处理矩阵乘法。如果第一个参数是一维的,则在其维数前加上1,以便批处理矩阵相乘,然后删除。如果第二个参数是一维的,则为批处理矩阵倍数的目的,将在其维上追加一个1,然后删除它。

  • 非矩阵(即批处理)维度是广播的(因此必须是可广播的)。

    示例:如果input是 ( j × 1 × n × n ) (j\times 1 \times n \times n) (j×1×n×n)的张量 乘另外一个张量other ( k × n × n ) (k \times n \times n) (k×n×n), 那么输出将会是 ( j × k × n × n ) (j \times k \times n \times n) (j×k×n×n)

    要注意的是,在确定输入是否可广播时,广播逻辑只查看批处理维,而不查看矩阵维。

    例如:input是张量 ( j × 1 × n × m ) (j \times 1 \times n \times m) (j×1×n×m)而other是张量 ( k × m × p ) (k \times m\times p) (k×m×p),这些输入对于广播是有效的,即使最后两个维度(即矩阵维度)是不同的。out将会是张量 ( j × k × n × p ) (j \times k \times n \times p) (j×k×n×p)。

    支持tensorfloat32

  >>> # vector x vector>>> tensor1 = torch.randn(3)>>> tensor2 = torch.randn(3)>>> torch.matmul(tensor1, tensor2).size()torch.Size([])>>> # matrix x vector>>> tensor1 = torch.randn(3, 4)>>> tensor2 = torch.randn(4)>>> torch.matmul(tensor1, tensor2).size()torch.Size([3])>>> # batched matrix x broadcasted vector>>> tensor1 = torch.randn(10, 3, 4)>>> tensor2 = torch.randn(4)>>> torch.matmul(tensor1, tensor2).size()torch.Size([10, 3])>>> # batched matrix x batched matrix>>> tensor1 = torch.randn(10, 3, 4)>>> tensor2 = torch.randn(10, 4, 5)>>> torch.matmul(tensor1, tensor2).size()torch.Size([10, 3, 5])>>> # batched matrix x broadcasted matrix>>> tensor1 = torch.randn(10, 3, 4)>>> tensor2 = torch.randn(4, 5)>>> torch.matmul(tensor1, tensor2).size()torch.Size([10, 3, 5])

更多推荐

Pytorch基础:Torch.mul、Torch.mm与Torch.matmul的异同

本文发布于:2024-03-04 11:00:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1709085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异同   基础   Torch   Pytorch   matmul

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!