深度学习之长短时记忆网络(LSTM)

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

一、长短时记忆网络是啥

我们首先了解一下长短时记忆网络产生的背景。回顾一下《深度学习之循环神经网络(RNN)》中推导的,误差项沿时间反向传播的公式: δ k T = δ t T ∏ i = k t − 1 d i a g [ f ′ ( n e t i ) ] W \delta_k^T= \delta_t^T\prod_{i=k}^{t-1}diag[f'(net_i)]W δkT=δtTi=kt1diag[f(neti)]W我们可以根据下面的不等式,来获取 δ k T \delta_k^T δkT的模的上界(模可以看做对 δ k T \delta_k^T δkT中每一项值的大小的度量): ∥ δ k T ∥ ⩽ ∥ δ t T ∥ ∏ i = k t − 1 ∥ W ∥ ∥ d i a g [ f ′ ( n e t i ) ] ∥ \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δtTi=kt1Wdiag[f(neti)] ⩽ ∥ δ t T ∥ ( β 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)tk             我们可以看到,误差项 δ \delta δ从t时刻传递到k时刻,其值的上界是 β f β w \beta_f\beta_w βfβw的指数函数。 β f β w \beta_f\beta_w βfβw分别是对角矩阵 d i a g [ f ′ ( n e t i ) ] diag[f'(net_i)] diag[f(neti)]和矩阵 W W W模的上界。显然,除非 β 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最终的梯度是各个时刻的梯度之和,即: ∇ W E = ∑ i = 1 t ∇ W t E                           \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 WE=i=1tWtE                                                 = ∇ W t E + ∇ W t − 1 E + . . . + ∇ W 1 E \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                       =WtE+Wt1E+...+W1E假设某轮训练中,各时刻的梯度以及最终的梯度之和如下图:在这里插入图片描述
我们就可以看到,从上图的t-3时刻开始,梯度已经几乎减少到0了。那么,从这个时刻开始再往之前走,得到的梯度(几乎为零)就不会对最终的梯度值有任何贡献,这就相当于无论t-3时刻之前的网络状态h是什么,在训练中都不会对权重数组W的更新产生影响,也就是网络事实上已经忽略了t-3时刻之前的状态。这就是原始RNN无法处理长距离依赖的原因。

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

其实,长短时记忆网络的思路比较简单。原始RNN的隐藏层只有一个状态,即h,它对于短期的输入非常敏感。那么,假如我们再增加一个状态,即c,让它来保存长期的状态,那么问题不就解决了么?如下图所示:
在这里插入图片描述
新增加的状态c,称为单元状态(cell state)。我们把上图按照时间维度展开:
在这里插入图片描述
上图仅仅是一个示意图,我们可以看出,在t时刻,LSTM的输入有三个:当前时刻网络的输入值 x t \mathbf{x}_t xt、上一时刻LSTM的输出值 h t − 1 \mathbf{h}_{t-1} ht1、以及上一时刻的单元状态 c t − 1 \mathbf{c}_{t-1} ct1;LSTM的输出有两个:当前时刻LSTM输出值 h t \mathbf{h}_{t} ht、和当前时刻的单元状态 c t \mathbf{c}_{t} ct。注意 x 、 h 、 c \mathbf{x}、\mathbf{h}、\mathbf{c} xhc都是向量

LSTM的关键,就是怎样控制长期状态 c c c。在这里,LSTM的思路是使用三个控制开关。第一个开关,负责控制继续保存长期状态 c c c;第二个开关,负责控制把即时状态输入到长期状态 c c c;第三个开关,负责控制是否把长期状态 c c c作为当前的LSTM的输出。三个开关的作用如下图所示:
在这里插入图片描述
接下来,我们要描述一下,输出h和单元状态c的具体计算方法。

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

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

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

