Pytorch学习之torch----数学操作(一)

1. torch.abs(input, out=None)

说明:计算输入张量的每个元素绝对值

参数

  • input(Tensor) -- 输入张量
  • out(可选) -- 输出
>>> import torch
>>> torch.abs(torch.FloatTensor([-1, -2, 3]))
tensor([1., 2., 3.])

2. torch.acos(input, out=None)

说明:返回一个新张量,包含输入张量每个元素的反余弦。

参数

  • input(Tensor) -- 输入张量
  • out(Tensor,可选) -- 结果张量
>>> a = torch.randn(4)
>>> a
tensor([ 2.5609,  0.1132, -0.5844,  0.0257])
>>> torch.acos(a)
tensor([   nan, 1.4573, 2.1950, 1.5451])

3. torch.add(input, value, out=None)

说明:对输入张量input逐元素加上标量值value,并返回到一个新的张量out。即out = tensor + value。

参数

  • input(Tensor) -- 输入张量
  • value(Number) -- 添加到输入每个元素的数
  • out(Tensor,可选) -- 结果张量
>>> a = torch.randn(4)
>>> a
tensor([ 0.6450,  0.8491, -0.2552, -0.1404])
>>> torch.add(a, 20)
tensor([20.6450, 20.8491, 19.7448, 19.8596])
>>> torch.add(a, 9.8)
tensor([10.4450, 10.6491,  9.5448,  9.6596])

4. torch.add(input, value, other, out=None)

说明:other张量的每个元素乘以以标量值value,并加到input张量上,返回结果到输出张量out。即out = input + (other * value)。形状需要匹配。

参数

  • input(Tensor) -- 第一个输入张量
  • value(Number) -- 用于第二个张量的尺寸因子
  • other(Tensor) -- 第二个输入张量
  • out(Tensor,可选) -- 结果张量
>>> a = torch.randn(4)
>>> a
tensor([-0.0692, -1.1224, -1.6475,  2.0617])
>>> b = torch.randn(4)
>>> b
tensor([-0.5094,  0.4380, -0.3469, -0.8634])
>>> torch.add(a, 10, b)
tensor([-5.1634,  3.2573, -5.1164, -6.5726])

5. torch.addcdiv(tensor, value=1, tensor1, tensor2, out=None)

说明:用tensor2对tensor1逐元素相除,然后乘以标量值value,并加到tensor。张量的形状需要匹配。我实验过形状不匹配时,会报错。

参数

  • tensor(Tensor) -- 张量,对tensor1./tensor2进行相加
  • value(number) -- 标量,对tensor1./tensor2进行相除
  • tensor1(Tensor) -- 张量,作为被除数(分子)
  • tensor2(Tensor) -- 张量,作为除数(分母)
  • out(Tensor,可选) -- 输出张量
>>> t = torch.randn(1, 6)
>>> t1 = torch.randn(1, 6)
>>> t2 = torch.randn(1, 6)
>>> torch.addcdiv(t, 0.1, t1, t2)
tensor([[ 0.8583,  2.1763, -0.5981, -1.2156, -0.3442,  1.3631]])

6. torch.addcmul(tensor, value=1, tensor1, tensor2, out=None)

说明:用tensor2对tensor1逐元素相乘,并对结果乘以标量值value然后加到tensor。张量的形状需要匹配的。

参数

  • tensor(Tensor) -- 张量,对tensor1*tensor2进行相加
  • value(Number) -- 标量,对tensor1,tensor2进行相乘
  • tensor1(Tensor) -- 张量,作为乘子1
  • tensor2(Tensor) -- 张量,作为乘子2
  • out(Tensor, 可选) -- 输出张量
>>> t = torch.randn(1, 6)
>>> t1 = torch.randn(1, 6)
>>> t2 = torch.randn(1, 6)
>>> torch.addcmul(t, 0.1, t1, t2)
tensor([[ 1.0198,  1.0696, -0.2039, -0.8555, -0.9452, -0.2066]])

7. torch.asin(input, out=None)

说明:返回一个张量,包含输入input张量每个元素的反正弦函数

参数

  • tensor(Tensor) -- 输入张量
  • out(Tensor,可选) -- 输出张量
>>> a = torch.randn(4)
>>> a
tensor([ 1.0829,  0.3976, -0.7801,  1.1426])
>>> torch.asin(a)
tensor([    nan,  0.4089, -0.8948,     nan])

8. torch.atan(input, out=None)

说明:返回一个新张量,包含输入input张量每个元素的反正切函数。

参数

  • tensor(Tensor) -- 输入张量
  • out(Tensor, optional) -- 输出张量
>>> a = torch.randn(4)
>>> torch.atan(a)
tensor([ 0.4967, -0.6854,  0.8361,  1.0014])

9. torch.atan2(input1, input2, out=None)

说明:返回一个新张量,包含两个输入张量input1和input2的反正切函数

参数

  • input1(Tensor) -- 第一个输入张量
  • input2(Tensor) -- 第二个输入张量
  • out(Tensor, 可选) -- 输出张量
