当前位置:网站首页>《nlp入门+实战:第三章:梯度下降和反向传播 》
《nlp入门+实战:第三章:梯度下降和反向传播 》
2022-07-26 01:00:00 【ZNineSun】
文章目录
上一篇: 《nlp入门+实战:第二章:pytorch的入门使用 》
1.梯度是什么?
梯度:是一个向量,导数+变化最快的方向(学习参数的前进方向)
回顾机器学习
收集数据x。构建机器学习模型f,得到
f ( x , w ) = Y p r e d i c t f(x,w)=Y_{predict} f(x,w)=Ypredict
也就是说通过我们的模型计算之后会得到一系列的预测值
判断模型好坏的方法:
l o s s = ( Y p r e d i c t − Y t r u e ) 2 − > 回归损失 l o s s = Y t r u e ∗ l o g ( Y p r e d i c t ) − > 分类损失 loss=(Y_{predict}-Y_{true})^2 ->回归损失\\ loss=Y_{true}*log(Y_{predict}) ->分类损失 loss=(Ypredict−Ytrue)2−>回归损失loss=Ytrue∗log(Ypredict)−>分类损失
目标:通过调整(学习)参数w,尽可能的降低loss。那么我们该如何调整w呢?
随机选择一个起始点W0,证过调整W0,让loss函数取到最小值。
w的更新方法:
1.计算w的梯度(导数)
Δ w = f ( w + 0.0000001 ) − f ( w − 0.0000001 ) 2 ∗ 0.0000001 \Delta w=\frac{f(w+0.0000001)-f(w-0.0000001)}{2*0.0000001} Δw=2∗0.0000001f(w+0.0000001)−f(w−0.0000001)
2.更新w
w = w − α Δ w w=w-\alpha \Delta w w=w−αΔw
其中:
- △w<0,意味着w将增大
- △w>0,意味着w将减小
总结:梯度就是多元函数参数的变化趋势(参数学习的方向),只有一个自变量时称为导数,多元的话就是偏导数。
2.偏导的计算
2.1 常见的导数计算

2.2 多元函数求偏导

3.反向传播算法
3.1 计算图和反向传播
计算图:通过图的方式来描述函数的图形
如J(a,b,c) = 3(a + bc),令u=a+v,v=bc,则有J(u)=3u
把它绘制成计算图可以表示为
绘制成计算图之后,可以清楚的看到向前计算的过程
之后对每个节点求偏导,可有:
对于反向传播,因为我们最终要求的是
- J对a的偏导
- J对b的偏导
- J对c的偏导
但是我们直接对a,b,c求偏导是没办法的,所以,根据上图,我们可以看出反向传播的过程就是一个从右往左的过程,自变量(a, b, c)各自的偏导就是连线上的梯度的乘积:
d J d u = 3 \frac{dJ}{du}=3 dudJ=3
d J d b = d J d u ∗ d u d v ∗ d v d b = 3 ∗ 1 ∗ c \frac{dJ}{db}=\frac{dJ}{du}*\frac{du}{dv}*\frac{dv}{db}=3*1*c dbdJ=dudJ∗dvdu∗dbdv=3∗1∗c
d J d c = d J d u ∗ d u d v ∗ d v d c = 3 ∗ 1 ∗ b \frac{dJ}{dc}=\frac{dJ}{du}*\frac{du}{dv}*\frac{dv}{dc}=3*1*b dcdJ=dudJ∗dvdu∗dcdv=3∗1∗b
3.2 神经网络中的反向传播
3.2.1 神经网络的示意图
w1,w2,…,wn表示网络第n层权重
wn[i,j]表示第n层第i个神经元,连接到第n+1层第j个神经元的权重。
如:w3[2,1]:表示第三层的第二个神经元到第四层的第一个神经元的权重
3.2.2 神经网络的计算图

其中:
- △out:是根据损失函数对预测值进行求导得到的结果
- f函数:可以理解为激活函数
加入我们要求△out对w1[1,2]的偏导,可以看到下图
从w1[1,2]到△out有两个路径,分别是红色线和蓝色线,所以我们只需对绿色框内的两条路径乘积相加之后再与绿色框外面路径值相乘即可,结果如下:
公式分为两部分:
- 1.括号外:左边红线部分
- 2.括号内
- 1.加号左边:右边红线部分
- 2加号右边:蓝线部分
但是这样做,当模型很大的时候,计算量非常大
所以反向传播的思想就是对其中的某一个参数单独求梯度,之后更新,如下图所示:
计算过程如下:
更新参数之和继续反向传播
计算过程如下:
继续反向传播
计算过程如下:
以上过程就是对下面公式的拆解:
我们在去思考一下,我们反向传播的时候需要我们向前传播过程中计算出的结果,所以我们需要保留向前传播的痕迹,这一点在pytorch中将会有所体现。
边栏推荐
- Tell you the meaning of unit testing from another angle
- The difference and application of in and exists in SQL statement
- User defined variables and extracted public variables of JMeter
- Using questpdf operation to generate PDF is faster and more efficient!
- Small sample learning - getting started
- Django数据库增删改查
- 阿明的告白
- [RTOS training camp] learn C language from a higher perspective
- What is the difference between request forwarding and request redirection?
- typing‘ has no attribute ‘_ SpecialForm‘
猜你喜欢

【RTOS训练营】任务调度(续)、任务礼让、调度总结、队列和晚课提问

Tell you the meaning of unit testing from another angle

ASP.NET Core配置
![[RTOS training camp] GPIO knowledge and preview arrangement + evening class questions](/img/44/f3323a16e505a7fe923b25630f62f7.jpg)
[RTOS training camp] GPIO knowledge and preview arrangement + evening class questions

【RTOS训练营】GPIO知识和预习安排 + 晚课提问

Save unhappiness! My robot psychologist; KPI tracking of machine learning; Pytorch implementation of Yolo V7; Li Hang's new book "machine learning methods" open trial | showmeai Information Daily

【纪中】2022.7.16 1432.输油管道

SQL statement exercise

Azure synapse analytics Performance Optimization Guide (1) -- optimize performance using ordered aggregate column storage indexes

Travel + strategy accelerated landing, jietu new product matrix exposure
随机推荐
Download exclusively | Alibaba cloud maxcompute questions and answers to unlock SaaS mode cloud data warehouse in this electronic manual!
动态IP地址是什么?为什么大家会推荐用动态ip代理?
Practical exercise | find customers who buy more than n products within a given time range
Microwave oven rectifier diode cl01-12
【RTOS训练营】关于上课和答疑
Seretod2022 track1 code analysis - task-based dialogue system challenge for semi supervised and reinforcement learning
聊聊研发团队中的“人”
matlab 按位 与 或 非
微波炉整流二极管 CL01-12
Implementation process of adding loading effect to easycvr page
[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions
用 QuestPDF操作生成PDF更快更高效!
Leetcode notes 350. Intersection of two arrays II
MMOCR使用指南
Zabbix监控主机及资源告警
【RTOS训练营】上节回顾、空闲任务、定时器任务、执行顺序、调度策略和晚课提问
Some abnormal error reports and precautions of flowable (1)
Upload local file trial version using SAP ui5 fileuploader control
Regular expression
200 yuan a hair dryer, only a week, to achieve 2million?