我们先来看一下遗忘门: f t = σ ( W f ⋅ [ h t − 1 , x t ] + b f )        ( 式 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[ht1,xt]+bf)      (1)上式中, W f W_f Wf是遗忘门的权重矩阵, [ h t − 1 , x t ] [\mathbf{h}_{t-1},\mathbf{x}_t] [ht1,xt]表示把两个向量连接成一个更长的向量, b f \mathbf{b}_f bf是遗忘门的偏置项, σ \sigma σ是sigmoid函数。如果输入的维度是 d x d_x dx,隐藏层的维度是 d h d_h dh,单元状态的维度是 d c d_c dc(通常 d c = d h d_c=d_h dc=dh),则遗忘门的权重矩阵 W f W_f Wf维度是 d c × ( d h + d x ) d_c\times (d_h+d_x) dc×(dh+dx)。事实上,权重矩阵 W f W_f Wf都是两个矩阵拼接而成的:一个是 W f h W_{fh} Wfh,它对应着输入项 h t − 1 \mathbf{h}_{t-1} ht1,其维度为 d c × d h d_c\times d_h dc×dh;一个是 W f x W_{fx} Wfx,它对应着输入项 x t \mathbf{x}_t xt,其维度为 d c × d x d_c \times d_x dc×dx W f W_f Wf可以写为: [ W f ] [ h t − 1 x t ] = [ W f h    W f x ] [ h t − 1 x t ]              [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][ht1xt]=[Wfh  Wfx][ht1xt]                     = W f h h t − 1 + W f x x t \space\space\space\space\space\space\space=W_{fh}\mathbf{h}_{t-1}+W_{fx}\mathbf{x}_t        =Wfhht1+Wfxxt下图显示了遗忘门的计算:
在这里插入图片描述
接下来看看输入门:
i t = σ ( W i ⋅ [ h t − 1 , x t ] + b i )        ( 式 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[ht1,xt]+bi)      (2)上式中, W i W_i Wi是输入门的权重矩阵, b i \mathbf{b}_i bi是输入门的偏置项。下图表示了输入门的计算:
在这里插入图片描述
接下来,我们计算用于描述当前输入的单元状态 c ~ t \tilde \mathbf{c}_t c~t,它是根据上一次的输出和本次输入来计算的: c ~ t = t a n h ( W c ⋅ [ h t − 1 , x t ] + b c )        ( 式 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[ht1,xt]+bc)      (3)下图是 c ~ t \tilde \mathbf{c}_t c~t的计算:
在这里插入图片描述
现在,我们计算当前时刻的单元状态 c t \mathbf{c}_t ct。它是由上一次的单元状态 c t − 1 \mathbf{c}_{t-1} ct1按元素乘以遗忘门 f t f_t ft,再用当前输入的单元状态 c ~ t \tilde \mathbf{c}_t c~t按元素乘以输入门 i t i_t it,再将两个积加和产生的: c t = f t ∘ c − 1 + i t ∘ 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=ftc1+itc~t      (4)符号 ∘ \circ 表示按元素乘。下图是 c t \mathbf{c}_t ct的计算:在这里插入图片描述
这样,我们就把LSTM关于当前的记忆 c ~ t \tilde \mathbf{c}_t c~t和长期的记忆 c t − 1 \mathbf{c}_{t-1} ct1组合在一起,形成了新的单元状态 c t \mathbf{c}_t ct。由于遗忘门的控制,它可以保存很久很久之前的信息,由于输入门的控制,它又可以避免当前无关紧要的内容进入记忆。下面,我们要看看输出门,它控制了长期记忆对当前输出的影响: o t = σ ( W o ⋅ [ h t − 1 , x t ] + b o )        ( 式 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[ht1,xt]+bo)      (5)下图表示输出门的计算:在这里插入图片描述
LSTM最终的输出,是由输出门和单元状态共同确定的: h t = o t ∘ t a n h ( c t )        ( 式 6 ) \mathbf{h}_t=\mathbf{o}_t \circ tanh(\mathbf{c}_t)\space\space\space\space\space\space(式6) ht=ottanh(ct)      (6)下图表示LSTM最终输出的计算:在这里插入图片描述
(式1)到(式6)就是LSTM前向计算的全部公式。至此,我们就把LSTM前向计算讲完了。

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

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

LSTM训练算法框架

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

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

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

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

