RNN对隐含层求梯度

在看《动手学深度学习》一书时,里面有介绍简化版的对RNN求梯度。其中求隐含层梯度时,作者只是简略地说了句“将上⾯的递归公式展开”就直接给出了结果,下面我详细地给出中间步骤。
在这里插入图片描述
在这里插入图片描述
∂ L ∂ h t = W h h ⊤ ⋅ ∂ L ∂ h t + 1 + W q h ⊤ ⋅ ∂ L ∂ O t \frac{\partial L}{\partial h_t} = W^{\top}_{hh} \cdot \frac{\partial L}{\partial h_{t+1}} + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_t} htL=Whhht+1L+WqhOtL
= W h h ⊤ ⋅ ( W h h ⊤ ⋅ ∂ L ∂ h t + 2 + W q h ⊤ ⋅ ∂ L ∂ O t + 1 ) + W q h ⊤ ⋅ ∂ L ∂ O t = W^{\top}_{hh} \cdot(W^{\top}_{hh} \cdot \frac{\partial L}{\partial h_{t+2}} + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{t+1}}) + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_t} =Whh(Whhht+2L+WqhOt+1L)+WqhOtL
= ( W h h ⊤ ) 2 ⋅ ∂ L ∂ h t + 2 + W h h ⊤ ⋅ W q h ⊤ ⋅ ∂ L ∂ O t + 1 + W q h ⊤ ⋅ ∂ L ∂ O t =( W^{\top}_{hh})^2\cdot \frac{\partial L}{\partial h_{t+2}} + W^{\top}_{hh} \cdot W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{t+1}} + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_t} =(Whh)2ht+2L+WhhWqhOt+1L+WqhOtL
= W h h ⊤ ⋅ ( W h h ⊤ ⋅ ( W h h ⊤ ⋅ ∂ L ∂ h t + 3 + W q h ⊤ ⋅ ∂ L ∂ O t + 2 ) + W q h ⊤ ⋅ ∂ L ∂ O t + 1 ) + W q h ⊤ ⋅ ∂ L ∂ O t = W^{\top}_{hh} \cdot(W^{\top}_{hh} \cdot (W^{\top}_{hh} \cdot \frac{\partial L}{\partial h_{t+3}} + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{t+2}}) + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{t+1}}) + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_t} =Whh(Whh(Whhht+3L+WqhOt+2L)+WqhOt+1L)+WqhOtL
= ( W h h ⊤ ) 3 ⋅ ∂ L ∂ h t + 3 + ( W h h ⊤ ) 2 ⋅ ∂ L ∂ h t + 2 + W h h ⊤ ⋅ W q h ⊤ ⋅ ∂ L ∂ O t + 1 + W q h ⊤ ⋅ ∂ L ∂ O t =(W^{\top}_{hh})^3\cdot \frac{\partial L}{\partial h_{t+3}} +(W^{\top}_{hh})^2\cdot \frac{\partial L}{\partial h_{t+2}} + W^{\top}_{hh} \cdot W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{t+1}} + W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_t} =(Whh)3ht+3L+(Whh)2ht+2L+WhhWqhOt+1L+WqhOtL
= ⋯ ⋯ =\cdots\cdots =
= ( W h h ⊤ ) T − t ⋅ ∂ L ∂ h T + ∑ i = t + 1 T [ ( W h h ⊤ ) T − i ⋅ W q h ⊤ ⋅ ∂ L ∂ O T + t − i ] =(W^{\top}_{hh})^{T-t}\cdot \frac{\partial L}{\partial h_T} + \sum_{i=t+1}^{T}\textbf{[}(W^{\top}_{hh})^{T-i}\cdot W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{T+t-i}}\textbf{]} =(Whh)TthTL+i=t+1T[(Whh)TiWqhOT+tiL]
又 ∂ L ∂ h T = ( W q h ⊤ ) T ⋅ ∂ L ∂ O T 又\qquad\qquad\frac{\partial L}{\partial h_T}=(W^{\top}_{qh})^T\cdot \frac{\partial L}{\partial O_T} hTL=(Wqh)TOTL
将其代入上式, 即得:
∂ L ∂ h t = ∑ i = t T [ ( W h h ⊤ ) T − i ⋅ W q h ⊤ ⋅ ∂ L ∂ O T + t − i ] \frac{\partial L}{\partial h_t}=\sum_{i=t}^{T}\textbf{[}(W^{\top}_{hh})^{T-i}\cdot W^{\top}_{qh} \cdot \frac{\partial L}{\partial O_{T+t-i}}\textbf{]} htL=i=tT[(Whh)TiWqhOT+tiL]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值