pytorch基操02-回到小学之基本算数运算

1 基本算数运算

今天让我们回到小学,学习pytorch中的算数运算。

1.1 加减法

加法运算通过add方法实现,add_表示原地加,修改执行该操作的对象。

# 加法运算
print('-'*8+'add'+'-'*8)
a=torch.ones(size=(2,3))
b=torch.ones(size=(2,3))*2
# a+b
print('a+b:\n',a+b)
# torch.add(a,b)
print('torch.add(a,b):\n',torch.add(a,b))
# Tensor.add(b)
print('a.add(b):\n',a.add(b))
# in-place add
a.add_(b)
print('a.add_(b)->a:\n',a)
# 减法sub类比add
print('b.sub(a):\n',b.sub(a))
--------add--------
a+b:
 tensor([[3., 3., 3.],
        [3., 3., 3.]])
torch.add(a,b):
 tensor([[3., 3., 3.],
        [3., 3., 3.]])
a.add(b):
 tensor([[3., 3., 3.],
        [3., 3., 3.]])
a.add_(b)->a:
 tensor([[3., 3., 3.],
        [3., 3., 3.]])
b.sub(a):
 tensor([[-1., -1., -1.],
        [-1., -1., -1.]])

1.2 乘除法

# 哈达玛积 对应元素相乘
a=torch.ones(size=(2,3))*3
b=torch.ones(size=(2,3))*2
print('-'*8+'multiplication'+'-'*8)
print('a*b:\n',a*b)
print('torch.mul(a,b):\n',torch.mul(a,b))
print('a.mul(b):\n',a.mul(b))
a.mul_(b)
print('a.mul_(b)->a:\n',a)
# 除法div 类比mul
print('a.div(b):\n',a.div(b))
--------multiplication--------
a*b:
 tensor([[6., 6., 6.],
        [6., 6., 6.]])
torch.mul(a,b):
 tensor([[6., 6., 6.],
        [6., 6., 6.]])
a.mul(b):
 tensor([[6., 6., 6.],
        [6., 6., 6.]])
a.mul_(b)->a:
 tensor([[6., 6., 6.],
        [6., 6., 6.]])
a.div(b):
 tensor([[3., 3., 3.],
        [3., 3., 3.]])

1.3 矩阵运算

在超过两维的Tensor间进行矩阵运算,矩阵运算只作用在最后两个维度上,其余的维度当作索引,但这就就只能使用matmul方法。

# 矩阵运算
print('-'*8+'matrix muliplication'+'-'*8)
a=torch.ones(size=(2,3))*3
b=torch.ones(size=(3,2))*2
print('torch.mm(a,b):\n',torch.mm(a,b))
print('torch.matmul(a,b):\n',torch.matmul(a,b))
print('a.mm(b):\n',a.mm(b))
print('a.matmul(b):\n',a.matmul(b))
print('a@b:\n',a@b)
# 对于高维度的Tensor,矩阵运算只作用在最后两个维度上,其余的维度当作索引
# 且只能使用matmul作用高纬度Tensor
a=torch.ones(size=(20,10,2,3))
b=torch.ones(size=(20,10,3,2))
# multidimensional matrix multiplication
print('multidimensional matrix multiplication:')
print('matmul(a,b).size:',torch.matmul(a,b).size())
print('a.matmul(b).size:',a.matmul(b).size())
--------matrix muliplication--------
torch.mm(a,b):
 tensor([[18., 18.],
        [18., 18.]])
torch.matmul(a,b):
 tensor([[18., 18.],
        [18., 18.]])
a.mm(b):
 tensor([[18., 18.],
        [18., 18.]])
a.matmul(b):
 tensor([[18., 18.],
        [18., 18.]])
a@b:
 tensor([[18., 18.],
        [18., 18.]])
multidimensional matrix multiplication:
matmul(a,b).size: torch.Size([20, 10, 2, 2])
a.matmul(b).size: torch.Size([20, 10, 2, 2])

1.4 幂运算

# 幂运算
print('-'*8+'幂运算'+'-'*8)
a=torch.ones(size=(2,3))*3
b=torch.ones(size=(2,3))*2
print('torch.pow(a,b):\n',torch.pow(a,b))
print('a.pow(b):\n',a.pow(b))
print('a**b:\n',a**b)
a.pow_(b)
print('a.pow_(b)->a:\n',a)
# e的次方
print('torch.exp(b):\n',torch.exp(b)) #e^2
print('b.exp():\n',b.exp())
b.exp_()
print('b.exp_()->b:\n',b)
--------幂运算--------
torch.pow(a,b):
 tensor([[9., 9., 9.],
        [9., 9., 9.]])
a.pow(b):
 tensor([[9., 9., 9.],
        [9., 9., 9.]])
a**b:
 tensor([[9., 9., 9.],
        [9., 9., 9.]])
a.pow_(b)->a:
 tensor([[9., 9., 9.],
        [9., 9., 9.]])
torch.exp(b):
 tensor([[7.3891, 7.3891, 7.3891],
        [7.3891, 7.3891, 7.3891]])
b.exp():
 tensor([[7.3891, 7.3891, 7.3891],
        [7.3891, 7.3891, 7.3891]])
b.exp_()->b:
 tensor([[7.3891, 7.3891, 7.3891],
        [7.3891, 7.3891, 7.3891]])

1.5 开方运算

# 开方运算
b=torch.ones(2,3)*2
print('-'*8+'开方运算'+'-'*8)
print('torch.sqrt(b):\n',torch.sqrt(b))
print('b.sqrt():\n',b.sqrt())
b.sqrt_()
print('b.sqrt_()->b:\n',b)

--------开方运算--------
torch.sqrt(b):
 tensor([[1.4142, 1.4142, 1.4142],
        [1.4142, 1.4142, 1.4142]])
b.sqrt():
 tensor([[1.4142, 1.4142, 1.4142],
        [1.4142, 1.4142, 1.4142]])
b.sqrt_()->b:
 tensor([[1.4142, 1.4142, 1.4142],
        [1.4142, 1.4142, 1.4142]])

1.6 对数运算

# 对数运算 有log log2 log10
print('-'*8+'对数操作'+'-'*8)
a=torch.ones(2,3)
print('torch.log(a):\n',torch.log(a))
print('a.log():\n',a.log())
a.log_()
print('a.log_()->a:\n',a)

--------对数操作--------
torch.log(a):
 tensor([[0., 0., 0.],
        [0., 0., 0.]])
a.log():
 tensor([[0., 0., 0.],
        [0., 0., 0.]])
a.log_()->a:
 tensor([[0., 0., 0.],
        [0., 0., 0.]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值