本文转自《零基础入门深度学习》系列文章,阅读原文请移步这里
之前我们介绍了循环神经网络以及它的训练算法。我们也介绍了循环神经网络很难训练的原因,这导致了它在实际应用中,很难处理长距离的依赖。在本文中,我们将介绍一种改进之后的循环神经网络:长短时记忆网络(Long Short Term Memory Network, LSTM),它成功的解决了原始循环神经网络的缺陷,成为当前最流行的RNN,在语音识别、图片描述、自然语言处理等许多领域中成功应用。但不幸的一面是,LSTM的结构很复杂,因此,我们需要花上一些力气,才能把LSTM以及它的训练算法弄明白。在搞清楚LSTM之后,我们再介绍一种LSTM的变体:GRU (Gated Recurrent Unit)。 它的结构比LSTM简单,而效果却和LSTM一样好,因此,它正在逐渐流行起来。最后,我们仍然会动手实现一个LSTM。

一、长短时记忆网络是啥

我们首先了解一下长短时记忆网络产生的背景。回顾一下《深度学习之循环神经网络(RNN)》中推导的,误差项沿时间反向传播的公式:δkT=δtT∏i=kt−1diag[f′(neti)]W\delta_k^T= \delta_t^T\prod_{i=k}^{t-1}diag[f'(net_i)]WδkT​=δtT​i=k∏t−1​diag[f′(neti​)]W我们可以根据下面的不等式,来获取δkT\delta_k^TδkT​的模的上界(模可以看做对δkT\delta_k^TδkT​中每一项值的大小的度量):∥δkT∥⩽∥δtT∥∏i=kt−1∥W∥∥diag[f′(neti)]∥\lVert\delta_k^T\rVert\leqslant\lVert\delta_t^T\rVert\prod_{i=k}^{t-1}\lVert W \rVert\lVert diag[f'(net_i)]\rVert∥δkT​∥⩽∥δtT​∥i=k∏t−1​∥W∥∥diag[f′(neti​)]∥⩽∥δtT∥(βWβf)t−k\leqslant\lVert\delta_t^T\rVert(\beta_W\beta_f)^{t-k}\space\space\space\space\space\space\space\space\space\space\space\space\space⩽∥δtT​∥(βW​βf​)t−k             我们可以看到,误差项δ\deltaδ从t时刻传递到k时刻,其值的上界是βfβw\beta_f\beta_wβf​βw​的指数函数。βfβw\beta_f\beta_wβf​βw​分别是对角矩阵diag[f′(neti)]diag[f'(net_i)]diag[f′(neti​)]和矩阵WWW模的上界。显然,除非βfβw\beta_f\beta_wβf​βw​乘积的值位于1附近,否则,当t-k很大时(也就是误差传递很多个时刻时),整个式子的值就会变得极小(当βfβw\beta_f\beta_wβf​βw​乘积小于1)或者极大(当βfβw\beta_f\beta_wβf​βw​乘积大于1),前者就是梯度消失,后者就是梯度爆炸。虽然科学家们搞出了很多技巧(比如怎样初始化权重),让的值尽可能贴近于1,终究还是难以抵挡指数函数的威力。

梯度消失到底意味着什么?在《深度学习之循环神经网络(RNN)》中我们已证明,权重数组W最终的梯度是各个时刻的梯度之和,即:∇WE=∑i=1t∇WtE\nabla_{W}E=\sum_{i=1}^t\nabla_{W_t}E\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space∇W​E=i=1∑t​∇Wt​​E                         =∇WtE+∇Wt−1E+...+∇W1E\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\nabla_{W_t}E+\nabla_{W_{t-1}}E+...+\nabla_{W_1}E                      =∇Wt​​E+∇Wt−1​​E+...+∇W1​​E假设某轮训练中,各时刻的梯度以及最终的梯度之和如下图:
我们就可以看到,从上图的t-3时刻开始,梯度已经几乎减少到0了。那么,从这个时刻开始再往之前走,得到的梯度(几乎为零)就不会对最终的梯度值有任何贡献,这就相当于无论t-3时刻之前的网络状态h是什么,在训练中都不会对权重数组W的更新产生影响,也就是网络事实上已经忽略了t-3时刻之前的状态。这就是原始RNN无法处理长距离依赖的原因。

既然找到了问题的原因,那么我们就能解决它。从问题的定位到解决,科学家们大概花了7、8年时间。终于有一天,Hochreiter和Schmidhuber两位科学家发明出长短时记忆网络,一举解决这个问题。

其实,长短时记忆网络的思路比较简单。原始RNN的隐藏层只有一个状态,即h,它对于短期的输入非常敏感。那么,假如我们再增加一个状态,即c,让它来保存长期的状态,那么问题不就解决了么?如下图所示:

新增加的状态c,称为单元状态(cell state)。我们把上图按照时间维度展开:

上图仅仅是一个示意图,我们可以看出,在t时刻,LSTM的输入有三个:当前时刻网络的输入值xt\mathbf{x}_txt​、上一时刻LSTM的输出值ht−1\mathbf{h}_{t-1}ht−1​、以及上一时刻的单元状态ct−1\mathbf{c}_{t-1}ct−1​;LSTM的输出有两个:当前时刻LSTM输出值ht\mathbf{h}_{t}ht​、和当前时刻的单元状态ct\mathbf{c}_{t}ct​。注意x、h、c\mathbf{x}、\mathbf{h}、\mathbf{c}x、h、c都是向量

LSTM的关键,就是怎样控制长期状态ccc。在这里,LSTM的思路是使用三个控制开关。第一个开关,负责控制继续保存长期状态ccc;第二个开关,负责控制把即时状态输入到长期状态ccc;第三个开关,负责控制是否把长期状态ccc作为当前的LSTM的输出。三个开关的作用如下图所示:

接下来,我们要描述一下,输出h和单元状态c的具体计算方法。

二、长短时记忆网络的前向计算

前面描述的开关是怎样在算法中实现的呢?这就用到了门(gate)的概念。门实际上就是一层全连接层,它的输入是一个向量,输出是一个0到1之间的实数向量。假设WWW是门的权重向量,b\mathbf{b}b是偏置项,那么门可以表示为:g(x)=σ(Wx+b)g(x)=\sigma(W\mathbf{x}+\mathbf{b})g(x)=σ(Wx+b)门的使用,就是用门的输出向量按元素乘以我们需要控制的那个向量。因为门的输出是0到1之间的实数向量,那么,当门输出为0时,任何向量与之相乘都会得到0向量,这就相当于啥都不能通过;输出为1时,任何向量与之相乘都不会有任何改变,这就相当于啥都可以通过。因为σ\sigmaσ(也就是sigmoid函数)的值域是(0,1),所以门的状态都是半开半闭的。

LSTM用两个门来控制单元状态ccc的内容,一个是遗忘门(forget gate),它决定了上一时刻的单元状态ct−1\mathbf{c}_{t-1}ct−1​有多少保留到当前时刻ct\mathbf{c}_{t}ct​;另一个是输入门(input gate),它决定了当前时刻网络的输入xt\mathbf{x}_txt​有多少保存到单元状态ct\mathbf{c}_tct​。LSTM用输出门(output gate)来控制单元状态ct\mathbf{c}_tct​有多少输出到LSTM的当前输出值ht\mathbf{h}_tht​。

我们先来看一下遗忘门:ft=σ(Wf⋅[ht−1,xt]+bf)(式1)\mathbf{f}_t=\sigma(W_f·[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_f)\space\space\space\space\space\space(式1)ft​=σ(Wf​⋅[ht−1​,xt​]+bf​)      (式1)上式中,WfW_fWf​是遗忘门的权重矩阵,[ht−1,xt][\mathbf{h}_{t-1},\mathbf{x}_t][ht−1​,xt​]表示把两个向量连接成一个更长的向量,bf\mathbf{b}_fbf​是遗忘门的偏置项,σ\sigmaσ是sigmoid函数。如果输入的维度是dxd_xdx​,隐藏层的维度是dhd_hdh​,单元状态的维度是dcd_cdc​(通常dc=dhd_c=d_hdc​=dh​),则遗忘门的权重矩阵WfW_fWf​维度是dc×(dh+dx)d_c\times (d_h+d_x)dc​×(dh​+dx​)。事实上,权重矩阵WfW_fWf​都是两个矩阵拼接而成的:一个是WfhW_{fh}Wfh​,它对应着输入项ht−1\mathbf{h}_{t-1}ht−1​,其维度为dc×dhd_c\times d_hdc​×dh​;一个是WfxW_{fx}Wfx​,它对应着输入项xt\mathbf{x}_txt​,其维度为dc×dxd_c \times d_xdc​×dx​。WfW_fWf​可以写为:[Wf][ht−1xt]=[WfhWfx][ht−1xt][W_f]\begin{bmatrix} \mathbf{h}_{t-1} \\ \mathbf{x}_t \end{bmatrix}=\begin{bmatrix} W_{fh}\space\space W_{fx}\end{bmatrix}\begin{bmatrix} \mathbf{h}_{t-1} \\ \mathbf{x}_t\end{bmatrix}\space\space\space\space\space\space\space\space\space\space\space\space[Wf​][ht−1​xt​​]=[Wfh​  Wfx​​][ht−1​xt​​]            =Wfhht−1+Wfxxt\space\space\space\space\space\space\space=W_{fh}\mathbf{h}_{t-1}+W_{fx}\mathbf{x}_t       =Wfh​ht−1​+Wfx​xt​下图显示了遗忘门的计算:

接下来看看输入门:
it=σ(Wi⋅[ht−1,xt]+bi)(式2)\mathbf{i}_t=\sigma(W_i·[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_i)\space\space\space\space\space\space(式2)it​=σ(Wi​⋅[ht−1​,xt​]+bi​)      (式2)上式中,WiW_iWi​是输入门的权重矩阵,bi\mathbf{b}_ibi​是输入门的偏置项。下图表示了输入门的计算:

接下来,我们计算用于描述当前输入的单元状态c~t\tilde \mathbf{c}_tc~t​,它是根据上一次的输出和本次输入来计算的:c~t=tanh(Wc⋅[ht−1,xt]+bc)(式3)\tilde \mathbf{c}_t=tanh(W_c·[\mathbf{h}_{t-1},\mathbf{x}_t] + \mathbf{b}_c)\space\space\space\space\space\space(式3)c~t​=tanh(Wc​⋅[ht−1​,xt​]+bc​)      (式3)下图是c~t\tilde \mathbf{c}_tc~t​的计算:

现在,我们计算当前时刻的单元状态ct\mathbf{c}_tct​。它是由上一次的单元状态ct−1\mathbf{c}_{t-1}ct−1​按元素乘以遗忘门ftf_tft​,再用当前输入的单元状态c~t\tilde \mathbf{c}_tc~t​按元素乘以输入门iti_tit​,再将两个积加和产生的:ct=ft∘c−1+it∘c~t(式4)\mathbf{c}_t=f_t\circ \mathbf{c}_{-1}+i_t \circ \tilde \mathbf{c}_t\space\space\space\space\space\space(式4)ct​=ft​∘c−1​+it​∘c~t​      (式4)符号∘\circ∘表示按元素乘。下图是ct\mathbf{c}_tct​的计算:
这样,我们就把LSTM关于当前的记忆c~t\tilde \mathbf{c}_tc~t​和长期的记忆ct−1\mathbf{c}_{t-1}ct−1​组合在一起,形成了新的单元状态ct\mathbf{c}_tct​。由于遗忘门的控制,它可以保存很久很久之前的信息,由于输入门的控制,它又可以避免当前无关紧要的内容进入记忆。下面,我们要看看输出门,它控制了长期记忆对当前输出的影响:ot=σ(Wo⋅[ht−1,xt]+bo)(式5)\mathbf{o}_t=\sigma(W_o·[\mathbf{h}_{t-1}, \mathbf{x}_t]+\mathbf{b}_o)\space\space\space\space\space\space(式5)ot​=σ(Wo​⋅[ht−1​,xt​]+bo​)      (式5)下图表示输出门的计算:
LSTM最终的输出,是由输出门和单元状态共同确定的:ht=ot∘tanh(ct)(式6)\mathbf{h}_t=\mathbf{o}_t \circ tanh(\mathbf{c}_t)\space\space\space\space\space\space(式6)ht​=ot​∘tanh(ct​)      (式6)下图表示LSTM最终输出的计算:
(式1)到(式6)就是LSTM前向计算的全部公式。至此,我们就把LSTM前向计算讲完了。

三、长短时记忆网络的训练

熟悉我们这个系列文章的同学都清楚,训练部分往往比前向计算部分复杂多了。LSTM的前向计算都这么复杂,那么,可想而知,它的训练算法一定是非常非常复杂的。现在只有做几次深呼吸,再一头扎进公式海洋吧。

LSTM训练算法框架

LSTM的训练算法仍然是反向传播算法,对于这个算法,我们已经非常熟悉了。主要有下面三个步骤:

  1. 前向计算每个神经元的输出值,对于LSTM来说,即ft、it、ct、ot、ht\mathbf{f}_t、\mathbf{i}_t、\mathbf{c}_t、\mathbf{o}_t、\mathbf{h}_tft​、it​、ct​、ot​、ht​五个向量的值。计算方法已经在上一节中描述过了。
  2. 反向计算每个神经元的误差项δ\deltaδ值。与循环神经网络一样,LSTM误差项的反向传播也是包括两个方向:一个是沿时间的反向传播,即从当前t时刻开始,计算每个时刻的误差项;一个是将误差项向上一层传播。
  3. 根据相应的误差项,计算每个权重的梯度。
关于公式和符号的说明

首先,我们对推导中用到的一些公式、符号做一下必要的说明。

接下来的推导中,我们设定gate的激活函数为sigmoid函数,输出的激活函数为tanh函数。他们的导数分别为:
σ(z)=y=11+e−z\sigma(z)=y=\frac{1}{1+e^{-z}}σ(z)=y=1+e−z1​σ′(z)=y(1−y)\sigma'(z)=y(1-y)\space\space\space\space\space\space\space\spaceσ′(z)=y(1−y)        tanh(z)=y=ez−e−zez+e−ztanh(z)=y=\frac{e^z-e^{-z}}{e^z+e^{-z}}\space\space\space\spacetanh(z)=y=ez+e−zez−e−z​    tanh′(z)=1−y2tanh'(z)=1-y^2\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\spacetanh′(z)=1−y2                 从上面可以看出,sigmoid和tanh函数的导数都是原函数的函数。这样,我们一旦计算原函数的值,就可以用它来计算出导数的值。

LSTM需要学习的参数共有8组,分别是:遗忘门的权重矩阵WfW_fWf​和偏置项bf\mathbf{b}_fbf​、输入门的权重矩阵WiW_iWi​和偏置项bi\mathbf{b}_ibi​、输出门的权重矩阵WoW_oWo​和偏置项bo\mathbf{b}_obo​,以及计算单元状态的权重矩阵WcW_cWc​和偏置项bc\mathbf{b}_cbc​。因为权重矩阵的两部分在反向传播中使用不同的公式,因此在后续的推导中,权重矩阵Wf、Wi、Wo、WcW_f、W_i、W_o、W_cWf​、Wi​、Wo​、Wc​都将被写为分开的两个矩阵:Wfh、Wfx、Wih、Wix、Woh、Wox、Wch、WcxW_{fh}、W_{fx}、W_{ih}、W_{ix}、W_{oh}、W_{ox}、W_{ch}、W_{cx}Wfh​、Wfx​、Wih​、Wix​、Woh​、Wox​、Wch​、Wcx​。

我们解释一下按元素乘∘\circ∘符号。当∘\circ∘作用于两个向量时,运算如下:a∘b=[a1a2a3...an]∘[b1b2b3...bn]=[a1b1a2b2a3b3...anbn]a \circ b=\begin{bmatrix} a_1 \\ a_2\\ a_3\\...\\a_n \end{bmatrix} \circ \begin{bmatrix} b_1 \\ b_2\\ b_3\\...\\b_n \end{bmatrix} = \begin{bmatrix} a_1b_1 \\ a_2b_2\\ a_3b_3\\...\\a_nb_n \end{bmatrix}a∘b=⎣⎢⎢⎢⎢⎡​a1​a2​a3​...an​​⎦⎥⎥⎥⎥⎤​∘⎣⎢⎢⎢⎢⎡​b1​b2​b3​...bn​​⎦⎥⎥⎥⎥⎤​=⎣⎢⎢⎢⎢⎡​a1​b1​a2​b2​a3​b3​...an​bn​​⎦⎥⎥⎥⎥⎤​当∘\circ∘作用于一个向量和一个矩阵时,运算如下:a∘X=[a1a2a3...an]∘[x11x12...x1nx21x22...x2nx31x32...x3n...xn1xn2...xnn]a \circ X = \begin{bmatrix} a_1 \\ a_2\\ a_3\\...\\a_n \end{bmatrix} \circ \begin{bmatrix} x_{11} \space\space x_{12} \space...\space x_{1n}\\ x_{21}\space\space x_{22} \space...\space x_{2n}\\ x_{31}\space\space x_{32} \space...\space x_{3n}\\...\\x_{n1}\space\space x_{n2} \space...\space x_{nn} \end{bmatrix} a∘X=⎣⎢⎢⎢⎢⎡​a1​a2​a3​...an​​⎦⎥⎥⎥⎥⎤​∘⎣⎢⎢⎢⎢⎡​x11​  x12​ ... x1n​x21​  x22​ ... x2n​x31​  x32​ ... x3n​...xn1​  xn2​ ... xnn​​⎦⎥⎥⎥⎥⎤​=[a1x11a1x12...a1x1na2x21a2x22...a2x2na3x31a3x32...a3x3n...anxn1anxn2...anxnn]\space\space\space\space\space\space\space\space\space\space=\begin{bmatrix} a_1x_{11} \space\space a_1x_{12} \space...\space a_1x_{1n}\\ a_2x_{21}\space\space a_2x_{22} \space...\space a_2x_{2n}\\ a_3x_{31}\space\space a_3x_{32} \space...\space a_3x_{3n}\\...\\a_nx_{n1}\space\space a_nx_{n2} \space...\space a_nx_{nn} \end{bmatrix}           =⎣⎢⎢⎢⎢⎡​a1​x11​  a1​x12​ ... a1​x1n​a2​x21​  a2​x22​ ... a2​x2n​a3​x31​  a3​x32​ ... a3​x3n​...an​xn1​  an​xn2​ ... an​xnn​​⎦⎥⎥⎥⎥⎤​当∘\circ∘作用于两个矩阵时,两个矩阵对应位置的元素相乘。按元素乘可以在某些情况下简化矩阵和向量运算。例如,当一个对角矩阵右乘一个矩阵时,相当于用对角矩阵的对角线组成的向量按元素乘那个矩阵:diag[a]X=a∘Xdiag[a]X=a \circ Xdiag[a]X=a∘X当一个行向量右乘一个对角矩阵时,相当于这个行向量按元素乘那个矩阵对角线组成的向量:aTdiag[b]=a∘ba^Tdiag[b]=a \circ baTdiag[b]=a∘b上面这两点,在我们后续推导中会多次用到。

在t时刻,LSTM的输出值为ht\mathbf{h}_tht​。我们定义t时刻的误差项δt\delta_tδt​为:δt=def∂E∂ht\delta_t\overset{def}{=}\frac{\partial E}{\partial \mathbf{h}_t}δt​=def∂ht​∂E​注意,和前面几篇文章不同,我们这里假设误差项是损失函数对输出值的导数,而不是对加权输入nettlnet_t^lnettl​的导数。因为LSTM有四个加权输入,分别对应ft、it、ct、ot\mathbf{f}_t、\mathbf{i}_t、\mathbf{c}_t、\mathbf{o}_tft​、it​、ct​、ot​,我们希望往上一层传递一个误差项而不是四个。但我们仍然需要定义出这四个加权输入,以及他们对应的误差项。netf,t=Wf[ht−1,xt]+bfnet_{f,t}=W_f[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_fnetf,t​=Wf​[ht−1​,xt​]+bf​=Wfhht−1+Wfxxt+bf\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=W_{fh}\mathbf{h}_{t-1}+W_{fx}\mathbf{x}_t+\mathbf{b}_f                    =Wfh​ht−1​+Wfx​xt​+bf​neti,t=Wi[ht−1,xt]+binet_{i,t}=W_i[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_ineti,t​=Wi​[ht−1​,xt​]+bi​=Wihht−1+Wixxt+bi\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=W_{ih}\mathbf{h}_{t-1}+W_{ix}\mathbf{x}_t+\mathbf{b}_i                   =Wih​ht−1​+Wix​xt​+bi​netc~,t=Wc[ht−1,xt]+bcnet_{\tilde c,t}=W_c[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_cnetc~,t​=Wc​[ht−1​,xt​]+bc​=Wchht−1+Wcxxt+bc\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=W_{ch}\mathbf{h}_{t-1}+W_{cx}\mathbf{x}_t+\mathbf{b}_c                   =Wch​ht−1​+Wcx​xt​+bc​neto,t=Wo[ht−1,xt]+bonet_{o,t}=W_o[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_oneto,t​=Wo​[ht−1​,xt​]+bo​=Wohht−1+Woxxt+bo\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=W_{oh}\mathbf{h}_{t-1}+W_{ox}\mathbf{x}_t+\mathbf{b}_o                   =Woh​ht−1​+Wox​xt​+bo​δf,t=def∂E∂netf,t\delta_{f,t}\overset{def}{=}\frac{\partial E}{\partial net_{f,t}}δf,t​=def∂netf,t​∂E​δi,t=def∂E∂neti,t\delta_{i,t}\overset{def}{=}\frac{\partial E}{\partial net_{i,t}}δi,t​=def∂neti,t​∂E​δc~,t=def∂E∂netc~,t\delta_{\tilde c,t}\overset{def}{=}\frac{\partial E}{\partial net_{\tilde c,t}}δc~,t​=def∂netc~,t​∂E​δo,t=def∂E∂neto,t\delta_{o,t}\overset{def}{=}\frac{\partial E}{\partial net_{o,t}}δo,t​=def∂neto,t​∂E​

误差项沿时间的反向传递

沿时间反向传递误差项,就是要计算出t-1时刻的误差项δt−1\delta_{t-1}δt−1​。δt−1T=∂E∂ht−1\delta_{t-1}^T=\frac{\partial E}{\partial \mathbf{h}_{t-1}}δt−1T​=∂ht−1​∂E​=∂E∂ht∂ht∂ht−1\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=\frac{\partial E}{\partial \mathbf{h}_{t}}\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}}               =∂ht​∂E​∂ht−1​∂ht​​=δtT∂ht∂ht−1\space\space\space\space\space\space\space\space\space\space\space\space=\delta_t^T\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}}            =δtT​∂ht−1​∂ht​​我们知道,∂ht∂ht−1\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}}∂ht−1​∂ht​​是一个Jacobian矩阵。如果隐藏层h的维度是N的话,那么它就是一个N×NN\times NN×N矩阵。为了求出它,我们列出ht\mathbf{h}_tht​的计算公式,即前面的(式6)和(式4):ht=ot∘tanh(ct)\mathbf{h}_t=\mathbf{o}_t \circ tanh(\mathbf c_t)ht​=ot​∘tanh(ct​)ct=ft∘ct−1+it∘c~t\space\space\space\space\space\space\space\space\space\mathbf c_t=\mathbf{f}_t \circ \space \mathbf c_{t-1}+\mathbf{i}_t\circ \space \tilde \mathbf{c}_t         ct​=ft​∘ ct−1​+it​∘ c~t​显然,ot、ft、it、c~t\mathbf{o}_t、\mathbf{f}_t、\mathbf{i}_t、\mathbf{\tilde{c}}_tot​、ft​、it​、c~t​都是ht−1\mathbf{h}_{t-1}ht−1​的函数,那么,利用全导数公式可得:
δtT∂ht∂ht−1=δtT∂ht∂ot∂ot∂neto,t∂neto,t∂ht−1+δtT∂ht∂ct∂ct∂ft∂ft∂netf,t∂netf,t∂ht−1+δtT∂ht∂ct∂ct∂it∂it∂neti,t∂neti,t∂ht−1+δtT∂ht∂ct∂ct∂c~t∂c~t∂netc~,t∂netc~,t∂ht−1\delta_t^T\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{h_{t-1}}}}=\delta_t^T\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{o}_t}}\frac{\partial{\mathbf{o}_t}}{\partial{\mathbf{net}_{o,t}}}\frac{\partial{\mathbf{net}_{o,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_t^T\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{c}_t}}\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{f_{t}}}}\frac{\partial{\mathbf{f}_t}}{\partial{\mathbf{net}_{f,t}}}\frac{\partial{\mathbf{net}_{f,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_t^T\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{c}_t}}\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{i_{t}}}}\frac{\partial{\mathbf{i}_t}}{\partial{\mathbf{net}_{i,t}}}\frac{\partial{\mathbf{net}_{i,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_t^T\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{c}_t}}\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{\tilde{c}}_{t}}}\frac{\partial{\mathbf{\tilde{c}}_t}}{\partial{\mathbf{net}_{\tilde{c},t}}}\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{\mathbf{h_{t-1}}}}δtT​∂ht−1​∂ht​​=δtT​∂ot​∂ht​​∂neto,t​∂ot​​∂ht−1​∂neto,t​​+δtT​∂ct​∂ht​​∂ft​∂ct​​∂netf,t​∂ft​​∂ht−1​∂netf,t​​+δtT​∂ct​∂ht​​∂it​∂ct​​∂neti,t​∂it​​∂ht−1​∂neti,t​​+δtT​∂ct​∂ht​​∂c~t​∂ct​​∂netc~,t​∂c~t​​∂ht−1​∂netc~,t​​=δo,tT∂neto,t∂ht−1+δf,tT∂netf,t∂ht−1+δi,tT∂neti,t∂ht−1+δc~,tT∂netc~,t∂ht−1(式7)=\delta_{o,t}^T\frac{\partial{\mathbf{net}_{o,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{f,t}^T\frac{\partial{\mathbf{net}_{f,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{i,t}^T\frac{\partial{\mathbf{net}_{i,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{\tilde{c},t}^T\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{\mathbf{h_{t-1}}}}\qquad\quad(式7)=δo,tT​∂ht−1​∂neto,t​​+δf,tT​∂ht−1​∂netf,t​​+δi,tT​∂ht−1​∂neti,t​​+δc~,tT​∂ht−1​∂netc~,t​​(式7)下面,我们要把(式7)中的每个偏导数都求出来。根据(式6),我们可以求出:∂ht∂ot=diag[tanh⁡(ct)]\frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{o}_t}}=diag[\tanh(\mathbf{c}_t)]∂ot​∂ht​​=diag[tanh(ct​)]∂ht∂ct=diag[ot∘(1−tanh⁡(ct)2)]\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space \frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{c}_t}}=diag[\mathbf{o}_t\circ(1-\tanh(\mathbf{c}_t)^2)]                   ∂ct​∂ht​​=diag[ot​∘(1−tanh(ct​)2)]根据(式4),我们可以求出:∂ct∂ft=diag[ct−1]\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{f_{t}}}}=diag[\mathbf{c}_{t-1}]∂ft​∂ct​​=diag[ct−1​]∂ct∂it=diag[c~t]\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{i_{t}}}}=diag[\mathbf{\tilde{c}}_t]\space\space\space∂it​∂ct​​=diag[c~t​]   ∂ct∂c~t=diag[it]\frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{\tilde{c}_{t}}}}=diag[\mathbf{i}_t]\space\space\space\space∂c~t​∂ct​​=diag[it​]    因为:ot=σ(neto,t)\mathbf{o}_t=\sigma(\mathbf{net}_{o,t})\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\spaceot​=σ(neto,t​)                  neto,t=Wohht−1+Woxxt+bo\mathbf{net}_{o,t}=W_{oh}\mathbf{h}_{t-1}+W_{ox}\mathbf{x}_t+\mathbf{b}_oneto,t​=Woh​ht−1​+Wox​xt​+bo​ft=σ(netf,t)\mathbf{f}_t=\sigma(\mathbf{net}_{f,t})\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\spaceft​=σ(netf,t​)                 netf,t=Wfhht−1+Wfxxt+bf\mathbf{net}_{f,t}=W_{fh}\mathbf{h}_{t-1}+W_{fx}\mathbf{x}_t+\mathbf{b}_fnetf,t​=Wfh​ht−1​+Wfx​xt​+bf​it=σ(neti,t)\mathbf{i}_t=\sigma(\mathbf{net}_{i,t})\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\spaceit​=σ(neti,t​)                 neti,t=Wihht−1+Wixxt+bi\mathbf{net}_{i,t}=W_{ih}\mathbf{h}_{t-1}+W_{ix}\mathbf{x}_t+\mathbf{b}_ineti,t​=Wih​ht−1​+Wix​xt​+bi​c~t=tanh⁡(netc~,t)\mathbf{\tilde{c}}_t=\tanh(\mathbf{net}_{\tilde{c},t})\space\space\space\space\space\space\space\space\space\space\spacec~t​=tanh(netc~,t​)           netc~,t=Wchht−1+Wcxxt+bc\mathbf{net}_{\tilde{c},t}=W_{ch}\mathbf{h}_{t-1}+W_{cx}\mathbf{x}_t+\mathbf{b}_cnetc~,t​=Wch​ht−1​+Wcx​xt​+bc​我们很容易得出:∂ot∂neto,t=diag[ot∘(1−ot)]\frac{\partial{\mathbf{o}_t}}{\partial{\mathbf{net}_{o,t}}}=diag[\mathbf{o}_t\circ(1-\mathbf{o}_t)]∂neto,t​∂ot​​=diag[ot​∘(1−ot​)]∂neto,t∂ht−1=Woh\frac{\partial{\mathbf{net}_{o,t}}}{\partial{\mathbf{h_{t-1}}}}=W_{oh}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space∂ht−1​∂neto,t​​=Woh​                       ∂ft∂netf,t=diag[ft∘(1−ft)]\frac{\partial{\mathbf{f}_t}}{\partial{\mathbf{net}_{f,t}}}=diag[\mathbf{f}_t\circ(1-\mathbf{f}_t)]∂netf,t​∂ft​​=diag[ft​∘(1−ft​)]∂netf,t∂ht−1=Wfh\frac{\partial{\mathbf{net}_{f,t}}}{\partial{\mathbf{h}_{t-1}}}=W_{fh}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space∂ht−1​∂netf,t​​=Wfh​                      ∂it∂neti,t=diag[it∘(1−it)]\frac{\partial{\mathbf{i}_t}}{\partial{\mathbf{net}_{i,t}}}=diag[\mathbf{i}_t\circ(1-\mathbf{i}_t)]∂neti,t​∂it​​=diag[it​∘(1−it​)]∂neti,t∂ht−1=Wih\frac{\partial{\mathbf{net}_{i,t}}}{\partial{\mathbf{h}_{t-1}}}=W_{ih}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space∂ht−1​∂neti,t​​=Wih​                     ∂c~t∂netc~,t=diag[1−c~t2]\frac{\partial{\mathbf{\tilde{c}}_t}}{\partial{\mathbf{net}_{\tilde{c},t}}}=diag[1-\mathbf{\tilde{c}}_t^2]\space\space\space\space\space\space\space\space∂netc~,t​∂c~t​​=diag[1−c~t2​]        ∂netc~,t∂ht−1=Wch\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{\mathbf{h}_{t-1}}}=W_{ch}\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space∂ht−1​∂netc~,t​​=Wch​                   将上述偏导数带入到(式7),我们得到:δt−1=δo,tT∂neto,t∂ht−1+δf,tT∂netf,t∂ht−1+δi,tT∂neti,t∂ht−1+δc~,tT∂netc~,t∂ht−1\delta_{t-1}=\delta_{o,t}^T\frac{\partial{\mathbf{net}_{o,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{f,t}^T\frac{\partial{\mathbf{net}_{f,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{i,t}^T\frac{\partial{\mathbf{net}_{i,t}}}{\partial{\mathbf{h_{t-1}}}} +\delta_{\tilde{c},t}^T\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{\mathbf{h_{t-1}}}}δt−1​=δo,tT​∂ht−1​∂neto,t​​+δf,tT​∂ht−1​∂netf,t​​+δi,tT​∂ht−1​∂neti,t​​+δc~,tT​∂ht−1​∂netc~,t​​=δo,tTWoh+δf,tTWfh+δi,tTWih+δc~,tTWch(式8)\space\space\space\space\space=\delta_{o,t}^T W_{oh} +\delta_{f,t}^TW_{fh} +\delta_{i,t}^TW_{ih} +\delta_{\tilde{c},t}^TW_{ch}\qquad\quad(式8)     =δo,tT​Woh​+δf,tT​Wfh​+δi,tT​Wih​+δc~,tT​Wch​(式8)根据δo,t、δf,t、δi,t、δc~,t\delta_{o,t}、\delta_{f,t}、\delta_{i,t}、\delta_{\tilde{c},t}δo,t​、δf,t​、δi,t​、δc~,t​的定义,可知:δo,tT=δtT∘tanh⁡(ct)∘ot∘(1−ot)(式9)\delta_{o,t}^T=\delta_t^T\circ\tanh(\mathbf{c}_t)\circ\mathbf{o}_t\circ(1-\mathbf{o}_t)\qquad\quad(式9)\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\spaceδo,tT​=δtT​∘tanh(ct​)∘ot​∘(1−ot​)(式9)                       δf,tT=δtT∘ot∘(1−tanh⁡(ct)2)∘ct−1∘ft∘(1−ft)(式10)\space\space\space \delta_{f,t}^T=\delta_t^T\circ\mathbf{o}_t\circ(1-\tanh(\mathbf{c}_t)^2)\circ\mathbf{c}_{t-1}\circ\mathbf{f}_t\circ(1-\mathbf{f}_t)\qquad(式10)   δf,tT​=δtT​∘ot​∘(1−tanh(ct​)2)∘ct−1​∘ft​∘(1−ft​)(式10)δi,tT=δtT∘ot∘(1−tanh⁡(ct)2)∘c~t∘it∘(1−it)(式11)\space\space\space \delta_{i,t}^T=\delta_t^T\circ\mathbf{o}_t\circ(1-\tanh(\mathbf{c}_t)^2)\circ\mathbf{\tilde{c}}_t\circ\mathbf{i}_t\circ(1-\mathbf{i}_t)\qquad\quad(式11)   δi,tT​=δtT​∘ot​∘(1−tanh(ct​)2)∘c~t​∘it​∘(1−it​)(式11)δc~,tT=δtT∘ot∘(1−tanh⁡(ct)2)∘it∘(1−c~2)(式12)\delta_{\tilde{c},t}^T=\delta_t^T\circ\mathbf{o}_t\circ(1-\tanh(\mathbf{c}_t)^2)\circ\mathbf{i}_t\circ(1-\mathbf{\tilde{c}}^2)\qquad\quad(式12)\space\space\space\spaceδc~,tT​=δtT​∘ot​∘(1−tanh(ct​)2)∘it​∘(1−c~2)(式12)    (式8)到(式12)就是将误差沿时间反向传播一个时刻的公式。有了它,我们可以写出将误差项向前传递到任意k时刻的公式:δkT=∏j=kt−1δo,jTWoh+δf,jTWfh+δi,jTWih+δc~,jTWch(式13)\delta_k^T=\prod_{j=k}^{t-1}\delta_{o,j}^TW_{oh} +\delta_{f,j}^TW_{fh} +\delta_{i,j}^TW_{ih} +\delta_{\tilde{c},j}^TW_{ch}\qquad\quad(式13)δkT​=j=k∏t−1​δo,jT​Woh​+δf,jT​Wfh​+δi,jT​Wih​+δc~,jT​Wch​(式13)

将误差项传递到上一层

我们假设当前为第lll层,定义l−1l-1l−1层的误差项是误差函数对l−1l-1l−1层加权输入的导数,即:δtl−1=def∂Enettl−1\delta_t^{l-1}\overset{def}{=}\frac{\partial{E}}{\mathbf{net}_t^{l-1}}δtl−1​=defnettl−1​∂E​本次LSTM的输入xtx_txt​由下面的公式计算:xtl=fl−1(nettl−1)\mathbf{x}_t^l=f^{l-1}(\mathbf{net}_t^{l-1})xtl​=fl−1(nettl−1​)上式中,fl−1f^{l-1}fl−1表示第l−1l-1l−1层的激活函数。

因为netf,tl、neti,tl、netc~,tl、neto,tl\mathbf{net}_{f,t}^l、\mathbf{net}_{i,t}^l、\mathbf{net}_{\tilde{c},t}^l、\mathbf{net}_{o,t}^lnetf,tl​、neti,tl​、netc~,tl​、neto,tl​都是xtx_txt​的函数,xtx_txt​又是nettl−1net_t^{l-1}nettl−1​的函数,因此,要求出EEE对nettl−1net_t^{l-1}nettl−1​的导数,就需要使用全导数公式:∂E∂nettl−1=∂E∂netf,tl∂netf,tl∂xtl∂xtl∂nettl−1+∂E∂neti,tl∂neti,tl∂xtl∂xtl∂nettl−1+∂E∂netc~,tl∂netc~,tl∂xtl∂xtl∂nettl−1+∂E∂neto,tl∂neto,tl∂xtl∂xtl∂nettl−1\frac{\partial{E}}{\partial{\mathbf{net}_t^{l-1}}}=\frac{\partial{E}}{\partial{\mathbf{\mathbf{net}_{f,t}^l}}}\frac{\partial{\mathbf{\mathbf{net}_{f,t}^l}}}{\partial{\mathbf{x}_t^l}}\frac{\partial{\mathbf{x}_t^l}}{\partial{\mathbf{\mathbf{net}_t^{l-1}}}} +\frac{\partial{E}}{\partial{\mathbf{\mathbf{net}_{i,t}^l}}}\frac{\partial{\mathbf{\mathbf{net}_{i,t}^l}}}{\partial{\mathbf{x}_t^l}}\frac{\partial{\mathbf{x}_t^l}}{\partial{\mathbf{\mathbf{net}_t^{l-1}}}} +\frac{\partial{E}}{\partial{\mathbf{\mathbf{net}_{\tilde{c},t}^l}}}\frac{\partial{\mathbf{\mathbf{net}_{\tilde{c},t}^l}}}{\partial{\mathbf{x}_t^l}}\frac{\partial{\mathbf{x}_t^l}}{\partial{\mathbf{\mathbf{net}_t^{l-1}}}} +\frac{\partial{E}}{\partial{\mathbf{\mathbf{net}_{o,t}^l}}}\frac{\partial{\mathbf{\mathbf{net}_{o,t}^l}}}{\partial{\mathbf{x}_t^l}}\frac{\partial{\mathbf{x}_t^l}}{\partial{\mathbf{\mathbf{net}_t^{l-1}}}}∂nettl−1​∂E​=∂netf,tl​∂E​∂xtl​∂netf,tl​​∂nettl−1​∂xtl​​+∂neti,tl​∂E​∂xtl​∂neti,tl​​∂nettl−1​∂xtl​​+∂netc~,tl​∂E​∂xtl​∂netc~,tl​​∂nettl−1​∂xtl​​+∂neto,tl​∂E​∂xtl​∂neto,tl​​∂nettl−1​∂xtl​​=δf,tTWfx∘f′(nettl−1)+δi,tTWix∘f′(nettl−1)+δc~,tTWcx∘f′(nettl−1)+δo,tTWox∘f′(nettl−1)=\delta_{f,t}^TW_{fx}\circ f'(\mathbf{net}_t^{l-1})+\delta_{i,t}^TW_{ix}\circ f'(\mathbf{net}_t^{l-1})+\delta_{\tilde{c},t}^TW_{cx}\circ f'(\mathbf{net}_t^{l-1})+\delta_{o,t}^TW_{ox}\circ f'(\mathbf{net}_t^{l-1})=δf,tT​Wfx​∘f′(nettl−1​)+δi,tT​Wix​∘f′(nettl−1​)+δc~,tT​Wcx​∘f′(nettl−1​)+δo,tT​Wox​∘f′(nettl−1​)=(δf,tTWfx+δi,tTWix+δc~,tTWcx+δo,tTWox)∘f′(nettl−1)(式14)=(\delta_{f,t}^TW_{fx}+\delta_{i,t}^TW_{ix}+\delta_{\tilde{c},t}^TW_{cx}+\delta_{o,t}^TW_{ox})\circ f'(\mathbf{net}_t^{l-1})\qquad\quad(式14)=(δf,tT​Wfx​+δi,tT​Wix​+δc~,tT​Wcx​+δo,tT​Wox​)∘f′(nettl−1​)(式14)(式14)就是将误差传递到上一层的公式。

权重梯度的计算

对于Wft、Wih、Wch、WohW_{ft}、W_{ih}、W_{ch}、W_{oh}Wft​、Wih​、Wch​、Woh​的权重梯度,我们知道它的梯度是各个时刻梯度之和(证明过程请参考文章《深度学习之循环神经网络(RNN)》),我们首先求出它们在t时刻的梯度,然后再求出他们最终的梯度。

我们已经求得了误差项δo,t、δf,t、δi,t、δc~,t\delta_{o,t}、\delta_{f,t}、\delta_{i,t}、\delta_{\tilde c,t}δo,t​、δf,t​、δi,t​、δc~,t​,很容易求出t时刻的Woh、Wih、Wfh、WchW_{oh}、W_{ih}、W_{fh}、W_{ch}Woh​、Wih​、Wfh​、Wch​:∂E∂Woh,t=∂E∂neto,t∂neto,t∂Woh,t\frac{\partial{E}}{\partial{W_{oh,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{o,t}}}\frac{\partial{\mathbf{net}_{o,t}}}{\partial{W_{oh,t}}}∂Woh,t​∂E​=∂neto,t​∂E​∂Woh,t​∂neto,t​​=δo,tht−1T=\delta_{o,t}\mathbf{h}_{t-1}^T=δo,t​ht−1T​∂E∂Wfh,t=∂E∂netf,t∂netf,t∂Wfh,t\frac{\partial{E}}{\partial{W_{fh,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{f,t}}}\frac{\partial{\mathbf{net}_{f,t}}}{\partial{W_{fh,t}}}∂Wfh,t​∂E​=∂netf,t​∂E​∂Wfh,t​∂netf,t​​=δf,tht−1T=\delta_{f,t}\mathbf{h}_{t-1}^T=δf,t​ht−1T​∂E∂Wih,t=∂E∂neti,t∂neti,t∂Wih,t\frac{\partial{E}}{\partial{W_{ih,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{i,t}}}\frac{\partial{\mathbf{net}_{i,t}}}{\partial{W_{ih,t}}}∂Wih,t​∂E​=∂neti,t​∂E​∂Wih,t​∂neti,t​​=δi,tht−1T=\delta_{i,t}\mathbf{h}_{t-1}^T=δi,t​ht−1T​∂E∂Wch,t=∂E∂netc~,t∂netc~,t∂Wch,t\frac{\partial{E}}{\partial{W_{ch,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{\tilde{c},t}}}\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{W_{ch,t}}}∂Wch,t​∂E​=∂netc~,t​∂E​∂Wch,t​∂netc~,t​​=δc~,tht−1T=\delta_{\tilde{c},t}\mathbf{h}_{t-1}^T=δc~,t​ht−1T​将各个时刻的梯度加在一起,就能得到最终的梯度:∂E∂Woh=∑j=1tδo,jhj−1T\frac{\partial{E}}{\partial{W_{oh}}}=\sum_{j=1}^t\delta_{o,j}\mathbf{h}_{j-1}^T∂Woh​∂E​=j=1∑t​δo,j​hj−1T​∂E∂Wfh=∑j=1tδf,jhj−1T\frac{\partial{E}}{\partial{W_{fh}}}=\sum_{j=1}^t\delta_{f,j}\mathbf{h}_{j-1}^T∂Wfh​∂E​=j=1∑t​δf,j​hj−1T​∂E∂Wih=∑j=1tδi,jhj−1T\frac{\partial{E}}{\partial{W_{ih}}}=\sum_{j=1}^t\delta_{i,j}\mathbf{h}_{j-1}^T∂Wih​∂E​=j=1∑t​δi,j​hj−1T​∂E∂Wch=∑j=1tδc~,jhj−1T\frac{\partial{E}}{\partial{W_{ch}}}=\sum_{j=1}^t\delta_{\tilde{c},j}\mathbf{h}_{j-1}^T∂Wch​∂E​=j=1∑t​δc~,j​hj−1T​对于偏置项bf、bi、bc、bo\mathbf{b}_f、\mathbf{b}_i、\mathbf{b}_c、\mathbf{b}_obf​、bi​、bc​、bo​的梯度,也是将各个时刻的梯度加在一起。下面是各个时刻的偏置项梯度:∂E∂bo,t=∂E∂neto,t∂neto,t∂bo,t\frac{\partial{E}}{\partial{\mathbf{b}_{o,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{o,t}}}\frac{\partial{\mathbf{net}_{o,t}}}{\partial{\mathbf{b}_{o,t}}}∂bo,t​∂E​=∂neto,t​∂E​∂bo,t​∂neto,t​​=δo,t=\delta_{o,t}\space\space\space\space\space\space\space\space\space\space=δo,t​          ∂E∂bf,t=∂E∂netf,t∂netf,t∂bf,t\frac{\partial{E}}{\partial{\mathbf{b}_{f,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{f,t}}}\frac{\partial{\mathbf{net}_{f,t}}}{\partial{\mathbf{b}_{f,t}}}∂bf,t​∂E​=∂netf,t​∂E​∂bf,t​∂netf,t​​=δf,t=\delta_{f,t}\space\space\space\space\space\space\space\space\space\space=δf,t​          ∂E∂bi,t=∂E∂neti,t∂neti,t∂bi,t\frac{\partial{E}}{\partial{\mathbf{b}_{i,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{i,t}}}\frac{\partial{\mathbf{net}_{i,t}}}{\partial{\mathbf{b}_{i,t}}}∂bi,t​∂E​=∂neti,t​∂E​∂bi,t​∂neti,t​​=δi,t=\delta_{i,t}\space\space\space\space\space\space\space\space\space\space=δi,t​          ∂E∂bc,t=∂E∂netc~,t∂netc~,t∂bc,t\frac{\partial{E}}{\partial{\mathbf{b}_{c,t}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{\tilde{c},t}}}\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{\mathbf{b}_{c,t}}}∂bc,t​∂E​=∂netc~,t​∂E​∂bc,t​∂netc~,t​​=δc~,t=\delta_{\tilde{c},t}\space\space\space\space\space\space\space\space\space\space=δc~,t​          下面是最终的偏置项梯度,即将各个时刻的偏置项梯度加在一起:∂E∂bo=∑j=1tδo,j\frac{\partial{E}}{\partial{\mathbf{b}_o}}=\sum_{j=1}^t\delta_{o,j}∂bo​∂E​=j=1∑t​δo,j​∂E∂bi=∑j=1tδi,j\frac{\partial{E}}{\partial{\mathbf{b}_i}}=\sum_{j=1}^t\delta_{i,j}∂bi​∂E​=j=1∑t​δi,j​∂E∂bf=∑j=1tδf,j\frac{\partial{E}}{\partial{\mathbf{b}_f}}=\sum_{j=1}^t\delta_{f,j}∂bf​∂E​=j=1∑t​δf,j​∂E∂bc=∑j=1tδc~,j\frac{\partial{E}}{\partial{\mathbf{b}_c}}=\sum_{j=1}^t\delta_{\tilde{c},j}∂bc​∂E​=j=1∑t​δc~,j​对于Wfx、Wix、Wcx、WoxW_{fx}、W_{ix}、W_{cx}、W_{ox}Wfx​、Wix​、Wcx​、Wox​的权重梯度,只需要根据相应的误差项直接计算即可:∂E∂Wox=∂E∂neto,t∂neto,t∂Wox\frac{\partial{E}}{\partial{W_{ox}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{o,t}}}\frac{\partial{\mathbf{net}_{o,t}}}{\partial{W_{ox}}}∂Wox​∂E​=∂neto,t​∂E​∂Wox​∂neto,t​​=δo,txtT=\delta_{o,t}\mathbf{x}_{t}^T\space\space\space\space\space=δo,t​xtT​     ∂E∂Wfx=∂E∂netf,t∂netf,t∂Wfx\frac{\partial{E}}{\partial{W_{fx}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{f,t}}}\frac{\partial{\mathbf{net}_{f,t}}}{\partial{W_{fx}}}∂Wfx​∂E​=∂netf,t​∂E​∂Wfx​∂netf,t​​=δf,txtT=\delta_{f,t}\mathbf{x}_{t}^T\space\space\space\space\space=δf,t​xtT​     ∂E∂Wix=∂E∂neti,t∂neti,t∂Wix\frac{\partial{E}}{\partial{W_{ix}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{i,t}}}\frac{\partial{\mathbf{net}_{i,t}}}{\partial{W_{ix}}}∂Wix​∂E​=∂neti,t​∂E​∂Wix​∂neti,t​​=δi,txtT=\delta_{i,t}\mathbf{x}_{t}^T\space\space\space\space\space=δi,t​xtT​     ∂E∂Wcx=∂E∂netc~,t∂netc~,t∂Wcx\frac{\partial{E}}{\partial{W_{cx}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{\tilde{c},t}}}\frac{\partial{\mathbf{net}_{\tilde{c},t}}}{\partial{W_{cx}}}∂Wcx​∂E​=∂netc~,t​∂E​∂Wcx​∂netc~,t​​=δc~,txtT=\delta_{\tilde{c},t}\mathbf{x}_{t}^T\space\space\space\space\space=δc~,t​xtT​     
以上就是LSTM的训练算法的全部公式。因为这里面存在很多重复的模式,仔细看看,会发觉并不是太复杂。

当然,LSTM存在着相当多的变体,读者可以在互联网上找到很多资料。因为大家已经熟悉了基本LSTM的算法,因此理解这些变体比较容易,因此本文就不再赘述了。

四、GRU

前面我们讲了一种普通的LSTM,事实上LSTM存在很多变体,许多论文中的LSTM都或多或少的不太一样。在众多的LSTM变体中,**GRU (Gated Recurrent Unit)**也许是最成功的一种。它对LSTM做了很多简化,同时却保持着和LSTM相同的效果。因此,GRU最近变得越来越流行。

GRU对LSTM做了两个大改动:

  1. 将输入门、遗忘门、输出门变为两个门:更新门(Update Gate)zt\mathbf{z}_tzt​和重置门(Reset Gate)rt\mathbf{r}_trt​。
  2. 将单元状态与输出合并为一个状态:h\mathbf{h}h。

GRU的前向计算公式为:zt=σ(Wz⋅[ht−1,xt])\mathbf{z}_t=\sigma(W_z\cdot[\mathbf{h}_{t-1},\mathbf{x}_t])zt​=σ(Wz​⋅[ht−1​,xt​])rt=σ(Wr⋅[ht−1,xt])\mathbf{r}_t=\sigma(W_r\cdot[\mathbf{h}_{t-1},\mathbf{x}_t])rt​=σ(Wr​⋅[ht−1​,xt​])h~t=tanh⁡(W⋅[rt∘ht−1,xt])\mathbf{\tilde{h}}_t=\tanh(W\cdot[\mathbf{r}_t\circ\mathbf{h}_{t-1},\mathbf{x}_t])h~t​=tanh(W⋅[rt​∘ht−1​,xt​])h=(1−zt)∘ht−1+zt∘h~t\mathbf{h}=(1-\mathbf{z}_t)\circ\mathbf{h}_{t-1}+\mathbf{z}_t\circ\mathbf{\tilde{h}}_th=(1−zt​)∘ht−1​+zt​∘h~t​下图是GRU的示意图:
GRU的训练算法比LSTM简单一些,留给读者自行推导,本文就不再赘述了。

五、小结

至此,LSTM——也许是结构最复杂的一类神经网络——就讲完了,相信拿下前几篇文章的读者们搞定这篇文章也不在话下吧!现在我们已经了解循环神经网络和它最流行的变体——LSTM,它们都可以用来处理序列。但是,有时候仅仅拥有处理序列的能力还不够,还需要处理比序列更为复杂的结构(比如树结构),这时候就需要用到另外一类网络:递归神经网络(Recursive Neural Network),巧合的是,它的缩写也是RNN

深度学习之长短时记忆网络(LSTM)相关推荐

  1. 深度学习(7) - 长短时记忆网络(LSTM)

    长短时记忆网络是啥 我们首先了解一下长短时记忆网络产生的背景.回顾一下零基础入门深度学习(5) - 循环神经网络中推导的,误差项沿时间反向传播的公式: 我们可以根据下面的不等式,来获取的模的上界(模可 ...

  2. 长短时记忆神经网络python代码_零基础入门深度学习(6) - 长短时记忆网络(LSTM)

    无论即将到来的是大数据时代还是人工智能时代,亦或是传统行业使用人工智能在云上处理大数据的时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的技术,会不会感觉马上就o ...

  3. 小常识10: 循环神经网络(RNN)与长短时记忆网络LSTM简介。

    小常识10:  循环神经网络(RNN)与长短时记忆网络LSTM简介. 本文目的:在计算机视觉(CV)中,CNN 通过局部连接/权值共享/池化操作/多层次结构逐层自动的提取特征,适应于处理如图片类的网格 ...

  4. 【深度学习理论】(7) 长短时记忆网络 LSTM

    大家好,今天和各位分享一下长短时记忆网络 LSTM 的原理,并使用 Pytorch 从公式上实现 LSTM 层 上一节介绍了循环神经网络 RNN,感兴趣的可以看一下:https://blog.csdn ...

  5. 长短时记忆网络(LSTM)的训练

    长短时记忆网络的训练 熟悉我们这个系列文章的同学都清楚,训练部分往往比前向计算部分复杂多了.LSTM的前向计算都这么复杂,那么,可想而知,它的训练算法一定是非常非常复杂的.现在只有做几次深呼吸,再一头 ...

  6. 长短时记忆网络LSTM

    网络介绍 长短时记忆网络(Long short time memory network, LSTM)是RNN的重要变体,解决了RNN无法长距离依赖的问题,同时缓了RNN的梯度爆炸问题.LSTM由遗忘门 ...

  7. 长短时记忆网络(LSTM)部分组件(六)

    在前面的几篇文章中试着实现了CNN,RNN的一些组件,这里继续学习LSTM,也是是实现部分组件,旨在学习其LSTM的原理. 具体参考: https://www.zybuluo.com/hanbingt ...

  8. 深度学习代码实战演示_Tensorflow_卷积神经网络CNN_循环神经网络RNN_长短时记忆网络LSTM_对抗生成网络GAN

    前言 经过大半年断断续续的学习和实践,终于将深度学习的基础知识看完了,虽然还有很多比较深入的内容没有涉及到,但也是感觉收获满满.因为是断断续续的学习做笔记写代码跑实验,所以笔记也零零散散的散落在每个角 ...

  9. 3分钟带你理解深度学习中的RNN和LSTM究竟是什么?

    https://www.toutiao.com/a6688658948276355596/ 摘要 本文将介绍一种常用的神经网络-循环神经网络(Recurrent Neural Network,RNN) ...

最新文章

  1. 【转】apache常用配置
  2. 前沿|《细胞》:绕过眼睛植入幻觉,科学家成功在盲人脑海中呈现指定图像!...
  3. 【RPC服务器不可用】解决方法
  4. Requirement already satisfied 解决方法
  5. php输出json html,html怎样格式化输出JSON数据
  6. asp.net的get和post请求
  7. RMAN备份恢复报错 ORA-19573: cannot obtain exclusive enqueue for datafile 5
  8. D类IP地址和组播传输
  9. c语言五子棋人机对弈算法_从零开始编写C语言五子棋程序1
  10. 团队开发——第一次冲刺第7天
  11. 手机号段对应地区编码_什么是编码器?了解两种类型的编码器
  12. xcode6 使用MJRefresh
  13. Illustrator 教程,如何在 Illustrator 中创建标签?
  14. 使用netmeeting进行网络培训
  15. 创业公司的技术 Leader,在 100offer 寻找怎样的工作机会?
  16. 计算机为什么获取mac地址,电脑获取mac地址失败怎么办
  17. SAE上使用cron定时发微博
  18. 社区计算机知识,社区公共基础知识备考指导——计算机知识
  19. 机器学习_吴恩达-总
  20. ROT13 - 维基百科,自由的百科全书

热门文章

  1. Sp6 PLL and PLL_DRP使用技巧小结
  2. [地心游记]探讨以大客户为核心的增值体系
  3. Windows环境下个别Gem包安装不上的解决办法
  4. 如何使用查找功能从 Mac、iPhone、iPad、iPod touch、Apple Watch 或 iCloud共享位置?
  5. 论文解读:Dynamic Connected Networks for Chinese Spelling Check
  6. 64位Ubuntu无法运行Adnroid SDK adb命令
  7. java实现商品的分类_电商网站首页商品分类列表功能实现
  8. Unreal多播委托
  9. 深度学习实战(2):肺炎预测|附数据集与源码
  10. 计算机软件著作权取得方式,软件著作权取得方式