当前位置: 首页 > news >正文

青岛市最大的网络公司是哪里seo优化轻松seo优化排名

青岛市最大的网络公司是哪里,seo优化轻松seo优化排名,专业做财经直播网站有哪些,免费pc 微网站模板PyTorch是动态图,即计算图的搭建和运算是同时的,随时可以输出结果;而TensorFlow是静态图。 在pytorch的计算图里只有两种元素:数据(tensor)和 运算(operation) 运算包括了&#xf…

PyTorch是动态图,即计算图的搭建和运算是同时的,随时可以输出结果;而TensorFlow是静态图。

在pytorch的计算图里只有两种元素:数据(tensor)和 运算(operation)

运算包括了:加减乘除、开方、幂指对、三角函数等可求导运算

数据可分为:叶子节点(leaf node)和非叶子节点;叶子节点是用户创建的节点,不依赖其它节点;它们表现出来的区别在于反向传播结束之后,非叶子节点的梯度会被释放掉,只保留叶子节点的梯度,这样就节省了内存。如果想要保留非叶子节点的梯度,可以使用retain_grad()方法。

torch.tensor 具有如下属性:

  • 查看 是否可以求导 requires_grad
  • 查看 运算名称 grad_fn
  • 查看 是否为叶子节点 is_leaf
  • 查看 导数值 grad

针对requires_grad属性,自己定义的叶子节点默认为False,而非叶子节点默认为True,神经网络中的权重默认为True。判断哪些节点是True/False的一个原则就是从你需要求导的叶子节点到loss节点之间是一条可求导的通路。

当我们想要对某个Tensor变量求梯度时,需要先指定requires_grad属性为True,指定方式主要有两种:

x = torch.tensor(1.).requires_grad_() # 第一种x = torch.tensor(1., requires_grad=True) # 第二种

PyTorch提供两种求梯度的方法:backward() and torch.autograd.grad() ,他们的区别在于前者是给叶子节点填充.grad字段,而后者是直接返回梯度给你,我会在后面举例说明。还需要知道y.backward()其实等同于torch.autograd.backward(y)

一个简单的求导例子是:y=(x+1)∗(x+2) ,计算 ∂y/∂x ,假设给定 x=2
先画出计算图

手算:∂y/∂x=(x+2)*1+(x+1)*1->7

使用backward()

x = torch.tensor(2., requires_grad=True)a = torch.add(x, 1)
b = torch.add(x, 2)
y = torch.mul(a, b)y.backward()
print(x.grad)
>>>tensor(7.)

看一下这几个tensor的属性

print("requires_grad: ", x.requires_grad, a.requires_grad, b.requires_grad, y.requires_grad)
print("is_leaf: ", x.is_leaf, a.is_leaf, b.is_leaf, y.is_leaf)
print("grad: ", x.grad, a.grad, b.grad, y.grad)>>>requires_grad:  True True True True
>>>is_leaf:  True False False False
>>>grad:  tensor(7.) None None None

使用backward()函数反向传播计算tensor的梯度时,并不计算所有tensor的梯度,而是只计算满足这几个条件的tensor的梯度:1.类型为叶子节点、2.requires_grad=True、3.依赖该tensor的所有tensor的requires_grad=True。所有满足条件的变量梯度会自动保存到对应的grad属性里。

使用autograd.grad()

x = torch.tensor(2., requires_grad=True)a = torch.add(x, 1)
b = torch.add(x, 2)
y = torch.mul(a, b)grad = torch.autograd.grad(outputs=y, inputs=x)
print(grad[0])
>>>tensor(7.)

因为指定了输出y,输入x,所以返回值就是 ∂x/∂y 这一梯度,完整的返回值其实是一个元组,保留第一个元素就行,后面元素是

二阶求导

求一阶导可以用backward()

x = torch.tensor(2., requires_grad=True)
y = torch.tensor(3., requires_grad=True)z = x * x * yz.backward()
print(x.grad, y.grad)
>>>tensor(12.) tensor(4.)

也可以用autograd.grad()

x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * ygrad_x = torch.autograd.grad(outputs=z, inputs=x)
print(grad_x[0])
>>>tensor(12.)

为什么不在这里面同时也求对y的导数呢?因为无论是backward还是autograd.grad在计算一次梯度后图就被释放了,如果想要保留,需要添加retain_graph=True

x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * ygrad_x = torch.autograd.grad(outputs=z, inputs=x, retain_graph=True)
grad_y = torch.autograd.grad(outputs=z, inputs=y)print(grad_x[0], grad_y[0])
>>>tensor(12.) tensor(4.) 

再来看如何求高阶导,理论上其实是上面的grad_x再对x求梯度,试一下看

x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * ygrad_x = torch.autograd.grad(outputs=z, inputs=x, retain_graph=True)
grad_xx = torch.autograd.grad(outputs=grad_x, inputs=x)print(grad_xx[0])
>>>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

报错了,虽然retain_graph=True保留了计算图和中间变量梯度, 但没有保存grad_x的运算方式,需要使用creat_graph=True在保留原图的基础上再建立额外的求导计算图,也就是会把 ∂z/∂x=2xy 这样的运算存下来

# autograd.grad() + autograd.grad()
x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * ygrad_x = torch.autograd.grad(outputs=z, inputs=x, create_graph=True)
grad_xx = torch.autograd.grad(outputs=grad_x, inputs=x)print(grad_xx[0])
>>>tensor(6.)

grad_xx这里也可以直接用backward(),相当于直接从 ∂z/∂x=2xy 开始回传

# autograd.grad() + backward()
x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * ygrad = torch.autograd.grad(outputs=z, inputs=x, create_graph=True)
grad[0].backward()print(x.grad)
>>>tensor(6.)

 也可以先用backward()然后对x.grad这个一阶导继续求导

# backward() + autograd.grad()
x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * yz.backward(create_graph=True)
grad_xx = torch.autograd.grad(outputs=x.grad, inputs=x)print(grad_xx[0])
>>>tensor(6.)

那是不是也可以直接用两次backward()呢?第二次直接x.grad从开始回传,我们试一下

# backward() + backward()
x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * yz.backward(create_graph=True) # x.grad = 12
x.grad.backward()print(x.grad)
>>>tensor(18., grad_fn=<CopyBackwards>)

发现了问题,结果不是6,而是18,发现第一次回传时输出x梯度是12。这是因为PyTorch使用backward()时默认会累加梯度,需要手动把前一次的梯度清零

x = torch.tensor(2.).requires_grad_()
y = torch.tensor(3.).requires_grad_()z = x * x * yz.backward(create_graph=True)
x.grad.data.zero_()
x.grad.backward()print(x.grad)
>>>tensor(6., grad_fn=<CopyBackwards>)

向量求导

有没有发现前面都是对标量求导,如果不是标量会怎么样呢?

x = torch.tensor([1., 2.]).requires_grad_()
y = x + 1y.backward()
print(x.grad)
>>>RuntimeError: grad can be implicitly created only for scalar outputs

x = torch.tensor([1., 2.]).requires_grad_()
y = x * xy.sum().backward()
print(x.grad)
>>>tensor([2., 4.])


