当前位置:网站首页>[pytorch] pytorch automatic derivation, Tensor and Autograd
[pytorch] pytorch automatic derivation, Tensor and Autograd
2022-07-31 16:25:00 【Enzo wants to smash the computer】
在神经网络中,一个重要内容就是进行参数学习,而参数学习离不开求导.
现在大部分深度学习架构都有自动求导的功能,torch.autograd包 就是用来自动求导的.
torch.autograd Packages are on tensors 所有的操作 提供了自动求导功能
Learn and record this one 自动求导 的要点.
一、计算图
throughout the forward calculation process,PyTorch采用计算图的形式进行组织,The calculation diagram is动态图,and every time 前向传播时,将重新构建.其他深度学习架构,如TensorFlow、Keras一般为静态图.
- 计算图是一种有向无环图像,Graphically represent the relationship between operators and variables,直观高效.
- 图中 圆形表示变量,矩阵表示算子
- 表达式:z=wx+b,可写成两个表示式: y=wx,z=y+b,
- 其中x、w、b为变量,是用户创建的变量,不依赖于其他变量,故又称 为叶子节点.为计算各叶子节点的梯度,需要把对应的张量参数requires_grad属性设置为 True,这样就可自动跟踪其历史记录.(后面会细说)
- y、z 是计算得到的变量,非叶子节点,z为根节点
- mul和add是算子(或操作或函数)
These variables and operators constitute a complete calculation process (或前向传播过程)
二、自动求导要点
为实现对Tensor自动求导,需考虑如下事项:
1)创建叶子节点(Leaf Node)的Tensor,使用requires_gradThe parameter specifies whether to log it 的操作,以便之后利用backward()方法进行梯度求解.requires_grad参数的缺省值为 False,如果要对其求导需设置为True,Then the node that has a dependency on it automatically becomesTrue.
2)可利用requires_grad_()方法修改Tensor的requires_grad属性(For example, at the beginning of the training phase,requires_grad 值设置为了True,Modified in the testing phase to False).可以调用.detach()或 with torch.no_grad():,将不再计算张量的梯度,跟踪张量的历史记录.这点在Evaluation mode 型、Test model stage中常常用到.
3)通过created by the operationTensor(即非叶子节点),会automatically assignedgrad_fn属性.the property sheet shows the gradient function.叶子节点的grad_fn为None.
4)最后得到的Tensor(根节点)执行backward()函数,此时自动计算各变量的梯度.
- Each backpropagation ends,The gradient of leaf nodes will be cleared.If multiple backpropagation gradient accumulations are required,需要指定backward 中的参数retain_graph=True,In this way, the gradients of the child nodes are cumulative.
- 非叶子节点的梯度backward调用后即被清空
5)backward()函数接收参数,该参数应和调用backward()函数的Tensor的维度相同, 或者是可broadcast的维度.如果求导的Tensor为标量(即一个数字),则backward中的参数可省略.
三、Calculation of scalar backpropagation
- 假设x、w、b都是标量,则计算结果 z 也是标量 (z=wx+b)
- 对根节点z调用backward()方法,我们无须对 backward()传入参数
* 这里先提一嘴,It will be mentioned later: 如果目标张量对一个非标量调用backward(),则需要传入一个 gradient参数,该参数也是张量,而且需要与调用backward()的张量形状相同.
以下是实现自动求导的主要步骤:
import torch
# 输入张量 x
x = torch.Tensor([2])
# 初始化 权重参数w, 偏移量b,并设置 require_grad 属性为 True, 为自动求导
w = torch.randn(1, requires_grad=True)
b = torch.randn(1, requires_grad=True)
# Implement forward propagation
y = torch.mul(w, x)
z = torch.add(y, b)
# View leaf nodes separately x, w, b 和 非叶子节点 y、z 的require_grad属性
print(x.requires_grad, w.requires_grad, b.requires_grad) # False True True
print(y.requires_grad, z.requires_grad ) # True True
# 查看各节点是否为叶子节点
print(x.is_leaf, w.is_leaf, b.is_leaf, y.is_leaf, z.is_leaf) # True True True False False
# 分别查看 叶子节点 和 非叶子节点 的 grad_fn 属性
print(x.grad_fn, w.grad_fn, b.grad_fn) # None None None
print(y.grad_fn, z.grad_fn) # <MulBackward0 object at 0x7f8ac1303910> <AddBackward0 object at 0x7f8ac1303070>
z.backward() # Gradients do not accumulate
# z.backward(retain_graph=True) # 如果多次使用backward,Gradient accumulation is required,则需要修改参数retain_graph为True
# 查看叶子节点的梯度,x是叶子节点但它无须求导,故其梯度为None
print(w.grad,b.grad,x.grad) # tensor([2.]) tensor([1.]) None
#非叶子节点的梯度,执行backward之后,会自动清空
print(y.grad,z.grad) # None None
四、Computation of nonscalar backpropagation
边栏推荐
- How does automated testing create business value?
- WPF project - basic usage of controls entry, you must know XAML
- npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
- mysql黑窗口~建库建表
- Snake Project (Simple)
- Delete the disk in good condition (recovery partition)
- Foreign media right, apple on May be true in inventory
- 宁波大学NBU IT项目管理期末考试知识点整理
- 数据表插入数据insert into
- After the form is submitted, the page does not jump [easy to understand]
猜你喜欢
动态规划(一)
6-22漏洞利用-postgresql数据库密码破解
MySQL基础篇【单行函数】
Kubernetes common commands
How to switch remote server in gerrit
type of timer
二分查找的细节坑
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
随机推荐
type of timer
C language "the third is" upgrade (mode selection + AI chess)
C language - function
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
js的toString方法
form 表单提交后,使页面不跳转[通俗易懂]
牛客网刷题(一)
Delete the disk in good condition (recovery partition)
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
gerrit中如何切换远程服务器
小程序:matlab解微分方程「建议收藏」
How does automated testing create business value?
牛客网刷题(四)
2020微信小程序反编译教程(小程序反编译源码能用吗)
二分查找的细节坑
宁波大学NBU IT项目管理期末考试知识点整理
使用互相关进行音频对齐
jeecg主从数据库读写分离配置「建议收藏」
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