pytorch的数学计算(元素级别)

1.绝对值、求和、三角函数

a = torch.Tensor([-1,0,1])
print(torch.abs(a))  # 绝对值
print(torch.add(a,3))  # 求和
print(torch.cos(a))
print(torch.acos(a))   # 反余弦
---------------------------------------------------------------------
result:
tensor([1., 0., 1.])
tensor([2., 3., 4.])
tensor([0.5403, 1.0000, 0.5403])
tensor([3.1416, 1.5708, 0.0000])

2.相乘再相加、相除再相加

a = torch.Tensor([-1,0,1])
b = torch.Tensor([1,2,3])
c = torch.Tensor([4,5,6])
# a,b,c维度必须相同
#a + 0.1*b/c
x = torch.addcdiv(a,0.1,b,c)
print(x)
#a + 0.1*b*c
y = torch.addcmul(a,0.1,b,c)
print(y)
---------------------------------------------------------------------
result:
tensor([-0.9750,  0.0400,  1.0500])
tensor([-0.6000,  1.0000,  2.8000])

3.向上取整、向下取整、夹逼函数、乘法、取相反数、取倒数、取平方根倒数和平方根

x = torch.Tensor([[-1.5,0,1.1],[1.2,2.8,3.6]])
# 向上取整
print(torch.ceil(x))
# 向下取整
print(torch.floor(x))
# 夹逼函数
print(torch.clamp(x,-1.5,1.5))     # 输入 下限 上限
# 乘法
print(torch.mul(x,0.1))
# 取相反数
print(torch.neg(x))        # neg 否定的
# 取倒数
print(torch.reciprocal(x))   # reciprocal 倒数
# 取平方根倒数
print(torch.rsqrt(x[x>0]))
# 平方根
print(torch.sqrt(x))   # 负数没有平方根
---------------------------------------------------------------------
result:
tensor([[-1.,  0.,  2.],
        [ 2.,  3.,  4.]])
tensor([[-2.,  0.,  1.],
        [ 1.,  2.,  3.]])
tensor([[-1.5000,  0.0000,  1.1000],
        [ 1.2000,  1.5000,  1.5000]])
tensor([[-0.1500,  0.0000,  0.1100],
        [ 0.1200,  0.2800,  0.3600]])
tensor([[ 1.5000, -0.0000, -1.1000],
        [-1.2000, -2.8000, -3.6000]])
tensor([[-0.6667,     inf,  0.9091],
        [ 0.8333,  0.3571,  0.2778]])
tensor([0.9535, 0.9129, 0.5976, 0.5270])
tensor([[   nan, 0.0000, 1.0488],
        [1.0954, 1.6733, 1.8974]])

4.除法、余数、取小数、四舍五入、指数运算

x = torch.Tensor([4,9])
# 除法
print(torch.div(x,3))
# 余数
q = torch.Tensor([2.1,2.5,2.7])
print(torch.fmod(q,2))
print(torch.remainder(q,2))      # remainder 余数
# 返回浮点数小数部分
print(torch.frac(q))
# 四舍五入
print(torch.round(q))
# 指数运算
print(torch.exp(torch.Tensor([0])))
---------------------------------------------------------------------
result:
tensor([1.3333, 3.0000])
tensor([0.1000, 0.5000, 0.7000])
tensor([0.1000, 0.5000, 0.7000])
tensor([0.1000, 0.5000, 0.7000])
tensor([2., 2., 3.])
tensor([1.])

5.自然对数、平滑对数、幂运算

# 自然对数
x = torch.Tensor([math.e,math.e**2])
print(torch.log(x))
# 平滑对数 对x+1平滑处理后再求对数
print(torch.log1p(x))
# 以2为底的对数
print(torch.log2(x))
# 以10为底的对数
print(torch.log10(x))
# 幂运算
print(torch.pow(x,1),torch.pow(x,2))
---------------------------------------------------------------------
result:
tensor([1., 2.])
tensor([1.3133, 2.1269])
tensor([1.4427, 2.8854])
tensor([0.4343, 0.8686])
tensor([2.7183, 7.3891]) tensor([ 7.3891, 54.5982])

6.线性插值

# 线性插值: out = start + weight(end-start)
x = torch.zeros(10)
print(x)
y = torch.arange(10).float()
print(y)
z = torch.lerp(x,y,0.5)
print(z)
---------------------------------------------------------------------
result:
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([0.0000, 0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000,
        4.5000])

7.Sigmoid函数,sign函数,截断值

# Sigmoid函数
x = torch.arange(-5,5,1).float()
print(x)
print(torch.sigmoid(x))
# sign函数: 元素为正返回1 为负返回-1 ,0返回0
print(torch.sign(x))
# 截断值:截取小数点前面的数
print(torch.trunc(torch.Tensor([-0.9,-1.2,-1.9,0,2.2,2.9])))    #trunc 劫取整数部分
---------------------------------------------------------------------
result:
tensor([-5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.])
tensor([0.0067, 0.0180, 0.0474, 0.1192, 0.2689, 0.5000, 0.7311, 0.8808, 0.9526,
        0.9820])
tensor([-1., -1., -1., -1., -1.,  0.,  1.,  1.,  1.,  1.])
tensor([-0., -1., -1.,  0.,  2.,  2.])
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值