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)

"""

你可能感兴趣的:(Pytorch系列,python,pytorch)