>>> a = torch.randn(4)
>>> b = torch.randn(4)
>>> torch.atan2(a, b)
tensor([ 1.8779, -2.7628, -1.9384, -1.4760])

10. torch.ceil(input, out=None)

说明:天井函数,对输入input张量每个元素向上取整,即取不小于每个元素的最小整数。并返回结果到输出

参数

  • input(Tensor) -- 输入张量
  • out(Tensor, 可选) -- 输出张量
>>> a = torch.randn(4)
>>> a
tensor([-0.7592, -0.8455,  1.2844,  1.4402])
>>> torch.ceil(a)
tensor([-0., -0., 2., 2.])

11. torch.clamp(input, min, max, out=None)

说明:将输入input张量每个元素的夹紧到区间[min,max],并返回结果到一个新张量。对小于min的值,令其等于min,大于max的值等于max。其他值仍等于原值。

参数

  • input(Tensor) -- 输入张量
  • min(Numberi) -- 限制范围下限
  • max(Number) -- 限制范围上限
  • out(Tensor, 可选) -- 输出张量
>>> a = torch.randn(4)
>>> a
tensor([-1.8579,  0.4232, -0.1166,  1.1509])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.4232, -0.1166,  0.5000])

12. torch.clamp(input, *, min, out=None) 

说明:将输入input张量每个元素的限制都不小于min,并返回结果到一个新张量。如果输入是FloatTensor或DoubleTensor类型,则参数min必须为实数,否则须为整数。经过实验得知。无论输入类型,皆可。

参数

  • input(Tenosr) -- 输入张量
  • min(Number) -- 限制范围下限
  • out(Tenosr,可选) -- 输出张量
>>> a = torch.randn(6)
>>> a
tensor([ 1.6551,  0.6760,  2.2335, -0.7242, -1.0403,  0.7974])
>>> torch.clamp(a, min=0.5)
tensor([1.6551, 0.6760, 2.2335, 0.5000, 0.5000, 0.7974])

13. torch.clamp(input, *, max, out=None)

说明:将输入input张量每个元素的限制到不大于max,并返回结果到一个新张量。

参数

  • input(Tensor) -- 输入张量
  • max(Number) -- 限制范围上限
  • out(Tenosr, 可选) -- 输出张量
>>> a = torch.randn(5)
>>> a
tensor([-0.9049, -2.5959, -0.2436,  0.2380, -0.5851])
>>> torch.clamp(a, max=0.5)
tensor([-0.9049, -2.5959, -0.2436,  0.2380, -0.5851])

 14. torch.cos(input, out=None)

说明:返回一个新张量,包含输入input张量每个元素的余弦。

参数

  • input(Tensor) -- 输入张量
  • out(Tenosr,可选) -- 输出张量
>>> a = torch.randn(4)
>>> a
tensor([-0.5251,  1.2806, -0.0669, -0.2035])
>>> torch.cos(a)
tensor([0.8653, 0.2861, 0.9978, 0.9794])

15. torch.cosh(input, out=None)

说明:返回一个新张量,包含输入input张量每个元素的双曲余弦

参数

  • input(Tenosr) -- 输入张量
  • out(Tensor, 可选) -- 输出张量
>>> a = torch.randn(4)
>>> a
tensor([ 0.6999,  0.4897, -1.0476, -0.4528])
>>> torch.cosh(a)
tensor([1.2551, 1.1223, 1.6008, 1.1043])

16. torch.div(input, value, out=None)

说明:将input逐元素除以标量值value,并返回结果到输出张量out。

参数

  • input(Tenosr) -- 输入张量
  • value(Number) -- 除数
  • out(Tenosr,可选) -- 输出张量
>>> a = torch.randn(5)
>>> a
tensor([ 1.1755, -0.7143,  2.1232,  0.4559, -1.3207])
>>> torch.div(a, 0.5)
tensor([ 2.3510, -1.4286,  4.2465,  0.9119, -2.6414])

17. torch.div(input, other, out=None)

说明:两张量input和other逐元素相除,并将结果返回到输出。

参数

  • input(Tensor) -- 张量(分子)
  • other(Tensor) -- 张量(分母)
  • out(Tensor, 可选) -- 输出张量
>>> a = torch.randn(3, 3)
>>> a
tensor([[ 0.6808,  1.1017, -1.1122],
        [ 0.9604, -0.1047, -0.9544],
        [-0.2481, -0.9300,  0.5857]])
>>> b = torch.randn(3, 3)
>>> b
tensor([[-1.4382, -0.4094,  0.6650],
        [-0.2651,  0.5529, -0.7386],
        [-0.0719,  1.3542,  2.0680]])
>>> torch.div(a, b)
tensor([[-0.4734, -2.6912, -1.6723],
        [-3.6223, -0.1894,  1.2921],
        [ 3.4513, -0.6867,  0.2832]])

18. torch.exp(tensor, out=None)

说明:返回一个新张量,包含输入input张量每个元素的指数。

参数

  • input(Tensor) -- 输入张量
  • out(Tensor,可选) -- 输出张量
>>> a = torch.randn(5)
>>> torch.exp(a)
tensor([0.3263, 2.6751, 1.1791, 0.4109, 0.2701])

 

  • 7
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值