Pytorch中常见的算数运算

案例:

import torch


# a = torch.rand(2, 1, 3) + torch.rand(3)
a = torch.rand(2, 1) + torch.rand(3)
print(a)

a = torch.tensor(2)
b = torch.tensor(3)
c = torch.add(a, b)
print(c)  # 5
print(a)  # 2
# 会修改原始值,add_函数
a.add_(b)
print(a)  # 5

"""
out:
tensor(5)
tensor(2)
tensor(5)
"""

a = torch.tensor(2)
b = torch.tensor(3)
c = torch.sub(a, b)
print(c)
print(a)
# 会修改原始值,sub_函数
a.sub_(b)
print(a)

"""
tensor(-1)
tensor(2)
tensor(-1)
"""

a = torch.tensor(2)
b = torch.tensor(3)
c = torch.mul(a, b)
print(c)
print(a)
# 会修改原始值,mul_函数
a.mul_(b)
print(a)

"""
tensor(6)
tensor(2)
tensor(6)
"""

a = torch.tensor(6)
b = torch.tensor(3)
c = torch.div(a, b)
print(c)
print(a)
# 会修改原始值,div_函数
a.div_(b)
print(a)

"""
tensor(2)
tensor(6)
tensor(2)
"""



# 幂运算
a = torch.tensor(6)
c = torch.pow(a, 2)
print(c)
print(a)
print(a ** 2)
# 会修改原始值,pow_函数
a.pow_(2)
print(a)

"""
tensor(36)
tensor(6)
tensor(36)
tensor(36)
"""

# 指数运算
a = torch.tensor(2.6)
print(torch.exp(a))
# 改变原始值
a.exp_()
print(a)

"""
tensor(13.4637)
tensor(13.4637)
"""

# 开发运算
a = torch.tensor(4.4)
print(torch.sqrt(a))
print(a)
# 改变原始值
print(a.sqrt_())
print(a)

"""
tensor(2.0976)
tensor(4.4000)
tensor(2.0976)
tensor(2.0976)
"""

# 对数运算
a = torch.tensor(4.4)
print(torch.log(a))
print(a)
# 改变原始值
print(a.log_())
print(a)
"""
tensor(1.4816)
tensor(4.4000)
tensor(1.4816)
tensor(1.4816)

"""

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅蓝的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值