LSTM需要学习的参数共有8组,分别是:遗忘门的权重矩阵 W f W_f Wf和偏置项 b f \mathbf{b}_f bf、输入门的权重矩阵 W i W_i Wi和偏置项 b i \mathbf{b}_i bi、输出门的权重矩阵 W o W_o Wo和偏置项 b o \mathbf{b}_o bo,以及计算单元状态的权重矩阵 W c W_c Wc和偏置项 b c \mathbf{b}_c bc。因为权重矩阵的两部分在反向传播中使用不同的公式,因此在后续的推导中,权重矩阵 W f 、 W i 、 W o 、 W c W_f、W_i、W_o、W_c WfWiWoWc都将被写为分开的两个矩阵: W f h 、 W f x 、 W i h 、 W i x 、 W o h 、 W o x 、 W c h 、 W c x W_{fh}、W_{fx}、W_{ih}、W_{ix}、W_{oh}、W_{ox}、W_{ch}、W_{cx} WfhWfxWihWixWohWoxWchWcx

我们解释一下按元素乘 ∘ \circ 符号。当 ∘ \circ 作用于两个向量时,运算如下: a ∘ b = [ a 1 a 2 a 3 . . . a n ] ∘ [ b 1 b 2 b 3 . . . b n ] = [ a 1 b 1 a 2 b 2 a 3 b 3 . . . a n b n ] 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} ab=a1a2a3...anb1b2b3...bn=a1b1a2b2a3b3...anbn ∘ \circ 作用于一个向量和一个矩阵时,运算如下: a ∘ X = [ a 1 a 2 a 3 . . . a n ] ∘ [ x 11    x 12   . . .   x 1 n x 21    x 22   . . .   x 2 n x 31    x 32   . . .   x 3 n . . . x n 1    x n 2   . . .   x n n ] 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} aX=a1a2a3...anx11  x12 ... x1nx21  x22 ... x2nx31  x32 ... x3n...xn1  xn2 ... xnn            = [ a 1 x 11    a 1 x 12   . . .   a 1 x 1 n a 2 x 21    a 2 x 22   . . .   a 2 x 2 n a 3 x 31    a 3 x 32   . . .   a 3 x 3 n . . . a n x n 1    a n x n 2   . . .   a n x n n ] \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}           =a1x11  a1x12 ... a1x1na2x21  a2x22 ... a2x2na3x31  a3x32 ... a3x3n...anxn1  anxn2 ... anxnn ∘ \circ 作用于两个矩阵时,两个矩阵对应位置的元素相乘。按元素乘可以在某些情况下简化矩阵和向量运算。例如,当一个对角矩阵右乘一个矩阵时,相当于用对角矩阵的对角线组成的向量按元素乘那个矩阵: d i a g [ a ] X = a ∘ X diag[a]X=a \circ X diag[a]X=aX当一个行向量右乘一个对角矩阵时,相当于这个行向量按元素乘那个矩阵对角线组成的向量: a T d i a g [ b ] = a ∘ b a^Tdiag[b]=a \circ b aTdiag[b]=ab上面这两点,在我们后续推导中会多次用到。

