Pytorch学习笔记(七)——数学运算

学习目标

掌握Pytorch


学习内容

常用的数学运算:
加减乘除:这几个操作建议直接使用运算符。

add +
sub -
mul *
div /
matmul 矩阵相乘
pow 指数
sqrt/rsqrt 平方根/平方根倒数
round 四舍五入

(1)torch.add(input,other,*,alpha=1,out=None) → Tensor
outi​=inputi​+alpha×otheri​
note: 当两个tensor维度不一致时,先进行broadcasting操作,再进行add。

a = torch.randn(4)	#tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
torch.add(a, 20)	#tensor([ 20.0202,  21.0985,  21.3506,  19.3944])
b = torch.randn(4)	#tensor([-0.9732, -0.3497,  0.6245,  0.4022])
c = torch.randn(4, 1)	
# tensor([[ 0.3743],
#         [-1.7724],
#         [-0.5811],
#         [-0.8017]])

torch.add(b, c, alpha=10)
# tensor([[  2.7695,   3.3930,   4.3672,   4.1450],
#         [-18.6971, -18.0736, -17.0994, -17.3216],
#         [ -6.7845,  -6.1610,  -5.1868,  -5.4090],
#         [ -8.9902,  -8.3667,  -7.3925,  -7.6147]])

(2)torch.sub(input, other, *, alpha=1, out=None) → Tensor
操作与torch.add类似。
outi​=inputi​-alpha×otheri​

a = torch.tensor(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值