|
目录
1、正向传播
2、反向传播
?
3、总结
1、正向传播
LSTM的正向传播公式:
![\Gamma_f^{<t>}= \sigma(W_f[a^{<t-1>},x^{<t>}]+b_f)](https://latex.codecogs.com/gif.latex?%5CGamma_f%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_f%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_f%29)
![\Gamma_u^{<t>}= \sigma(W_u[a^{<t-1>},x^{<t>}]+b_u)](https://latex.codecogs.com/gif.latex?%5CGamma_u%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_u%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_u%29)
![\Gamma_o^{<t>}= \sigma(W_o[a^{<t-1>},x^{<t>}]+b_o)](https://latex.codecogs.com/gif.latex?%5CGamma_o%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_o%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_o%29)
![\widetilde{c}^{<t>}=tanh(W_c[a^{<t-1>},x^{<t>}]+bc)](https://latex.codecogs.com/gif.latex?%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%3Dtanh%28W_c%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+bc%29)





??
由于传播过程比较复杂,我们画一个计算草图来反映时间步t的正向传播中,各个量的关系:

计算图中红色部分不属于时间步t,而是属于时间步t+1,把这几个红色的计算加入以后,可以更清晰的看到, 的值会传播到两个位置(即在时间步t的正向传播中,传递到 ,在时间步t+1的正向传播中,传递到 ;而 的值会传播到5个位置(即时间步t的正向传播中,传递到 ,在时间步t+1的正向传播中,传递到 、 、 和 )。这对于理解反向传播中 、 的导数从何而来非常重要,在反向传播中,我们会看到, 的误差从相应的2个位置反向传播得到, 的误差从相应的5个位置反向传播得到。
2、反向传播
把正向传播的计算图反过来就是时间步t的反向传播的计算图:?

?红色的部分是时间步t+1的量对于? 、 ?导数的贡献。我们根据这张图,从上往下推导。
对于输出激活函数是softmax,损失函数是交叉熵的情况,常用的公式是:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????????????????????????????????????????? (1)
我在RNN反向传播的推导中证明了这个公式,这里就不证明了。
根据

我们可以进而得到:
? ? ? ?????????????????????????????????????????? ? (2)
? ? ? ? ? ? ? ? ?(3)
? ? ? ? ????????????????????????????????????????????????????????????????????????????????(4)
?以上公式和RNN的情况是一模一样的,也不多解释了。
正如反向传播的计算图所示的那样, 的导数除了包含式(4)中的那一项,还包括来自? 、 、 和 ?的四项,即:
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????(5.a)
?类似的, 的导数也可以表示为两项,一项来自 ,另一项来自 :
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(6.a)
?公式(5)(6)实际上给出了 、 导数的递推关系式。
对于最后一个时间步,t=Tx,没有来自下一个时间步的导数传入,公式(5)(6)变为:
(5):???????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (7)
(6):???????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(8.a)
根据 的下标表示:

并把公式(8.a)也用下标表示,可得:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(8.b)
上式的推导用到了
?式(7)和(8.b)给出了最后一个时间步 和 的导数,我们下面只需要假设已知 和 的导数,并求出 和 导数的表达式(递推关系式),就能够利用递推关系一步步反向传播求出所有时间步下? 和 的导数。
先根据 和 的导数,得到?? 、 、 和 ?的导数:
根据这两个正向传播的式子:


写成下标表示:


应用链式法则,得到:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????(9)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? (10)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(11)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (12)
于是如公式(5.a)所示, 可以表示为如下5项:
? ? (5.b)
根据正向传播的公式:
?![\Gamma_f^{<t>}= \sigma(W_f[a^{<t-1>},x^{<t>}]+b_f)](https://latex.codecogs.com/gif.latex?%5CGamma_f%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_f%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_f%29)
![\Gamma_u^{<t>}= \sigma(W_u[a^{<t-1>},x^{<t>}]+b_u)](https://latex.codecogs.com/gif.latex?%5CGamma_u%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_u%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_u%29)
![\Gamma_o^{<t>}= \sigma(W_o[a^{<t-1>},x^{<t>}]+b_o)](https://latex.codecogs.com/gif.latex?%5CGamma_o%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_o%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_o%29)
![\widetilde{c}^{<t>}=tanh(W_c[a^{<t-1>},x^{<t>}]+bc)](https://latex.codecogs.com/gif.latex?%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%3Dtanh%28W_c%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+bc%29)
?写为下标形式:
![\Gamma_{i}^{<t+1>}= \sigma(W_{ij}[a^{<t>},x^{<t+1>}]_j+b_i)](https://latex.codecogs.com/gif.latex?%5CGamma_%7Bi%7D%5E%7B%3Ct+1%3E%7D%3D%20%5Csigma%28W_%7Bij%7D%5Ba%5E%7B%3Ct%3E%7D%2Cx%5E%7B%3Ct+1%3E%7D%5D_j+b_i%29)
![\widetilde{c}^{<t+1>}_i=tanh(W_{cij}[a^{<t>},x^{<t+1>}]_j+b_{ci})](https://latex.codecogs.com/gif.latex?%5Cwidetilde%7Bc%7D%5E%7B%3Ct+1%3E%7D_i%3Dtanh%28W_%7Bcij%7D%5Ba%5E%7B%3Ct%3E%7D%2Cx%5E%7B%3Ct+1%3E%7D%5D_j+b_%7Bci%7D%29)
式中的 代表三个门中的任意一个
于是,我们可以给出偏导数:


以上的推导用到了sigmoid函数的导数
带入(5.b),可以得到

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (5.c)
(5.c)中W下标的a表示只考虑W中与a有关的部分,即![W[:,1:n_a]](https://latex.codecogs.com/gif.latex?W%5B%3A%2C1%3An_a%5D)
再考虑(6.a):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(6.a)
(6.a)右边第一项等于:
?![\frac{\partial L}{\partial a^{<t>}}\frac{\partial a^{<t>}}{\partial c^{<t>}}=\frac{\partial L}{\partial a^{<t>}}.*\Gamma_{o}^{<t>}.*[1-tanh^2(c^{<t>})]](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20a%5E%7B%3Ct%3E%7D%7D%5Cfrac%7B%5Cpartial%20a%5E%7B%3Ct%3E%7D%7D%7B%5Cpartial%20c%5E%7B%3Ct%3E%7D%7D%3D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20a%5E%7B%3Ct%3E%7D%7D.*%5CGamma_%7Bo%7D%5E%7B%3Ct%3E%7D.*%5B1-tanh%5E2%28c%5E%7B%3Ct%3E%7D%29%5D)
这里的推导和(8.b)是一模一样的,就不再证明了。
考虑(6.a)右边第二项
利用正向传播的公式:
第二项??
故(6.a)可以写为:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (6.b)
至此,我们给出了 、 的导数的递推关系式(5.c)、(6.b)。接下来的部分就比较简单了,只需要根据 、 的导数计算 等参数的梯度。以下式子和公式(9)(10)(11)(12)是一样的,只是把时间步t+1改为了t:




结合正向传播的公式:
![\Gamma_{i}^{<t>}= \sigma(W_{ij}[a^{<t-1>},x^{<t>}]_j+b_i)](https://latex.codecogs.com/gif.latex?%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_%7Bij%7D%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D_j+b_i%29)
![\widetilde{c}^{<t>}_i=tanh(W_{cij}[a^{<t-1>},x^{<t>}]_j+b_{ci})](https://latex.codecogs.com/gif.latex?%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%3Dtanh%28W_%7Bcij%7D%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D_j+b_%7Bci%7D%29)
(公式中下标不指示那个门的 ,代表三个门中的任意一个)
得到:
![\frac{\partial L}{\partial W_{ij}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \Gamma_{i}^{<t>}}\frac{\partial \Gamma_{i}^{<t>}}{\partial W_{ij}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \Gamma_{i}^{<t>}}\Gamma_{i}^{<t>}(1-\Gamma_{i}^{<t>})[a^{<t-1>},x^{<t>}]_j](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20W_%7Bij%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%7D%5Cfrac%7B%5Cpartial%20%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%7D%7B%5Cpartial%20W_%7Bij%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%7D%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%281-%5CGamma_%7Bi%7D%5E%7B%3Ct%3E%7D%29%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D_j)

![\frac{\partial L}{\partial W_{cij}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}_i}\frac{\partial \widetilde{c}^{<t>}_i}{\partial W_{cij}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}_i}[1-(\widetilde{c}^{<t>}_i)^2][a^{<t-1>},x^{<t>}]_j](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20W_%7Bcij%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%5Cfrac%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%7B%5Cpartial%20W_%7Bcij%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%5B1-%28%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%29%5E2%5D%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D_j)
![\frac{\partial L}{\partial b_{ci}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}_i}\frac{\partial \widetilde{c}^{<t>}_i}{\partial b_{ci}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}_i}[1-(\widetilde{c}^{<t>}_i)^2]](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20b_%7Bci%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%5Cfrac%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%7B%5Cpartial%20b_%7Bci%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%7D%5B1-%28%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D_i%29%5E2%5D)

![\frac{\partial L}{\partial W}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \Gamma^{<t>}}.*\Gamma^{<t>}.*(1-\Gamma^{<t>})[a^{<t-1>},x^{<t>}]^T](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20W%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5CGamma%5E%7B%3Ct%3E%7D%7D.*%5CGamma%5E%7B%3Ct%3E%7D.*%281-%5CGamma%5E%7B%3Ct%3E%7D%29%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D%5ET)

![\frac{\partial L}{\partial W_{c}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}}.*[1-(\widetilde{c}^{<t>})^2][a^{<t-1>},x^{<t>}]^T](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20W_%7Bc%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%7D.*%5B1-%28%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%29%5E2%5D%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D%5ET)
![\frac{\partial L}{\partial b_{c}}=\sum_{t=1}^{T_x}\frac{\partial L}{\partial \widetilde{c}^{<t>}}.*[1-(\widetilde{c}^{<t>})^2]](https://latex.codecogs.com/gif.latex?%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20b_%7Bc%7D%7D%3D%5Csum_%7Bt%3D1%7D%5E%7BT_x%7D%5Cfrac%7B%5Cpartial%20L%7D%7B%5Cpartial%20%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%7D.*%5B1-%28%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%29%5E2%5D)

? ? ? ? ? ? ? ? ? ? ? ? (13)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (14)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (15)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (16)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (17)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(18)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (19)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (20)
以上就是LSTM反向传播所需要的所有公式。
3、总结
LSTM的正向传播由以下公式给出:
![\Gamma_f^{<t>}= \sigma(W_f[a^{<t-1>},x^{<t>}]+b_f)](https://latex.codecogs.com/gif.latex?%5CGamma_f%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_f%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_f%29)
![\Gamma_u^{<t>}= \sigma(W_u[a^{<t-1>},x^{<t>}]+b_u)](https://latex.codecogs.com/gif.latex?%5CGamma_u%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_u%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_u%29)
![\Gamma_o^{<t>}= \sigma(W_o[a^{<t-1>},x^{<t>}]+b_o)](https://latex.codecogs.com/gif.latex?%5CGamma_o%5E%7B%3Ct%3E%7D%3D%20%5Csigma%28W_o%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+b_o%29)
![\widetilde{c}^{<t>}=tanh(W_c[a^{<t-1>},x^{<t>}]+bc)](https://latex.codecogs.com/gif.latex?%5Cwidetilde%7Bc%7D%5E%7B%3Ct%3E%7D%3Dtanh%28W_c%5Ba%5E%7B%3Ct-1%3E%7D%2Cx%5E%7B%3Ct%3E%7D%5D+bc%29)





??
反向传播步骤:
①在每个时间步t,接受来自上一个时间步t+1的导数 、 ,(最后一个时间步下的值可以准确得到)
②
——计算参数Wy、by的导数
? ? ? ? ?????????????????????????????????????????? ? (2)
? ? ? ? ? ? ? ? ?(3)
(虽然是对所有时间步求和,但是时间步t的时候可以计算一项,最后再加到一起就行)
——计算
? ? ? ? ? ????????????????????????????????????????????????????????????????????????????????(4)?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (5.c)
?——计算
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (6.b)
—— 计算其他参数的导数
? ? ? ? ? ? ? ? ? ? ? ? ? (13)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (14)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (15)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (16)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (17)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(18)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (19)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (20)
(虽然是对所有时间步求和,但是时间步t的时候可以计算一项,最后再加到一起就行)
③把 、 传递给下一个时间步t-1
?
?
|