在t时刻,LSTM的输出值为 h t \mathbf{h}_t ht。我们定义t时刻的误差项 δ t \delta_t δt为: δ t = d e f ∂ E ∂ h t \delta_t\overset{def}{=}\frac{\partial E}{\partial \mathbf{h}_t} δt=defhtE注意,和前面几篇文章不同,我们这里假设误差项是损失函数对输出值的导数,而不是对加权输入 n e t t l net_t^l nettl的导数。因为LSTM有四个加权输入,分别对应 f t 、 i t 、 c t 、 o t \mathbf{f}_t、\mathbf{i}_t、\mathbf{c}_t、\mathbf{o}_t ftitctot,我们希望往上一层传递一个误差项而不是四个。但我们仍然需要定义出这四个加权输入,以及他们对应的误差项。 n e t f , t = W f [ h t − 1 , x t ] + b f net_{f,t}=W_f[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_f netf,t=Wf[ht1,xt]+bf                      = W f h h t − 1 + W f x x t + b f \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                     =Wfhht1+Wfxxt+bf n e t i , t = W i [ h t − 1 , x t ] + b i net_{i,t}=W_i[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_i neti,t=Wi[ht1,xt]+bi                     = W i h h t − 1 + W i x x t + b i \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                    =Wihht1+Wixxt+bi n e t c ~ , t = W c [ h t − 1 , x t ] + b c net_{\tilde c,t}=W_c[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_c netc~,t=Wc[ht1,xt]+bc                     = W c h h t − 1 + W c x x t + b c \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                    =Wchht1+Wcxxt+bc n e t o , t = W o [ h t − 1 , x t ] + b o net_{o,t}=W_o[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_o neto,t=Wo[ht1,xt]+bo                     = W o h h t − 1 + W o x x t + b o \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                    =Wohht1+Woxxt+bo δ f , t = d e f ∂ E ∂ n e t f , t \delta_{f,t}\overset{def}{=}\frac{\partial E}{\partial net_{f,t}} δf,t=defnetf,tE δ i , t = d e f ∂ E ∂ n e t i , t \delta_{i,t}\overset{def}{=}\frac{\partial E}{\partial net_{i,t}} δi,t=defneti,tE δ c ~ , t = d e f ∂ E ∂ n e t c ~ , t \delta_{\tilde c,t}\overset{def}{=}\frac{\partial E}{\partial net_{\tilde c,t}} δc~,t=defnetc~,tE δ o , t = d e f ∂ E ∂ n e t o , t \delta_{o,t}\overset{def}{=}\frac{\partial E}{\partial net_{o,t}} δo,t=defneto,tE

误差项沿时间的反向传递

沿时间反向传递误差项,就是要计算出t-1时刻的误差项 δ t − 1 \delta_{t-1} δt1 δ t − 1 T = ∂ E ∂ h t − 1 \delta_{t-1}^T=\frac{\partial E}{\partial \mathbf{h}_{t-1}} δt1T=ht1E                 = ∂ E ∂ h t ∂ h t ∂ h t − 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}}                =htEht1ht              = δ t T ∂ h t ∂ h t − 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}}             =δtTht1ht我们知道, ∂ h t ∂ h t − 1 \frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}} ht1ht是一个Jacobian矩阵。如果隐藏层h的维度是N的话,那么它就是一个 N × N N\times N N×N矩阵。为了求出它,我们列出 h t \mathbf{h}_t ht的计算公式,即前面的(式6)和(式4): h t = o t ∘ t a n h ( c t ) \mathbf{h}_t=\mathbf{o}_t \circ tanh(\mathbf c_t) ht=ottanh(ct)           c t = f t ∘   c t − 1 + i t ∘   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 ct1+it c~t显然, o t 、 f t 、 i t 、 c ~ t \mathbf{o}_t、\mathbf{f}_t、\mathbf{i}_t、\mathbf{\tilde{c}}_t otftitc~t都是 h t − 1 \mathbf{h}_{t-1} ht1的函数,那么,利用全导数公式可得:
δ t T ∂ h t ∂ h t − 1 = δ t T ∂ h t ∂ o t ∂ o t ∂ n e t o , t ∂ n e t o , t ∂ h t − 1 + δ t T ∂ h t ∂ c t ∂ c t ∂ f t ∂ f t ∂ n e t f , t ∂ n e t f , t ∂ h t − 1 + δ t T ∂ h t ∂ c t ∂ c t ∂ i t ∂ i t ∂ n e t i , t ∂ n e t i , t ∂ h t − 1 + δ t T ∂ h t ∂ c t ∂ c t ∂ c ~ t ∂ c ~ t ∂ n e t c ~ , t ∂ n e t c ~ , t ∂ h t − 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}}}} δtTht1ht=δtTothtneto,totht1neto,t+δtTcthtftctnetf,tftht1netf,t+δtTcthtitctneti,titht1neti,t+δtTcthtc~tctnetc~,tc~tht1netc~,t = δ o , t T ∂ n e t o , t ∂ h t − 1 + δ f , t T ∂ n e t f , t ∂ h t − 1 + δ i , t T ∂ n e t i , t ∂ h t − 1 + δ c ~ , t T ∂ n e t c ~ , t ∂ h t − 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,tTht1neto,t+δf,tTht1netf,t+δi,tTht1neti,t+δc~,tTht1netc~,t(7)下面,我们要把(式7)中的每个偏导数都求出来。根据(式6),我们可以求出: ∂ h t ∂ o t = d i a g [ tanh ⁡ ( c t ) ] \frac{\partial{\mathbf{h_t}}}{\partial{\mathbf{o}_t}}=diag[\tanh(\mathbf{c}_t)] otht=diag[tanh(ct)]                     ∂ h t ∂ c t = d i a g [ o t ∘ ( 1 − tanh ⁡ ( c t ) 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)]                    ctht=diag[ot(1tanh(ct)2)]根据(式4),我们可以求出: ∂ c t ∂ f t = d i a g [ c t − 1 ] \frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{f_{t}}}}=diag[\mathbf{c}_{t-1}] ftct=diag[ct1] ∂ c t ∂ i t = d i a g [ c ~ t ]     \frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{i_{t}}}}=diag[\mathbf{\tilde{c}}_t]\space\space\space itct=diag[c~t]    ∂ c t ∂ c ~ t = d i a g [ i t ]      \frac{\partial{\mathbf{c}_t}}{\partial{\mathbf{\tilde{c}_{t}}}}=diag[\mathbf{i}_t]\space\space\space\space c~tct=diag[it]    因为: o t = σ ( n e t o , 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\space ot=σ(neto,t)                   n e t o , t = W o h h t − 1 + W o x x t + b o \mathbf{net}_{o,t}=W_{oh}\mathbf{h}_{t-1}+W_{ox}\mathbf{x}_t+\mathbf{b}_o neto,t=Wohht1+Woxxt+bo f t = σ ( n e t f , 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\space ft=σ(netf,t)                  n e t f , t = W f h h t − 1 + W f x x t + b f \mathbf{net}_{f,t}=W_{fh}\mathbf{h}_{t-1}+W_{fx}\mathbf{x}_t+\mathbf{b}_f netf,t=Wfhht1+Wfxxt+bf i t = σ ( n e t i , 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\space it=σ(neti,t)                  n e t i , t = W i h h t − 1 + W i x x t + b i \mathbf{net}_{i,t}=W_{ih}\mathbf{h}_{t-1}+W_{ix}\mathbf{x}_t+\mathbf{b}_i neti,t=Wihht1+Wixxt+bi c ~ t = tanh ⁡ ( n e t c ~ , t )             \mathbf{\tilde{c}}_t=\tanh(\mathbf{net}_{\tilde{c},t})\space\space\space\space\space\space\space\space\space\space\space c~t=tanh(netc~,t)            n e t c ~ , t = W c h h t − 1 + W c x x t + b c \mathbf{net}_{\tilde{c},t}=W_{ch}\mathbf{h}_{t-1}+W_{cx}\mathbf{x}_t+\mathbf{b}_c netc~,t=Wchht1+Wcxxt+bc我们很容易得出: ∂ o t ∂ n e t o , t = d i a g [ o t ∘ ( 1 − o t ) ] \frac{\partial{\mathbf{o}_t}}{\partial{\mathbf{net}_{o,t}}}=diag[\mathbf{o}_t\circ(1-\mathbf{o}_t)] neto,tot=diag[ot(1ot)] ∂ n e t o , t ∂ h t − 1 = W o h                         \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 ht1neto,t=Woh                        ∂ f t ∂ n e t f , t = d i a g [ f t ∘ ( 1 − f t ) ] \frac{\partial{\mathbf{f}_t}}{\partial{\mathbf{net}_{f,t}}}=diag[\mathbf{f}_t\circ(1-\mathbf{f}_t)] netf,tft=diag[ft(1ft)] ∂ n e t f , t ∂ h t − 1 = W f h                        \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 ht1netf,t=Wfh                       ∂ i t ∂ n e t i , t = d i a g [ i t ∘ ( 1 − i t ) ] \frac{\partial{\mathbf{i}_t}}{\partial{\mathbf{net}_{i,t}}}=diag[\mathbf{i}_t\circ(1-\mathbf{i}_t)] neti,tit=diag[it(1it)] ∂ n e t i , t ∂ h t − 1 = W i h                       \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 ht1neti,t=Wih                      ∂ c ~ t ∂ n e t c ~ , t = d i a g [ 1 − c ~ t 2 ]          \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~,tc~t=diag[1c~t2]         ∂ n e t c ~ , t ∂ h t − 1 = W c h                     \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 ht1netc~,t=Wch                   将上述偏导数带入到(式7),我们得到: δ t − 1 = δ o , t T ∂ n e t o , t ∂ h t − 1 + δ f , t T ∂ n e t f , t ∂ h t − 1 + δ i , t T ∂ n e t i , t ∂ h t − 1 + δ c ~ , t T ∂ n e t c ~ , t ∂ h t − 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}}}} δt1=δo,tTht1neto,t+δf,tTht1netf,t+δi,tTht1neti,t+δc~,tTht1netc~,t       = δ o , t T W o h + δ f , t T W f h + δ i , t T W i h + δ c ~ , t T W c h ( 式 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,tTWoh+δf,tTWfh+δi,tTWih+δc~,tTWch(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 , t T = δ t T ∘ tanh ⁡ ( c t ) ∘ o t ∘ ( 1 − o t ) ( 式 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=δtTtanh(ct)ot(1ot)(9)                            δ f , t T = δ t T ∘ o t ∘ ( 1 − tanh ⁡ ( c t ) 2 ) ∘ c t − 1 ∘ f t ∘ ( 1 − f t ) ( 式 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=δtTot(1tanh(ct)2)ct1ft(1ft)(10)     δ i , t T = δ t T ∘ o t ∘ ( 1 − tanh ⁡ ( c t ) 2 ) ∘ c ~ t ∘ i t ∘ ( 1 − i t ) ( 式 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=δtTot(1tanh(ct)2)c~tit(1it)(11) δ c ~ , t T = δ t T ∘ o t ∘ ( 1 − tanh ⁡ ( c t ) 2 ) ∘ i t ∘ ( 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=δtTot(1tanh(ct)2)it(1c~2)(12)    (式8)到(式12)就是将误差沿时间反向传播一个时刻的公式。有了它,我们可以写出将误差项向前传递到任意k时刻的公式: δ k T = ∏ j = k t − 1 δ o , j T W o h + δ f , j T W f h + δ i , j T W i h + δ c ~ , j T W c h ( 式 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=kt1δo,jTWoh+δf,jTWfh+δi,jTWih+δc~,jTWch(13)

将误差项传递到上一层

我们假设当前为第 l l l层,定义 l − 1 l-1 l1层的误差项是误差函数对 l − 1 l-1 l1加权输入的导数,即: δ t l − 1 = d e f ∂ E n e t t l − 1 \delta_t^{l-1}\overset{def}{=}\frac{\partial{E}}{\mathbf{net}_t^{l-1}} δtl1=defnettl1E本次LSTM的输入 x t x_t xt由下面的公式计算: x t l = f l − 1 ( n e t t l − 1 ) \mathbf{x}_t^l=f^{l-1}(\mathbf{net}_t^{l-1}) xtl=fl1(nettl1)上式中, f l − 1 f^{l-1} fl1表示第 l − 1 l-1 l1层的激活函数。

因为 n e t f , t l 、 n e t i , t l 、 n e t c ~ , t l 、 n e t o , t l \mathbf{net}_{f,t}^l、\mathbf{net}_{i,t}^l、\mathbf{net}_{\tilde{c},t}^l、\mathbf{net}_{o,t}^l netf,tlneti,tlnetc~,tlneto,tl都是 x t x_t xt的函数, x t x_t xt又是 n e t t l − 1 net_t^{l-1} nettl1的函数,因此,要求出 E E E n e t t l − 1 net_t^{l-1} nettl1的导数,就需要使用全导数公式: ∂ E ∂ n e t t l − 1 = ∂ E ∂ n e t f , t l ∂ n e t f , t l ∂ x t l ∂ x t l ∂ n e t t l − 1 + ∂ E ∂ n e t i , t l ∂ n e t i , t l ∂ x t l ∂ x t l ∂ n e t t l − 1 + ∂ E ∂ n e t c ~ , t l ∂ n e t c ~ , t l ∂ x t l ∂ x t l ∂ n e t t l − 1 + ∂ E ∂ n e t o , t l ∂ n e t o , t l ∂ x t l ∂ x t l ∂ n e t t l − 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}}}} nettl1E=netf,tlExtlnetf,tlnettl1xtl+neti,tlExtlneti,tlnettl1xtl+netc~,tlExtlnetc~,tlnettl1xtl+neto,tlExtlneto,tlnettl1xtl = δ f , t T W f x ∘ f ′ ( n e t t l − 1 ) + δ i , t T W i x ∘ f ′ ( n e t t l − 1 ) + δ c ~ , t T W c x ∘ f ′ ( n e t t l − 1 ) + δ o , t T W o x ∘ f ′ ( n e t t l − 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,tTWfxf(nettl1)+δi,tTWixf(nettl1)+δc~,tTWcxf(nettl1)+δo,tTWoxf(nettl1) = ( δ f , t T W f x + δ i , t T W i x + δ c ~ , t T W c x + δ o , t T W o x ) ∘ f ′ ( n e t t l − 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,tTWfx+δi,tTWix+δc~,tTWcx+δo,tTWox)f(nettl1)(14)(式14)就是将误差传递到上一层的公式。

权重梯度的计算

对于 W f t 、 W i h 、 W c h 、 W o h W_{ft}、W_{ih}、W_{ch}、W_{oh} WftWihWchWoh的权重梯度,我们知道它的梯度是各个时刻梯度之和(证明过程请参考文章《深度学习之循环神经网络(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时刻的 W o h 、 W i h 、 W f h 、 W c h W_{oh}、W_{ih}、W_{fh}、W_{ch} WohWihWfhWch ∂ E ∂ W o h , t = ∂ E ∂ n e t o , t ∂ n e t o , t ∂ W o h , 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,tE=neto,tEWoh,tneto,t = δ o , t h t − 1 T =\delta_{o,t}\mathbf{h}_{t-1}^T =δo,tht1T ∂ E ∂ W f h , t = ∂ E ∂ n e t f , t ∂ n e t f , t ∂ W f h , 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,tE=netf,tEWfh,tnetf,t = δ f , t h t − 1 T =\delta_{f,t}\mathbf{h}_{t-1}^T =δf,tht1T ∂ E ∂ W i h , t = ∂ E ∂ n e t i , t ∂ n e t i , t ∂ W i h , 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,tE=neti,tEWih,tneti,t = δ i , t h t − 1 T =\delta_{i,t}\mathbf{h}_{t-1}^T =δi,tht1T ∂ E ∂ W c h , t = ∂ E ∂ n e t c ~ , t ∂ n e t c ~ , t ∂ W c h , 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,tE=netc~,tEWch,tnetc~,t = δ c ~ , t h t − 1 T =\delta_{\tilde{c},t}\mathbf{h}_{t-1}^T =δc~,tht1T将各个时刻的梯度加在一起,就能得到最终的梯度: ∂ E ∂ W o h = ∑ j = 1 t δ o , j h j − 1 T \frac{\partial{E}}{\partial{W_{oh}}}=\sum_{j=1}^t\delta_{o,j}\mathbf{h}_{j-1}^T WohE=j=1tδo,jhj1T ∂ E ∂ W f h = ∑ j = 1 t δ f , j h j − 1 T \frac{\partial{E}}{\partial{W_{fh}}}=\sum_{j=1}^t\delta_{f,j}\mathbf{h}_{j-1}^T WfhE=j=1tδf,jhj1T ∂ E ∂ W i h = ∑ j = 1 t δ i , j h j − 1 T \frac{\partial{E}}{\partial{W_{ih}}}=\sum_{j=1}^t\delta_{i,j}\mathbf{h}_{j-1}^T WihE=j=1tδi,jhj1T ∂ E ∂ W c h = ∑ j = 1 t δ c ~ , j h j − 1 T \frac{\partial{E}}{\partial{W_{ch}}}=\sum_{j=1}^t\delta_{\tilde{c},j}\mathbf{h}_{j-1}^T WchE=j=1tδc~,jhj1T对于偏置项 b f 、 b i 、 b c 、 b o \mathbf{b}_f、\mathbf{b}_i、\mathbf{b}_c、\mathbf{b}_o bfbibcbo的梯度,也是将各个时刻的梯度加在一起。下面是各个时刻的偏置项梯度: ∂ E ∂ b o , t = ∂ E ∂ n e t o , t ∂ n e t o , t ∂ b o , 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,tE=neto,tEbo,tneto,t = δ o , t            =\delta_{o,t}\space\space\space\space\space\space\space\space\space\space =δo,t           ∂ E ∂ b f , t = ∂ E ∂ n e t f , t ∂ n e t f , t ∂ b f , 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,tE=netf,tEbf,tnetf,t = δ f , t            =\delta_{f,t}\space\space\space\space\space\space\space\space\space\space =δf,t           ∂ E ∂ b i , t = ∂ E ∂ n e t i , t ∂ n e t i , t ∂ b i , 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,tE=neti,tEbi,tneti,t = δ i , t            =\delta_{i,t}\space\space\space\space\space\space\space\space\space\space =δi,t           ∂ E ∂ b c , t = ∂ E ∂ n e t c ~ , t ∂ n e t c ~ , t ∂ b c , 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,tE=netc~,tEbc,tnetc~,t = δ c ~ , t            =\delta_{\tilde{c},t}\space\space\space\space\space\space\space\space\space\space =δc~,t          下面是最终的偏置项梯度,即将各个时刻的偏置项梯度加在一起: ∂ E ∂ b o = ∑ j = 1 t δ o , j \frac{\partial{E}}{\partial{\mathbf{b}_o}}=\sum_{j=1}^t\delta_{o,j} boE=j=1tδo,j ∂ E ∂ b i = ∑ j = 1 t δ i , j \frac{\partial{E}}{\partial{\mathbf{b}_i}}=\sum_{j=1}^t\delta_{i,j} biE=j=1tδi,j ∂ E ∂ b f = ∑ j = 1 t δ f , j \frac{\partial{E}}{\partial{\mathbf{b}_f}}=\sum_{j=1}^t\delta_{f,j} bfE=j=1tδf,j ∂ E ∂ b c = ∑ j = 1 t δ c ~ , j \frac{\partial{E}}{\partial{\mathbf{b}_c}}=\sum_{j=1}^t\delta_{\tilde{c},j} bcE=j=1tδc~,j对于 W f x 、 W i x 、 W c x 、 W o x W_{fx}、W_{ix}、W_{cx}、W_{ox} WfxWixWcxWox的权重梯度,只需要根据相应的误差项直接计算即可: ∂ E ∂ W o x = ∂ E ∂ n e t o , t ∂ n e t o , t ∂ W o x \frac{\partial{E}}{\partial{W_{ox}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{o,t}}}\frac{\partial{\mathbf{net}_{o,t}}}{\partial{W_{ox}}} WoxE=neto,tEWoxneto,t = δ o , t x t T       =\delta_{o,t}\mathbf{x}_{t}^T\space\space\space\space\space =δo,txtT      ∂ E ∂ W f x = ∂ E ∂ n e t f , t ∂ n e t f , t ∂ W f x \frac{\partial{E}}{\partial{W_{fx}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{f,t}}}\frac{\partial{\mathbf{net}_{f,t}}}{\partial{W_{fx}}} WfxE=netf,tEWfxnetf,t = δ f , t x t T       =\delta_{f,t}\mathbf{x}_{t}^T\space\space\space\space\space =δf,txtT      ∂ E ∂ W i x = ∂ E ∂ n e t i , t ∂ n e t i , t ∂ W i x \frac{\partial{E}}{\partial{W_{ix}}}=\frac{\partial{E}}{\partial{\mathbf{net}_{i,t}}}\frac{\partial{\mathbf{net}_{i,t}}}{\partial{W_{ix}}} WixE=neti,tEWixneti,t = δ i , t x t T       =\delta_{i,t}\mathbf{x}_{t}^T\space\space\space\space\space =δi,txtT      ∂ E ∂ W c x = ∂ E ∂ n e t c ~ , t ∂ n e t c ~ , t ∂ W c x \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}}} WcxE=netc~,tEWcxnetc~,t = δ c ~ , t x t T       =\delta_{\tilde{c},t}\mathbf{x}_{t}^T\space\space\space\space\space =δc~,txtT     
以上就是LSTM的训练算法的全部公式。因为这里面存在很多重复的模式,仔细看看,会发觉并不是太复杂。

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

四、GRU

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

GRU对LSTM做了两个大改动:

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

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

五、小结

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

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值