Pytorch 数学计算

1. 加法运算

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

将标量 other 与input输入的每个元素相加并返回一个新的结果张量。

out=input+other

如果 input 是 Fl​​oatTensor 或 DoubleTensor 类型,则 other 必须是实数,否则应该是整数。

参数

  • input (Tensor) – the input tensor.

  • other (Number) – the number to be added to each element of input

关键字参数

  • out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.randn(4)
>>> a
tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
>>> torch.add(a, 20)
tensor([ 20.0202,  21.0985,  21.3506,  19.3944])

2. 减法运算

torch.sub(input, other, *, alpha=1, out=None) → Tensor

从输入中减去按 alpha 缩放的 other。

out_i=input_i-alpha*other_i

 支持广播(broadcasting)到相同的shape、类型转换以及整数、浮点数和复数输入。

 参数

  •  input (Tensor) – the input tensor.
  • other (Tensor or Scalar) – the tensor or scalar to subtract from input

关键字参数

  • alpha (Scalar) – the scalar multiplier for other

  • out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.tensor((1, 2))
>>> b = torch.tensor((0, 1))
>>> torch.sub(a, b, alpha=2)
tensor([1, 0])

3. 哈达玛积(element wise,对应元素相乘)

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

将输入input的每个元素与标量other元素相乘,并返回一个新的结果张量。

out_{i} = other*input_i

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

参数

  •  input (Tensor) – the input tensor.
  • other (Number) – the number to be multiplied to each element of input

关键字参数

  • out (Tensor, optional) – the output tensor.

举例:

>>> 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 的相应元素。返回运算结果的张量。

Input和other的shape必须是可以自动扩张的(broadcastable)。

参数

  •  input (Tensor) – 第一个被乘数张量
    .
  • other (Tensor) – 第二个被乘数张量

关键字参数

  • out (Tensor, optional) – 输出张量.

举例:

>>> 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]])

4. 除法运算

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

将输入input的每个元素除以other的对应元素。

out_{i}=\frac{input_{i}}{other_{i}}

支持广播(broadcasting)到相同的shape、类型转换以及整数、浮点数和复数输入。

参数

  •  input (Tensor) – 被除数
    .
  • other (Tensor or Number) – 除数

关键字参数

  • rounding_mode (str, optional) – 应用于结果的舍入类型
1. None 默认行为。不执行舍入,如果输入和其他都是整数类型,则将输入转化为默认标量类型。相当于      
   Python 中的真正除法(/ 运算符)和 NumPy 的 np.true_divide  
2. “trunc” - 将除法结果向零舍入。相当于 C 风格的整数除法。
3. “floor” - 将除法结果向下舍入。相当于 Python 中的楼层除法(// 运算符)和 NumPy 的 np.floor_divide。
  • out (Tensor, optional) – 输出张量.

举例:

>>> x = torch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(x, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])

>>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
...                   [ 0.1815, -1.0111,  0.9805, -1.5923],
...                   [ 0.1062,  1.4581,  0.7759, -1.2344],
...                   [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2639],
        [ 0.2260, -3.4509, -1.2086,  6.8990],
        [ 0.1322,  4.9764, -0.9564,  5.3484],
        [-0.2278, -0.1068, -1.4678,  6.3938]])

>>> torch.div(a, b, rounding_mode='trunc')
tensor([[-0., -6.,  0.,  1.],
        [ 0., -3., -1.,  6.],
        [ 0.,  4., -0.,  5.],
        [-0., -0., -1.,  6.]])

>>> torch.div(a, b, rounding_mode='floor')
tensor([[-1., -7.,  0.,  1.],
        [ 0., -4., -2.,  6.],
        [ 0.,  4., -1.,  5.],
        [-1., -1., -2.,  6.]])

5. 矩阵乘法

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

两个张量的矩阵乘积,行为取决于张量的维度,如下所示:

  • 如果两个张量都是一维的,则返回点积(对应元素相乘再累加,从而得到一个标量)这种形况需要两个一维矩阵的size是一致的。
  • 如果两个参数都是二维的,则返回矩阵-矩阵乘积。
  • 如果第一个参数是一维的,而第二个参数是二维的,则为了矩阵乘法的目的,在其维度前面加上 1。在矩阵乘法之后,前置维度被删除。
  • 如果第一个参数是二维的,第二个参数是一维的,则返回矩阵向量乘积。
  • 如果两个参数都至少是一维的并且至少一个参数是 N 维的(其中 N > 2),则返回批处理(batched)矩阵乘法。如果第一个参数是一维的,为了批量(batched)矩阵乘法的目的,在它的维度前面加上 1 并在之后删除。如果第二个参数是一维的,则为了批处理(batched)矩阵乘法的目的,将 1 附加到其维度并在之后删除。非矩阵(即批量)维度被广播(因此必须是可广播的)。例如,如果输入是一个 (j\times1\times n\times n)张量,其他是一个 (k\times n\times n) 张量,输出将是(j\times k\times n\times n)张量。请注意,广播逻辑在确定输入是否可广播时仅查看批次维度,而不是矩阵维度。例如,如果输入是一个 (j×1×n×m)(j \times 1 \times n \times m)(j×1×n×m) 张量,其他是一个 (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。

参数

  • input (Tensor) – 第一个被乘数张量

  • other (Tensor) – 第二个被乘数张量

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> # 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])

6. 幂运算

torch.pow(input, exponent, *, out=None) → Tensor

用指数exponent计算输入input中每​​个元素的幂,并返回一个带有计算结果的张量。指数可以是单个浮点数或具有与输入相同数量的元素的张量。
  • 当指数是标量值时,应用的操作是:

out_i=x_{i}^{exponent}

  • 当指数是张量时,应用的操作是:

out_i=x_{i}^{exponent_i}

 当指数是张量时,输入和指数的形状必须是可自动扩展(broadcastable)的。

参数

  • input (Tensor) – input 张量.

  • exponent (float or tensor) – 指数值

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.randn(4)
>>> a
tensor([ 0.4331,  1.2475,  0.6834, -0.2791])
>>> torch.pow(a, 2)
tensor([ 0.1875,  1.5561,  0.4670,  0.0779])
>>> exp = torch.arange(1., 5.)

>>> a = torch.arange(1., 5.)
>>> a
tensor([ 1.,  2.,  3.,  4.])
>>> exp
tensor([ 1.,  2.,  3.,  4.])
>>> torch.pow(a, exp)
tensor([   1.,    4.,   27.,  256.])

torch.pow(self, exponent, *, out=None) → Tensor

self 是标量浮点值,指数exponent是张量。返回的张量与指数exponent形状相同

应用的操作是:

out_i={self}^{exponent_{i}}

参数

  • self (float) – self 是幂运算的标量基值

  • exponent (Tensor) – 指数 exponent 张量

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> exp = torch.arange(1., 5.)
>>> exp
tensor([1., 2., 3., 4.])
>>> base = 2
>>> torch.pow(base, exp)
tensor([  2.,   4.,   8.,  16.])

7. 开方运算

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

返回具有输入input元素平方根的新张量。

out_i=\sqrt{input_i}

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.randn(4)
>>> a
tensor([-2.0755,  1.0226,  0.0831,  0.4806])
>>> torch.sqrt(a)
tensor([    nan,  1.0112,  0.2883,  0.6933])

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

返回一个新的张量,它是输入的每个元素的平方根的倒数。

out_i=\frac{1}{\sqrt{input_i}}

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.randn(4)
>>> a
tensor([-0.0370,  0.2970,  1.5420, -0.9105])
>>> torch.rsqrt(a)
tensor([    nan,  1.8351,  0.8053,     nan])

8.指数与对数运算

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

返回具有输入张量 input 元素的指数的新张量。

y_i=e^{x_i}

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> torch.exp(torch.tensor([0, math.log(2.)]))
tensor([ 1.,  2.])

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

返回具有输入元素的自然对数的新张量。

y_i=log_{e}(x_i)

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

import torch
 
a = torch.exp(torch.ones(2, 2))  # 得到2*2的全是e的Tensor
print(a)
print(torch.log(a))  # 取自然对数

输出结果:

tensor([[2.7183, 2.7183],
        [2.7183, 2.7183]])
tensor([[1., 1.],
        [1., 1.]])

9.近似值运算

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

返回一个新的张量,其中输入的每个元素都四舍五入为最接近的整数。

参数

  • input (Tensor) – input 张量.

关键字参数

out (Tensor, optional) – the output tensor.

举例:

>>> a = torch.randn(4)
>>> a
tensor([ 0.9920,  0.6077,  0.9734, -1.0362])
>>> torch.round(a)
tensor([ 1.,  1.,  1., -1.])

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值