机器学习笔记之递归网络——递归神经网络的反向传播过程
引言
上一节介绍了 Softmax \text{Softmax} Softmax回归的反向传播过程。本节将介绍递归神经网络的反向传播过程。
回顾:递归神经网络的前馈计算过程
场景构建
已知某特定时刻的递归神经网络神经元表示如下:
其中:
-
x t x_t xt表示数据在 t t t时刻的输入,其维度格式为 。其中 n x n_x nx表示当前时刻输入向量的维数; m m m表示样本数量; 1 1 1则表示当前所在时刻 t t t。
输入向量可能是‘词向量’,或者是其他描述序列单位的向量。而
n x n_x nx描述该向量的大小。
- m m m
可表示为当前
Batch \text{Batch} Batch内的样本数量。
对应完整序列数据
X \mathcal X X可表示为如下形式。其中
T \mathcal T T表示输入时刻的具体数量。
X = ( x 1 , x 2 , ⋯ , x t , x t + 1 , ⋯ , x T ) T ∈ R n x × m × T \mathcal X = (x_1,x_2,\cdots,x_t,x_{t+1},\cdots,x_{\mathcal T})^T \in \mathbb R^{n_x \times m \times \mathcal T} X=(x1,x2,⋯,xt,xt+1,⋯,xT)T∈Rnx×m×T
-
h t h_t ht表示 t t t时刻的序列信息,也是要传递到 t + 1 t+1 t+1时刻的值;它的维度格式表示为:
这里
n h n_h nh表示隐藏状态的维数大小;它由参数
W H ⇒ H , W H ⇒ X \mathcal W_{\mathcal H \Rightarrow \mathcal H},\mathcal W_{\mathcal H \Rightarrow \mathcal X} WH⇒H,WH⇒X决定;
同理。
h t ∈ R n h × m × 1 h_t \in \mathbb R^{n_h \times m \times 1} ht∈Rnh×m×1
对应的隐藏层矩阵
H ∈ R n h × m × T \mathcal H \in \mathbb R^{n_h \times m \times \mathcal T} H∈Rnh×m×T。因为每一进入一个输入,都会得到一个相应更长的序列信息。因此
X , H \mathcal X,\mathcal H X,H共用同一个
T \mathcal T T。 -
O t + 1 \mathcal O_{t+1} Ot+1表示数据传入后计算产生的预测值,它的维度格式表示为:
其中
n O n_{\mathcal O} nO表示预测输出结果的长度。
O t + 1 ∈ R n O × m × 1 \mathcal O_{t+1} \in \mathbb R^{n_{\mathcal O} \times m \times \mathcal 1} Ot+1∈RnO×m×1
同理,对应的输出矩阵
O ∈ R n O × m × T O \mathcal O \in \mathbb R^{n_{\mathcal O} \times m \times \mathcal T_{\mathcal O}} O∈RnO×m×TO,这里的
T O \mathcal T_{\mathcal O} TO表示输出时刻的数量。需要注意的是,
T O \mathcal T_{\mathcal O} TO和
T \mathcal T T是两个概念。输出的序列长度和输入长度无关,它与权重参数
W H ⇒ O \mathcal W_{\mathcal H \Rightarrow \mathcal O} WH⇒O相关。
前馈计算描述
为了方便描述,将上述过程中的序列下标表示为序列上标:
x t , h t , h t + 1 , O t + 1 ⇒ x ( t ) , h ( t ) , h ( t + 1 ) , O ( t + 1 ) x_t,h_t,h_{t+1},\mathcal O_{t+1} \Rightarrow x^{(t)},h^{(t)},h^{(t+1)},\mathcal O^{(t+1)} xt,ht,ht+1,Ot+1⇒x(t),h(t),h(t+1),O(t+1)
关于第 t t t时刻神经元的前馈计算过程表示如下:
需要注意的是,这里的
h ( t + 1 ) , O ( t + 1 ) h^{(t+1)},\mathcal O^{(t+1)} h(t+1),O(t+1)表示对下一时刻信息的预测,而这个预测过程是在
t t t时刻完成的。
- 序列信息 h ( t + 1 ) h^{(t+1)} h(t+1)的计算过程:
{ Z 1 ( t ) = W h ( t ) ⇒ h ( t + 1 ) ⋅ h ( t ) + W x ( t ) ⇒ h ( t + 1 ) ⋅ x ( t ) + b h ( t + 1 ) h ( t + 1 ) = Tanh ( Z 1 ( t ) ) \begin{cases} \mathcal Z_1^{(t)} = \mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}}\cdot h^{(t)} + \mathcal W_{x^{(t)} \Rightarrow h^{(t+1)}} \cdot x^{(t)} + b_{h^{(t+1)}} \\ \quad \\ h^{(t+1)} = \text{Tanh}(\mathcal Z_1^{(t)}) \end{cases} ⎩ ⎨ ⎧Z1(t)=Wh(t)⇒h(t+1)⋅h(t)+Wx(t)⇒h(t+1)⋅x(t)+bh(t+1)h(t+1)=Tanh(Z1(t)) - 预测值 O ( t + 1 ) \mathcal O^{(t+1)} O(t+1)的计算过程:
关于后验概率
P m o d e l [ O ( t + 1 ) ∣ x ( t ) , h ( t + 1 ) ] \mathcal P_{model}[\mathcal O^{(t+1)} \mid x^{(t)},h^{(t+1)}] Pmodel[O(t+1)∣x(t),h(t+1)]本质上是一个分类任务——从该分布中选择概率最高的结果作为
x ( t + 1 ) x^{(t+1)} x(t+1)的结果,这里使用
Softmax \text{Softmax} Softmax函数对各结果对应的概率分布信息进行评估。
{ Z 2 ( t + 1 ) = W h ( t + 1 ) ⇒ O ( t + 1 ) ⋅ h ( t + 1 ) + b O ( t + 1 ) O ( t + 1 ) = Softmax ( Z 2 ( t + 1 ) ) = exp { Z 2 ( t + 1 ) } ∑ i = 1 n O exp { Z 2 ; i ( t + 1 ) } \begin{cases} \mathcal Z_2^{(t+1)} = \mathcal W_{h^{(t+1)} \Rightarrow \mathcal O^{(t+1)}} \cdot h^{(t+1)} + b_{\mathcal O^{(t+1)}} \\ \quad \\ \begin{aligned} \mathcal O^{(t+1)} & = \text{Softmax}(\mathcal Z_2^{(t+1)}) \\ & = \frac{\exp \left\{\mathcal Z_2^{(t+1)}\right\}}{\sum_{i=1}^{n_{\mathcal O}}\exp \left\{\mathcal Z_{2;i}^{(t+1)}\right\}} \\ \end{aligned} \end{cases} ⎩ ⎨ ⎧Z2(t+1)=Wh(t+1)⇒O(t+1)⋅h(t+1)+bO(t+1)O(t+1)=Softmax(Z2(t+1))=∑i=1nOexp{Z2;i(t+1)}exp{Z2(t+1)}
其中,公式中出现的各参数维度格式表示如下:
Z 1 : { W h ( t ) ⇒ h ( t + 1 ) ∈ R 1 × n h ⇒ W H ⇒ H ∈ R n h × n h W x ( t ) ⇒ h ( t + 1 ) ∈ R 1 × n x ⇒ W X ⇒ H ∈ R n h × n x b h ( t + 1 ) ∈ R 1 × 1 ⇒ b H ∈ R n h × 1 Z 2 : { W h ( t + 1 ) ⇒ O ( t + 1 ) ∈ R ⇒ W H ⇒ O ∈ R n O × n h b O ( t + 1 ) ∈ R 1 × 1 ⇒ b O ∈ R n O × 1 \begin{aligned} & \mathcal Z_1:\begin{cases} \mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}} \in \mathbb R^{1 \times n_h} \Rightarrow \mathcal W_{\mathcal H \Rightarrow \mathcal H} \in \mathbb R^{n_h \times n_h} \\ \mathcal W_{x^{(t)} \Rightarrow h^{(t+1)}} \in \mathbb R^{1 \times n_x} \Rightarrow \mathcal W_{\mathcal X \Rightarrow \mathcal H} \in \mathbb R^{n_h \times n_x} \\ b_{\mathcal h^{(t+1)}} \in \mathbb R^{1 \times 1} \Rightarrow b_{\mathcal H} \in \mathbb R^{n_h \times 1} \end{cases} \\ & \mathcal Z_2:\begin{cases} \mathcal W_{h^{(t+1)} \Rightarrow \mathcal O^{(t+1)}} \in \mathbb R^{} \Rightarrow \mathcal W_{\mathcal H \Rightarrow \mathcal O} \in \mathbb R^{n_{\mathcal O} \times n_h} \\ b_{\mathcal O^{(t+1)}} \in \mathbb R^{1 \times 1} \Rightarrow b_{\mathcal O} \in \mathbb R^{n_{\mathcal O} \times 1} \end{cases} \end{aligned} Z1:⎩ ⎨ ⎧Wh(t)⇒h(t+1)∈R1×nh⇒WH⇒H∈Rnh×nhWx(t)⇒h(t+1)∈R1×nx⇒WX⇒H∈Rnh×nxbh(t+1)∈R1×1⇒bH∈Rnh×1Z2:{Wh(t+1)⇒O(t+1)∈R⇒WH⇒O∈RnO×nhbO(t+1)∈R1×1⇒bO∈RnO×1
反向传播过程各参数的梯度计算
各时刻损失函数梯度计算
假设损失函数 J \mathcal J J是描述真实目标 [ y ] n O × m × T O [y]_{n_{\mathcal O} \times m \times \mathcal T_{\mathcal O}} [y]nO×m×TO与预测结果 O n O × m × T O \mathcal O_{n_{\mathcal O} \times m \times \mathcal T_{\mathcal O}} OnO×m×TO之间的交叉熵 ( CrossEntropy ) (\text{CrossEntropy}) (CrossEntropy)累积结果。具体表示如下:
J = ∑ t = 1 T O L ( t ) L ( t ) = − ∑ i = 1 n O y i ( t ) log O i ( t ) \begin{aligned} \mathcal J & = \sum_{t = 1}^{\mathcal T_{\mathcal O}} \mathcal L^{(t)} \\ \mathcal L^{(t)} & = -\sum_{i=1}^{n_{\mathcal O}} y_i^{(t)} \log \mathcal O_i^{(t)} \end{aligned} JL(t)=t=1∑TOL(t)=−i=1∑nOyi(t)logOi(t)
首先计算 J \mathcal J J对 L ( t ) \mathcal L^{(t)} L(t)的梯度结果 ∂ J ∂ L ( t ) \begin{aligned} \frac{\partial \mathcal J}{\partial \mathcal L^{(t)}}\end{aligned} ∂L(t)∂J:
牛顿-莱布尼兹公式。
∂ J ∂ L ( t ) = ∑ k = 1 T O ∂ L ( k ) ∂ L ( t ) = 0 + 0 + ⋯ + 0 ⏟ k ≠ t + 1 ⏟ k = t = 1 \begin{aligned} \frac{\partial \mathcal J}{\partial \mathcal L^{(t)}} & = \sum_{k=1}^{\mathcal T_{\mathcal O}} \frac{\partial \mathcal L^{(k)}}{\partial \mathcal L^{(t)}} \\ & = \underbrace{0 + 0 + \cdots + 0}_{k \neq t} + \underbrace{1}_{k=t} \\ & = 1 \end{aligned} ∂L(t)∂J=k=1∑TO∂L(t)∂L(k)=k=t 0+0+⋯+0+k=t 1=1
损失函数对各时刻神经元输出的梯度计算
其次,计算 L ( t ) \mathcal L^{(t)} L(t)对 O ( t ) \mathcal O^{(t)} O(t)的梯度结果 ∂ L ( t ) ∂ O ( t ) \begin{aligned}\frac{\partial \mathcal L^{(t)}}{\partial \mathcal O^{(t)}}\end{aligned} ∂O(t)∂L(t):
这仅仅是交叉熵的梯度结果。这里需要使用‘标量对向量求导’。
∂ L ( t ) ∂ O ( t ) = ∂ ∂ O ( t ) [ − ∑ i = 1 n O y i ( t ) log O i ( t ) ] = { ∂ ∂ O 1 ( t ) [ − ( y 1 ( t ) log O 1 ( t ) ⏟ O 1 ( t ) 相关 + ⋯ + y n O ( t ) log O n O ( t ) ⏟ O 1 ( t ) 无关 ) ] , ⋯ , ∂ ∂ O n O ( t ) [ − ( y 1 ( t ) log O 1 ( t ) + ⋯ ⏟ O n O ( t ) 无关 + y n O ( t ) log O n O ( t ) ⏟ O n O ( t ) 相关 ) ] } = [ ∂ ∂ O 1 ( t ) ( − y 1 ( t ) log O 1 ( t ) ) , ⋯ , ∂ ∂ O n O ( t ) ( − y n O ( t ) log O n O ( t ) ) ] = [ − y 1 ( t ) O 1 ( t ) , ⋯ , − y n O ( t ) O n O ( t ) ] 1 × n O \begin{aligned} \frac{\partial \mathcal L^{(t)}}{\partial \mathcal O^{(t)}} & = \frac{\partial}{\partial \mathcal O^{(t)}} \left[- \sum_{i=1}^{n_{\mathcal O}} y_i^{(t)} \log \mathcal O_i^{(t)}\right] \\ & = \left\{\frac{\partial}{\partial \mathcal O_1^{(t)}} \left[-(\underbrace{y_1^{(t)} \log \mathcal O_1^{(t)}}_{\mathcal O_1^{(t)}相关} + \underbrace{\cdots + y_{n_{\mathcal O}}^{(t)} \log \mathcal O_{n_{\mathcal O}}^{(t)}}_{\mathcal O_1^{(t)}无关})\right] ,\cdots, \frac{\partial}{\partial \mathcal O_{n_{\mathcal O}}^{(t)}} \left[-(\underbrace{y_1^{(t)} \log \mathcal O_1^{(t)} + \cdots}_{\mathcal O_{n_{\mathcal O}}^{(t)}无关} + \underbrace{y_{n_{\mathcal O}}^{(t)} \log \mathcal O_{n_{\mathcal O}}^{(t)}}_{\mathcal O_{n_{\mathcal O}}^{(t)}相关})\right] \right\} \\ & = \left[\frac{\partial}{\partial \mathcal O_1^{(t)}}(-y_1^{(t)} \log \mathcal O_1^{(t)}),\cdots,\frac{\partial}{\partial \mathcal O_{n_{\mathcal O}}^{(t)}}(-y_{n_{\mathcal O}}^{(t)} \log \mathcal O_{n_{\mathcal O}}^{(t)})\right] \\ & = \left[-\frac{y_1^{(t)}}{\mathcal O_1^{(t)}},\cdots,-\frac{y_{n_{\mathcal O}}^{(t)}}{\mathcal O_{n_{\mathcal O}}^{(t)}}\right]_{1 \times n_{\mathcal O}} \end{aligned} ∂O(t)∂L(t)=∂O(t)∂[−i=1∑nOyi(t)logOi(t)]=⎩ ⎨ ⎧∂O1(t)∂ −(O1(t)相关 y1(t)logO1(t)+O1(t)无关 ⋯+ynO(t)logOnO(t)) ,⋯,∂OnO(t)∂ −(OnO(t)无关 y1(t)logO1(t)+⋯+OnO(t)相关 ynO(t)logOnO(t)) ⎭ ⎬ ⎫=[∂O1(t)∂(−y1(t)logO1(t)),⋯,∂OnO(t)∂(−ynO(t)logOnO(t))]=[−O1(t)y1(t),⋯,−OnO(t)ynO(t)]1×nO
Softmax \text{Softmax} Softmax回归的梯度计算
计算 O ( t ) \mathcal O^{(t)} O(t)对 Z 2 ( t ) \mathcal Z_2^{(t)} Z2(t)的梯度结果 ∂ O ( t ) ∂ Z 2 ( t ) \begin{aligned}\frac{\partial \mathcal O^{(t)}}{\partial \mathcal Z_2^{(t)}}\end{aligned} ∂Z2(t)∂O(t):
这里用到了
Softmax \text{Softmax} Softmax回归的反向传播过程。详见
Softmax函数的反向传播过程
其中
Z 2 ( t ) = ( Z 2 ; 1 ( t ) , Z 2 ; 2 ( t ) , ⋯ , Z 2 ; n O ( t ) ) 1 × n O \mathcal Z_2^{(t)} = (\mathcal Z_{2;1}^{(t)},\mathcal Z_{2;2}^{(t)},\cdots,\mathcal Z_{2;n_{\mathcal O}}^{(t)})_{1 \times n_{\mathcal O}} Z2(t)=(Z2;1(t),Z2;2(t),⋯,Z2;nO(t))1×nO
∂ O ( t ) ∂ Z 2 ( t ) = [ ∂ O i ( t ) ∂ Z 2 ; j ( t ) ] n O × n O i , j ∈ { 1 , 2 , ⋯ , n O } \frac{\partial \mathcal O^{(t)}}{\partial \mathcal Z_2^{(t)}} = \left[\frac{\partial \mathcal O_i^{(t)}}{\partial \mathcal Z_{2;j}^{(t)}}\right]_{n_{\mathcal O} \times n_{\mathcal O}} \quad i,j \in \{1,2,\cdots,n_{\mathcal O}\} ∂Z2(t)∂O(t)=[∂Z2;j(t)∂Oi(t)]nO×nOi,j∈{1,2,⋯,nO}
对应地, ∂ L ∂ Z 2 ( t ) = ∂ L ∂ L ( t ) ⋅ ∂ L ( t ) ∂ O ( t ) ⋅ ∂ O ( t ) ∂ Z 2 ( t ) \begin{aligned}\frac{\partial \mathcal L}{\partial \mathcal Z_2^{(t)}} = \frac{\partial \mathcal L}{\partial \mathcal L^{(t)}} \cdot \frac{\partial \mathcal L^{(t)}}{\partial \mathcal O^{(t)}} \cdot \frac{\partial \mathcal O^{(t)}}{\partial \mathcal Z_2^{(t)}} \end{aligned} ∂Z2(t)∂L=∂L(t)∂L⋅∂O(t)∂L(t)⋅∂Z2(t)∂O(t)可表示为:
并且将
∂ O i ( t ) ∂ Z 2 ; j ( t ) ( i , j = 1 , 2 , ⋯ , n O ) = { O i ( t ) ⋅ ( 1 − O j ( t ) ) i = j − O i ( t ) ⋅ O j ( t ) i ≠ j \begin{aligned}\frac{\partial \mathcal O_i^{(t)}}{\partial \mathcal Z_{2;j}^{(t)}}(i,j=1,2,\cdots,n_{\mathcal O}) = \begin{cases} \mathcal O_i^{(t)} \cdot (1 - \mathcal O_j^{(t)}) \quad i=j \\ -\mathcal O_i^{(t)} \cdot \mathcal O_j^{(t)} \quad i \neq j \end{cases}\end{aligned} ∂Z2;j(t)∂Oi(t)(i,j=1,2,⋯,nO)={Oi(t)⋅(1−Oj(t))i=j−Oi(t)⋅Oj(t)i=j代入到式子中。
∂ L ∂ Z 2 ( t ) = 1 × [ − y 1 ( t ) O 1 ( t ) , ⋯ , − y n O ( t ) O n O ( t ) ] 1 × n O ⋅ [ ∂ O i ( t ) ∂ Z 2 ; j ( t ) ] n O × n O = [ − ∑ i = 1 n O y i ( t ) O i ( t ) ⋅ ∂ O i ( t ) ∂ Z 2 ; j ( t ) ] 1 × n O j = 1 , 2 , ⋯ , n O \begin{aligned} \frac{\partial \mathcal L}{\partial \mathcal Z_2^{(t)}} & = 1 \times \left[-\frac{y_1^{(t)}}{\mathcal O_1^{(t)}},\cdots,-\frac{y_{n_{\mathcal O}}^{(t)}}{\mathcal O_{n_{\mathcal O}}^{(t)}}\right]_{1 \times n_{\mathcal O}} \cdot \left[\frac{\partial \mathcal O_i^{(t)}}{\partial \mathcal Z_{2;j}^{(t)}}\right]_{n_{\mathcal O} \times n_{\mathcal O}} \\ & = \left[-\sum_{i=1}^{n_{\mathcal O}} \frac{y_i^{(t)}}{\mathcal O_i^{(t)}}\ \cdot \frac{\partial \mathcal O_i^{(t)}}{\partial \mathcal Z_{2;j}^{(t)}}\right]_{1 \times n_{\mathcal O}} \quad j = 1,2,\cdots,n_{\mathcal O} \end{aligned} ∂Z2(t)∂L=1×[−O1(t)y1(t),⋯,−OnO(t)ynO(t)]1×nO⋅[∂Z2;j(t)∂Oi(t)]nO×nO=[−i=1∑nOOi(t)yi(t) ⋅∂Z2;j(t)∂Oi(t)]1×nOj=1,2,⋯,nO
可以看出,该结果是一个 1 × n O 1 \times n_{\mathcal O} 1×nO的向量。以其中第一项为例:
j = 1 ⇒ − ∑ i = 1 n O y i ( t ) O i ( t ) ⋅ ∂ O i ( t ) ∂ Z 2 ; 1 ( t ) = O 1 ( t ) − y 1 ( t ) \begin{aligned} j = 1 & \Rightarrow -\sum_{i=1}^{n_{\mathcal O}} \frac{y_i^{(t)}}{\mathcal O_i^{(t)}} \cdot \frac{\partial \mathcal O_i^{(t)}}{\partial \mathcal Z_{2;1}^{(t)}} \\ & = \mathcal O_1^{(t)} - y_1^{(t)} \end{aligned} j=1⇒−i=1∑nOOi(t)yi(t)⋅∂Z2;1(t)∂Oi(t)=O1(t)−y1(t)
同理,其他项同第一项操作,最终得到 ∂ L ∂ Z 2 ( t ) \begin{aligned}\frac{\partial \mathcal L}{\partial \mathcal Z_2^{(t)}}\end{aligned} ∂Z2(t)∂L为:
很简练的一个结果,基于交叉熵与
Softmax \text{Softmax} Softmax的反向传播梯度结果。
∂ L ∂ Z 2 ( t ) = [ O j ( t ) − y j ( t ) ] 1 × n O j = 1 , 2 , ⋯ , n O = O ( t ) − y ( t ) \begin{aligned} \frac{\partial \mathcal L}{\partial \mathcal Z_2^{(t)}} & = \left[\mathcal O_j^{(t)} - y_j^{(t)}\right]_{1 \times n_{\mathcal O}} \quad j=1,2,\cdots,n_{\mathcal O}\\ & = \mathcal O^{(t)} - y^{(t)} \end{aligned} ∂Z2(t)∂L=[Oj(t)−yj(t)]1×nOj=1,2,⋯,nO=O(t)−y(t)
但由于 [ y ( t ) ] n O × m × 1 [y^{(t)}]_{n_{\mathcal O} \times m \times 1} [y(t)]nO×m×1是 t t t时刻的真实分布,因此它有如下性质:
即
y ( t ) y^{(t)} y(t)内仅有一个值为
1 1 1其余值均为
0 0 0,这是真实样本给出的分布。
∑ j = 1 n O y j ( t ) = 1 ; y j ( t ) ∈ { 0 , 1 } \sum_{j=1}^{n_{\mathcal O}} y_j^{(t)} = 1;y_j^{(t)} \in \{0,1\} j=1∑nOyj(t)=1;yj(t)∈{0,1}
因此,描述每个分量 ( ∇ Z 2 ( t ) L ) j = [ ∂ L ∂ Z 2 ; j ( t ) ] ( j = 1 , 2 , ⋯ , n O ) \begin{aligned} \left(\nabla_{\mathcal Z_2^{(t)}}\mathcal L \right)_j = \left[\frac{\partial \mathcal L}{\partial \mathcal Z_{2;j}^{(t)}}\right](j=1,2,\cdots,n_{\mathcal O})\end{aligned} (∇Z2(t)L)j=[∂Z2;j(t)∂L](j=1,2,⋯,nO)可表示为如下形式:
其中
1 j , y ( t ) 1_{j,y^{(t)}} 1j,y(t)表示向量
y ( t ) y^{(t)} y(t)中第
j j j个分量
y j ( t ) ∈ { 0 , 1 } y_j^{(t)}\in \{0,1\} yj(t)∈{0,1}的具体结果。
该式子对应《机器学习》(花书) P234 10.2.2 公式10.18。
( ∇ Z 2 ( t ) L ) j = O j ( t ) − 1 j , y ( t ) (\nabla_{\mathcal Z_2^{(t)}}\mathcal L)_j = \mathcal O_j^{(t)} - 1_{j,y^{(t)}} (∇Z2(t)L)j=Oj(t)−1j,y(t)
关于 h ( t ) h^{(t)} h(t)的综合反向传播梯度
继续反向传播,计算梯度 ∂ L ∂ h ( t ) \begin{aligned} \frac{\partial \mathcal L}{\partial h^{(t)}}\end{aligned} ∂h(t)∂L:
后续的线性计算结果不展开写了。
∂ L ∂ h ( t ) = ∂ L ∂ Z 2 ( t ) ⋅ ∂ Z 2 ( t ) ∂ h ( t ) = [ W h ( t ) ⇒ O ( t ) ] T ⋅ ∇ Z 2 ( t ) L \begin{aligned} \frac{\partial \mathcal L}{\partial h^{(t)}} & = \frac{\partial \mathcal L}{\partial \mathcal Z_2^{(t)}} \cdot \frac{\partial \mathcal Z_2^{(t)}}{\partial h^{(t)}} \\ & = \left[\mathcal W_{h^{(t)} \Rightarrow \mathcal O^{(t)}}\right]^T \cdot \nabla_{\mathcal Z_2^{(t)}}\mathcal L \end{aligned} ∂h(t)∂L=∂Z2(t)∂L⋅∂h(t)∂Z2(t)=[Wh(t)⇒O(t)]T⋅∇Z2(t)L
实际上,关于 h ( t ) h^{(t)} h(t)的梯度一共包含两个部分:一个是从 O ( t ) \mathcal O^{(t)} O(t)传播过来的梯度结果;另一个是从 h ( t + 1 ) h^{(t+1)} h(t+1)方向传播过来的梯度结果:
上图并没有描述出来,这里进行补充。
上面的梯度结果是从
O ( t ) \mathcal O^{(t)} O(t)传播下来的梯度。
关于 h ( t + 1 ) h^{(t+1)} h(t+1)向 h ( t ) h^{(t)} h(t)传播的梯度表示为:
其中
∇ h ( t + 1 ) L \nabla_{h^{(t+1)}}\mathcal L ∇h(t+1)L就是
∂ L ∂ h ( t + 1 ) \begin{aligned}\frac{\partial \mathcal L}{\partial h^{(t+1)}}\end{aligned} ∂h(t+1)∂L,它和
∂ L ∂ h ( t ) \begin{aligned}\frac{\partial \mathcal L}{\partial h^{(t)}}\end{aligned} ∂h(t)∂L的情况完全相同,只是下标不同而已。为书写方便,后面不再展开。
( ∂ h ( t + 1 ) ∂ h ( t ) ) T ⋅ ∇ h ( t + 1 ) L ( ∇ h ( t + 1 ) L = [ W h ( t + 1 ) ⇒ O ( t + 1 ) ] T ⋅ ∇ Z 2 ( t + 1 ) L ) \left(\frac{\partial h^{(t+1)}}{\partial h^{(t)}}\right)^T \cdot \nabla_{h^{(t+1)}} \mathcal L \quad \left(\nabla_{h^{(t+1)}} \mathcal L = \left[\mathcal W_{h^{(t+1)} \Rightarrow \mathcal O^{(t+1)}}\right]^T \cdot \nabla_{\mathcal Z_2^{(t+1)}}\mathcal L \right) (∂h(t)∂h(t+1))T⋅∇h(t+1)L(∇h(t+1)L=[Wh(t+1)⇒O(t+1)]T⋅∇Z2(t+1)L)
其中, ∂ h ( t + 1 ) ∂ h ( t ) \begin{aligned}\frac{\partial h^{(t+1)}}{\partial h^{(t)}}\end{aligned} ∂h(t)∂h(t+1)中包含 W h ( t ) ⇒ h ( t + 1 ) \mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}} Wh(t)⇒h(t+1)的梯度以及 Tanh \text{Tanh} Tanh激活函数的梯度:
其中
Diag [ 1 − Tanh ( Z 1 ( t ) ) 2 ] \text{Diag}[1 - \text{Tanh}(\mathcal Z_1^{(t)})^2] Diag[1−Tanh(Z1(t))2]在
数值稳定性一节中介绍过与其相似的
ReLU \text{ReLU} ReLU激活函数的表达形式。它实际上是关于
Tanh \text{Tanh} Tanh梯度的雅可比矩阵。除去对角线元素外,其余位置元素均为
0 0 0。
∂ h ( t + 1 ) ∂ h ( t ) = ∂ h ( t + 1 ) ∂ Z 1 ( t ) ⋅ ∂ Z 1 ( t ) ∂ h ( t ) = Diag [ 1 − Tanh ( Z 1 ( t ) ) 2 ] ⋅ W h ( t ) ⇒ h ( t + 1 ) \begin{aligned} \frac{\partial h^{(t+1)}}{\partial h^{(t)}} & = \frac{\partial h^{(t+1)}}{\partial \mathcal Z_1^{(t)}} \cdot \frac{\partial \mathcal Z_1^{(t)}}{\partial h^{(t)}} \\ & = \text{Diag}[1 - \text{Tanh}(\mathcal Z_1^{(t)})^2] \cdot \mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}} \end{aligned} ∂h(t)∂h(t+1)=∂Z1(t)∂h(t+1)⋅∂h(t)∂Z1(t)=Diag[1−Tanh(Z1(t))2]⋅Wh(t)⇒h(t+1)
因此,从 h ( t + 1 ) h^{(t+1)} h(t+1)方向传播过来的梯度可表示为:
[ W h ( t ) ⇒ h ( t + 1 ) ] T ⋅ ( ∇ h ( t + 1 ) L ) ⋅ Diag [ 1 − Tanh ( Z 1 ( t ) ) 2 ] [\mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}}]^T\cdot (\nabla_{h^{(t+1)}} \mathcal L) \cdot \text{Diag}[1 - \text{Tanh}(\mathcal Z_1^{(t)})^2] [Wh(t)⇒h(t+1)]T⋅(∇h(t+1)L)⋅Diag[1−Tanh(Z1(t))2]
最终,将两个角度的梯度结果相加,得到最终 h ( t ) h^{(t)} h(t)的梯度结果:
对应《机器学习》(花书) P234 10.2.2 公式10.21。
∂ L ∂ h ( t ) = ∂ L ( t ) ∂ h ( t ) + ∂ L ( t + 1 ) ∂ h ( t + 1 ) ⋅ ∂ h ( t + 1 ) ∂ h ( t ) = [ W h ( t ) ⇒ O ( t ) ] T ⋅ ∇ Z 2 ( t ) L + [ W h ( t ) ⇒ h ( t + 1 ) ] T ⋅ ( ∇ h ( t + 1 ) L ) ⋅ Diag [ 1 − Tanh ( Z 1 ( t ) ) 2 ] \begin{aligned}\frac{\partial \mathcal L}{\partial h^{(t)}} & = \frac{\partial \mathcal L^{(t)}}{\partial h^{(t)}} + \frac{\partial \mathcal L^{(t+1)}}{\partial h^{(t+1)}} \cdot \frac{\partial h^{(t+1)}}{\partial h^{(t)}} \\ & = \left[\mathcal W_{h^{(t)} \Rightarrow \mathcal O^{(t)}}\right]^T \cdot \nabla_{\mathcal Z_2^{(t)}}\mathcal L + [\mathcal W_{h^{(t)} \Rightarrow h^{(t+1)}}]^T\cdot (\nabla_{h^{(t+1)}} \mathcal L) \cdot \text{Diag}[1 - \text{Tanh}(\mathcal Z_1^{(t)})^2] \end{aligned} ∂h(t)∂L=∂h(t)∂L(t)+∂h(t+1)∂L(t+1)⋅∂h(t)∂h(t+1)=[Wh(t)⇒O(t)]T⋅∇Z2(t)L+[Wh(t)⇒h(t+1)]T⋅(∇h(t+1)L)⋅Diag[1−Tanh(Z1(t))2]
总结
- Softmax \text{Softmax} Softmax函数与交叉熵结合,其梯度结果变得非常简洁。即输出分布与真实分布间的差值;
- 递归神经网络中,隐变量 h ( t ) ( t = 1 , 2 , ⋯ , T ) h^{(t)}(t=1,2,\cdots,\mathcal T) h(t)(t=1,2,⋯,T)在反向传播过程中,既要获取当前时刻输出 O ( t ) \mathcal O^{(t)} O(t)的梯度,也要获取下一时刻隐变量 h ( t + 1 ) h^{(t+1)} h(t+1)的梯度。
推荐一个递归神经网络的反向传播流程,见下方链接,侵删。
相关参考:
关于 RNN 循环神经网络的反向传播求导
《深度学习》(花书) P233-235 10.2.2 计算循环神经网络的梯度