网站后台开发技术,用手机做网站好学吗,北京定制网站公司,怎么建设网站页面文章目录 RNN/LSTM/GRU一、RNN1、为何引入RNN#xff1f;2、RNN的基本结构3、各种形式的RNN及其应用4、RNN的缺陷5、如何应对RNN的缺陷#xff1f;6、BPTT和BP的区别 二、LSTM1、LSTM 简介2、LSTM如何缓解梯度消失与梯度爆炸#xff1f; 三、GRU四、参考文献 RNN/LSTM/GRU
… 文章目录 RNN/LSTM/GRU一、RNN1、为何引入RNN2、RNN的基本结构3、各种形式的RNN及其应用4、RNN的缺陷5、如何应对RNN的缺陷6、BPTT和BP的区别 二、LSTM1、LSTM 简介2、LSTM如何缓解梯度消失与梯度爆炸 三、GRU四、参考文献 RNN/LSTM/GRU
一、RNN
1、为何引入RNN
循环神经网络Recurrent Neural NetworkRNN 是用来建模序列化数据的一种主流深度学习模型。我们知道传统的前馈神经网络一般的输入都是一个定长的向量无法处理变长的序列信息即使通过一些方法把序列处理成定长的向量模型也很难捕捉序列中的长距离依赖关系。RNN则通过将神经元串行起来处理序列化的数据。由于每个神经元能用它的内部变量保存之前输入的序列信息因此整个序列被浓缩成抽象的表示并可以据此进行分类或生成新的序列1。 2、RNN的基本结构
RNN的朴素形式可分别由如下两幅图表示2 其中 x 1 , x 2 , ⋯ , x T x_1,x_2,\cdots,x_T x1,x2,⋯,xT 是输入每一个位置是一个实数向量 U U U、 V V V、 W W W 是权重矩阵通常在模型初始化时随机生成通过梯度下降进行优化 h t h_t ht 是位于隐藏层上的活性值很多文献上也称为状态State或隐状态Hidden State p t p_t pt 表示第 t t t 个位置上的输出。 h t h_t ht、 p t p_t pt 可由下列公式得出 b b b 是偏置项 h t tanh ( U ⋅ h t − 1 W ⋅ x t b ) h_t\tanh\left(U\cdot h_{t-1}W\cdot x_tb\right) httanh(U⋅ht−1W⋅xtb) p t s o f t m a x ( V ⋅ h t c ) p_t\mathrm{softmax}(V\cdot h_tc) ptsoftmax(V⋅htc)
3、各种形式的RNN及其应用
(图片来自于cs231n) 模式描述应用领域One to One单个输入对应单个输出图像分类、回归任务One to Many单个输入生成序列输出图像字幕生成、音乐生成Many to One序列输入生成单个输出情感分析、时间序列分类Many to Many序列输入对应序列输出机器翻译、语音识别Many to Many同步同步序列输入输出视频帧分类、实时语音处理 4、RNN的缺陷
RNN通过在所有时间步共享相同的权重使得可以在不同时间步之间传递和积累信息从而更好地捕捉序列数据中的长期依赖关系但是缺点也很明显在RNN的学习过程中由于共享权重 W W W导致随着时间步的增加权重矩阵 W W W 不断连乘最终产生梯度消失即 ∂ L t ∂ h k \frac{\partial \mathcal{L}_{t}}{\partial \boldsymbol{h}_{k}} ∂hk∂Lt 消失 1 ≤ k ≤ t 1 \le k\le t 1≤k≤t 和梯度爆炸具体解释如下
首先由RNN前向传播公式 h t f ( W ⋅ h t − 1 U ⋅ x t b ) h_tf(W\cdot h_{t-1}U\cdot x_tb) htf(W⋅ht−1U⋅xtb) 其中 f f f 为激活函数。
在反向传播时BPTT损失函数 L \mathcal{L} L 对某一时间步长的梯度涉及到时间上所有的前置状态因此梯度会被多个矩阵连乘表示为: ∂ L ∂ h t ∂ L ∂ h T ⋅ ∏ k t T − 1 A k \frac{\partial\mathcal{L}}{\partial h_t}\frac{\partial\mathcal{L}}{\partial h_T}\cdot\prod_{kt}^{T-1}A_k ∂ht∂L∂hT∂L⋅kt∏T−1Ak 其中 A k diag ( f ′ ( h k ) ) ⋅ W A_k\operatorname{diag}(f^{\prime}(h_k))\cdot W Akdiag(f′(hk))⋅W 。
显然若 W 1 W1 W1随着时间的增加多个 W W W 连乘后结果会不断增大最终导致梯度爆炸
同理 W 1 W1 W1多个 W W W 连乘后结果会不断减小至趋于0最终导致梯度消失。
而在CNN中每一层的权重矩阵 W W W 是不同的并且在初始化时它们是独立同分布的因此最后可以相互抵消不容易发生梯度爆炸或消失。
5、如何应对RNN的缺陷
① 对于梯度爆炸一般通过权重衰减Weight Decay 或梯度截断Gradient Clipping 来避免3。权重衰减通过引入衰减系数来约束并避免权重矩阵元素过大从而减少梯度连乘时的爆炸风险梯度截断直接将梯度大小进行限制以防止梯度爆炸比如按值截断在第 t t t 次迭代时梯度为 g t g_t gt 给定一个区间 [ a , b ] [a,b] [a,b] 如果一个参数的梯度小于 a a a 时就将其设为 a a a 如果大于 b b b 时就将其设为 b b b公式如下 g t max ( min ( g t , b ) , a ) . \mathbf{g}_t\max(\min(\mathbf{g}_t,b),a). gtmax(min(gt,b),a).
② 对于梯度消失一个想法是改进激活函数比如替换成 ReLU 因为其右侧导数恒为 1 可以缓解梯度消失不能杜绝因为本质上是权重矩阵的问题。缺点是不好解决梯度爆炸从 RNN 的前向传播公式来看待这个问题前向传播公式如下 h t f ( W ⋅ h t − 1 U ⋅ x t b ) h_tf(W\cdot h_{t-1}U\cdot x_tb) htf(W⋅ht−1U⋅xtb) 使用 ReLU 激活函数后 h t h_t ht 可表达为 h t r e l u ( W ⋅ h t − 1 U ⋅ x t b ) h_t\mathrm{relu}\left(W\cdot h_{t-1}U\cdot x_tb\right) htrelu(W⋅ht−1U⋅xtb) 显然不管 h t − 1 h_{t-1} ht−1 怎么变化前面始终要乘上一个权重矩阵 W W W 因此替换激活函数后并不能实质上解决由于权重矩阵 W W W 连乘而导致的梯度爆炸问题。
③ 使用合适的权重初始化方法如 Xavier 初始化或 He 初始化使 W W W 的特征值接近 1 。
如果从结构上来考虑通过改变网络结构来减缓梯度消失或爆炸长短期记忆网络LSTMLong Short-Term Memory 就是基于这个想法诞生的。
6、BPTT和BP的区别
BP算法只处理纵向层级间的梯度反向传播适用于前馈神经网络。
BPTT算法在训练RNN时需要同时处理纵向层级间的反向传播深度方向和时间维度上的反向传播时间方向。 二、LSTM
1、LSTM 简介
LSTM 是循环神经网络的一个变体可以有效地解决简单循环神经网络的梯度爆炸或消失问题。LSTM 网络结构如下
在这里插入图片描述
LSTM 网络引入门控机制Gating Mechanism 来控制信息传递的路径公式如下 i t σ ( U i ⋅ h t − 1 W i ⋅ x t b i ) f t σ ( U f ⋅ h t − 1 W f ⋅ x t b f ) o t σ ( U o ⋅ h t − 1 W o ⋅ x t b o ) c ~ t tanh ( U c ⋅ h t − 1 W c ⋅ x t b c ) c t i t ⊙ c ~ t f t ⊙ c t − 1 h t o t ⊙ tanh ( c t ) \begin{array}{c}\boldsymbol{i}_{t}\sigma\left(\boldsymbol{U}_{i} \cdot \boldsymbol{h}_{t-1}\boldsymbol{W}_{i} \cdot \boldsymbol{x}_{t}\boldsymbol{b}_{i}\right) \\\boldsymbol{f}_{t}\sigma\left(\boldsymbol{U}_{f} \cdot \boldsymbol{h}_{t-1}\boldsymbol{W}_{f} \cdot \boldsymbol{x}_{t}\boldsymbol{b}_{f}\right) \\\boldsymbol{o}_{t}\sigma\left(\boldsymbol{U}_{o} \cdot \boldsymbol{h}_{t-1}\boldsymbol{W}_{o} \cdot \boldsymbol{x}_{t}\boldsymbol{b}_{o}\right) \\\tilde{\boldsymbol{c}}_{t}\tanh \left(\boldsymbol{U}_{c} \cdot \boldsymbol{h}_{t-1}\boldsymbol{W}_{c} \cdot \boldsymbol{x}_{t}\boldsymbol{b}_{c}\right) \\\boldsymbol{c}_{t}\boldsymbol{i}_{t} \odot \tilde{\boldsymbol{c}}_{t}\boldsymbol{f}_{t} \odot \boldsymbol{c}_{t-1} \\\boldsymbol{h}_{t}\boldsymbol{o}_{\boldsymbol{t}} \odot \tanh \left(\boldsymbol{c}_{t}\right)\end{array} itσ(Ui⋅ht−1Wi⋅xtbi)ftσ(Uf⋅ht−1Wf⋅xtbf)otσ(Uo⋅ht−1Wo⋅xtbo)c~ttanh(Uc⋅ht−1Wc⋅xtbc)ctit⊙c~tft⊙ct−1htot⊙tanh(ct) 进一步可以简写成 [ c ~ t o t i t f t ] [ tanh σ σ σ ] ( W [ x t h t − 1 ] b ) , c t f t ⊙ c t − 1 i t ⊙ c ~ t , h t o t ⊙ tanh ( c t ) , \begin{aligned}\begin{bmatrix}\tilde{\boldsymbol{c}}_t\\\\\boldsymbol{o}_t\\\\\boldsymbol{i}_t\\\\\boldsymbol{f}_t\end{bmatrix}\begin{bmatrix}\tanh\\\\\sigma\\\\\sigma\\\\\sigma\end{bmatrix}\begin{pmatrix}\boldsymbol{W}\begin{bmatrix}\boldsymbol{x}_t\\\\\boldsymbol{h}_{t-1}\end{bmatrix}\boldsymbol{b}\end{pmatrix},\\\\\boldsymbol{c}_t\boldsymbol{f}_t\odot\boldsymbol{c}_{t-1}\boldsymbol{i}_t\odot\boldsymbol{\tilde{c}}_t,\\\boldsymbol{h}_t\boldsymbol{o}_t\odot\tanh\left(\boldsymbol{c}_t\right),\end{aligned} c~totitft ctht tanhσσσ W xtht−1 b ,ft⊙ct−1it⊙c~t,ot⊙tanh(ct),
公式中有三个“门”分别为输入门 i t \boldsymbol{i}_t it 、遗忘门 f t \boldsymbol{f}_t ft 和输出门 o t \boldsymbol{o}_t ot 。这三个门的作用为
遗忘门 f t f_t ft 控制上一个时刻的内部状态 c t − 1 \boldsymbol c_t-1 ct−1 需要遗忘多少信息。输入门 i t \boldsymbol{i}_t it 控制当前时刻的候选状态 c ~ t \tilde{\boldsymbol{c}}_t c~t 有多少信息需要保存。输出门 o t \boldsymbol{o}_t ot 控制当前时刻的内部状态 c t \boldsymbol{c}_t ct 有多少信息需要输出给外部状态 h t . \boldsymbol{h}_t. ht.
具体的可点击查看如下视频很清晰易懂
【【官方双语】LSTM长短期记忆神经网络最简单清晰的解释来了】 https://www.bilibili.com/video/BV1zD421N7nA/?share_sourcecopy_webvd_source199a3f4e3a9db6061e1523e94505165a 2、LSTM如何缓解梯度消失与梯度爆炸
LSTM的细胞状态更新机制下图黄色部分可以有效地存储长期的信息 其更新公式如下 C t f t ⊙ C t − 1 i t ⊙ C ~ t C_tf_t\odot C_{t-1}i_t\odot\tilde{C}_t Ctft⊙Ct−1it⊙C~t
由于这一过程本质是线性操作加权求和相当于是所有候选路径的线性组合故不会因为一个路径上梯度的消失而导致整体梯度不断衰减。LSTM的细胞状态经过门控机制通过或阻断即 1 或 0控制这个线性组合达到缓解梯度消失的效果而门控机制又可以通过调节输入输出通过灵活地舍弃一些部分来缓解梯度爆炸问题。
简言之由于此线性组合会通过门控机制自主的调节而非 RNN 那样直接连乘因此可以达到减缓梯度消失和梯度爆炸的效果并实现对信息的过滤从而达到对长期记忆的保存与控制。 三、GRU
门控循环单元GRU 是对 LSTM 进行简化得到的模型。对于 LSTM 与 GRU 而言它们效果相当但由于 GRU 参数更少所以 GRU 的收敛速度更快计算效率更高。
与LSTM相比GRU 仅有两个门——更新门update gate和重置门reset gate不使用记忆元。重置门有助于捕获序列中的短期依赖关系更新门有助于捕获序列中的长期依赖关系详细结构如下图 四、参考文献 诸葛越, 葫芦娃, 百面机器学习, 北京:人民邮电出版社, 2018 ↩︎ 李航. 机器学习方法[M]. 第一版. 清华大学出版社, 2022. ↩︎ 邱锡鹏, 神经网络与深度学习, 北京:机械工业出版社, 2020 ↩︎
文章转载自: http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.zjrnq.cn.gov.cn.zjrnq.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn