在看《动手学深度学习》一书时,里面有介绍简化版的对RNN求梯度。其中求隐含层梯度时,作者只是简略地说了句“将上⾯的递归公式展开”就直接给出了结果,下面我详细地给出中间步骤。
RNN求梯度中最关键一环:对隐含层求梯度
RNN求梯度中最关键一环:对隐含层求梯度
Lht=WhhLht+1+WqhLOt \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}
=Whh(WhhLht+2+WqhLOt+1)+WqhLOt = 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)2Lht+2+WhhWqhLOt+1+WqhLOt =( 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(Whh(WhhLht+3+WqhLOt+2)+WqhLOt+1)+WqhLOt = 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)3Lht+3+(Whh)2Lht+2+WhhWqhLOt+1+WqhLOt =(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}
= =\cdots\cdots
=(Whh)TtLhT+i=t+1T[(Whh)TiWqhLOT+ti] =(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{]}
LhT=(Wqh)TLOT 又\qquad\qquad\frac{\partial L}{\partial h_T}=(W^{\top}_{qh})^T\cdot \frac{\partial L}{\partial O_T}
将其代入上式, 即得:
Lht=i=tT[(Whh)TiWqhLOT+ti] \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{]}

相关文章:

  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2021-11-03
  • 2021-12-14
  • 2022-12-23
  • 2022-02-20
猜你喜欢
  • 2021-06-30
  • 2021-04-29
  • 2021-09-22
  • 2022-12-23
  • 2021-10-27
  • 2021-05-25
  • 2022-12-23
相关资源
相似解决方案