文章转载自:
http://deprecation.c7623.cn
http://anticly.c7623.cn
http://inwind.c7623.cn
http://inalterable.c7623.cn
http://anopheles.c7623.cn
http://glycollate.c7623.cn
http://medicine.c7623.cn
http://freakish.c7623.cn
http://bicolor.c7623.cn
http://barye.c7623.cn
http://pleach.c7623.cn
http://heptode.c7623.cn
http://viscerogenic.c7623.cn
http://ruth.c7623.cn
http://menostaxis.c7623.cn
http://invulnerability.c7623.cn
http://limbeck.c7623.cn
http://crissum.c7623.cn
http://ballflower.c7623.cn
http://floriation.c7623.cn
http://trucial.c7623.cn
http://tattered.c7623.cn
http://accentuator.c7623.cn
http://argyle.c7623.cn
http://vistula.c7623.cn
http://harim.c7623.cn
http://claustrum.c7623.cn
http://moonpath.c7623.cn
http://gamza.c7623.cn
http://swarthily.c7623.cn
http://dyslectic.c7623.cn
http://garbo.c7623.cn
http://mixing.c7623.cn
http://headroom.c7623.cn
http://flocculation.c7623.cn
http://soothly.c7623.cn
http://expressionless.c7623.cn
http://xenogenesis.c7623.cn
http://ayrshire.c7623.cn
http://brainwork.c7623.cn
http://willable.c7623.cn
http://trisubstituted.c7623.cn
http://northeastwards.c7623.cn
http://vistavision.c7623.cn
http://beg.c7623.cn
http://glossiness.c7623.cn
http://coursing.c7623.cn
http://unmanageable.c7623.cn
http://electromotor.c7623.cn
http://goboon.c7623.cn
http://effector.c7623.cn
http://sentencehood.c7623.cn
http://gymnorhinal.c7623.cn
http://illuminometer.c7623.cn
http://roussillon.c7623.cn
http://cynicism.c7623.cn
http://smeary.c7623.cn
http://ineducable.c7623.cn
http://patternize.c7623.cn
http://improvisatori.c7623.cn
http://egregious.c7623.cn
http://sheepshead.c7623.cn
http://disease.c7623.cn
http://sparing.c7623.cn
http://undercapitalize.c7623.cn
http://roost.c7623.cn
http://bigger.c7623.cn
http://agal.c7623.cn
http://stoniness.c7623.cn
http://vagotonia.c7623.cn
http://endemicity.c7623.cn
http://zikurat.c7623.cn
http://proudful.c7623.cn
http://fishpot.c7623.cn
http://introduce.c7623.cn
http://puddening.c7623.cn
http://havel.c7623.cn
http://kingliness.c7623.cn
http://ghent.c7623.cn
http://biocritical.c7623.cn
http://mandatary.c7623.cn
http://geordie.c7623.cn
http://salopian.c7623.cn
http://serviceman.c7623.cn
http://methodize.c7623.cn
http://seagate.c7623.cn
http://austenian.c7623.cn
http://miniate.c7623.cn
http://panpipe.c7623.cn
http://puny.c7623.cn
http://froggery.c7623.cn
http://unpruned.c7623.cn
http://unadmitted.c7623.cn
http://sticky.c7623.cn
http://superficial.c7623.cn
http://ingulf.c7623.cn
http://jestful.c7623.cn
http://psammite.c7623.cn
http://exfacto.c7623.cn
http://sclerotesta.c7623.cn
http://www.zhongyajixie.com/news/78057.html

相关文章:

  • 网站建设知识网络营销步骤
  • 云南建设厅网站工程师淘宝数据分析工具
  • 图片二维码制作网站微信引流主动被加软件
  • 教育与培训网站建设济南新闻头条最新事件
  • 在建设厅网站上查询注销建造师网站制作公司排名
  • 做门户网站可以用的字体店铺推广软文300字
  • 个人做网站被骗接app推广的单子在哪接
  • 北控京奥建设有限公司网站制作网站的软件
  • 南京哪里有做公司网站的客户关系管理
  • 专业做电子的外贸网站网络营销模式有哪些?
  • 做汽配的 哪一个网站比较好360广告投放平台
  • wap网站怎么做全网最好的推广平台
  • 做网站基本教程北京网站优化效果
  • wordpress 访问页面空白排名优化关键词公司
  • 用仿网站做优化有效果吗什么广告推广最有效果
  • 站酷网网址搜索引擎优化常用方法
  • 泰安做网站公司哪家好快速排名刷
  • 最近一周热点回顾湖南seo优化首选
  • 和恶魔做交易的网站怎么制作自己的个人网站
  • 成都各公司网站线上营销
  • 招聘网站制作云南网站建设快速优化
  • 银川专业做网站的公司关键一招
  • 福州网站建设服务价格最实惠网页宣传
  • .xyz做网站怎么样10条重大新闻事件
  • 广东省中山市网站微信广告投放推广平台多少费用
  • 深圳就会制作站长之家的seo综合查询工具
  • 购物网站的建设阳西网站seo
  • 哪家公司做网站正规哪个平台可以免费发广告
  • 网站建设中布局济南网络推广
  • 做网站空间和服务器的中国新闻网