深度学习笔记之循环神经网络(四)铺垫:Softmax函数的反向传播过程

引言

上一节介绍了循环神经网络前馈计算过程的基本逻辑,以及作为语言模型时,衡量一个语言模型的优劣性指标——困惑度。本节介绍 Softmax \text{Softmax} Softmax函数的反向传播 ( Backward Propagation,BP ) (\text{Backward Propagation,BP}) (Backward Propagation,BP)过程。

铺垫: Softmax \text{Softmax} Softmax的反向传播过程

场景构建

假设一个 L \mathcal L L全连接神经网络用作 C \mathcal C C分类的分类任务,并且已知由 m m m训练样本构成的训练集 D \mathcal D D
D = { ( x ( i ) , y ( i ) ) } i = 1 m \mathcal D = \{(x^{(i)},y^{(i)})\}_{i=1}^m D={(x(i),y(i))}i=1m
中间的计算过程忽略。仅观察输出结果。设每一个 x ( i ) ( i = 1 , 2 , ⋯   , m ) x^{(i)}(i=1,2,\cdots,m) x(i)(i=1,2,,m)的对应预测结果 y ^ ( i ) \hat y^{(i)} y^(i),使用交叉熵 ( CrossEntropy ) (\text{CrossEntropy}) (CrossEntropy)对其计算损失:
L [ y ( i ) , y ^ ( i ) ] = − ∑ j = 1 C y j ( i ) log ⁡ y ^ j ( i ) \mathscr L \left[y^{(i)},\hat y^{(i)}\right] = -\sum_{j=1}^{\mathcal C} y_j^{(i)} \log \hat y_j^{(i)} L[y(i),y^(i)]=j=1Cyj(i)logy^j(i)
相应地,对训练集 D \mathcal D D的损失函数 J ( W ) \mathcal J(\mathcal W) J(W)表示为:
这里将偏置项 b b b忽略掉了。
J ( W ) = 1 m ∑ i = 1 m L [ y ( i ) , y ^ ( i ) ] \mathcal J(\mathcal W) = \frac{1}{m} \sum_{i=1}^m \mathscr L \left[y^{(i)},\hat y^{(i)}\right] J(W)=m1i=1mL[y(i),y^(i)]

关于最后一层神经网络输出 Z ( L ) \mathcal Z^{(\mathcal L)} Z(L) Softmax \text{Softmax} Softmax激活函数的前馈计算过程表示如下:
y ^ = a ( L ) = Softmax ( Z ( L ) ) \hat y = a^{(\mathcal L)} = \text{Softmax}(\mathcal Z^{(\mathcal L)}) y^=a(L)=Softmax(Z(L))

Softmax \text{Softmax} Softmax反向传播过程

以单个样本 ( x , y ) ∈ D (x,y) \in \mathcal D (x,y)D为例。首先计算该样本的损失函数结果 L ( y , y ^ ) \mathscr L(y,\hat y) L(y,y^)关于预测输出 y ^ = a ( L ) \hat y = a^{(\mathcal L)} y^=a(L) 的导数结果:
∂ L ∂ a ( L ) = ∂ ∂ a ( L ) [ − ∑ j = 1 C y j log ⁡ y ^ j ] = ∂ ∂ a ( L ) [ − ( y 1 log ⁡ y ^ 1 + y 2 log ⁡ y ^ 2 + ⋯ + y C log ⁡ y ^ C ) ] = ∂ ∂ a ( L ) [ − ( y 1 log ⁡ a 1 ( L ) + y 2 log ⁡ a 2 ( L ) + ⋯ + y C log ⁡ a C ( L ) ) ] \begin{aligned} \frac{\partial \mathscr L}{\partial a^{(\mathcal L)}} & = \frac{\partial}{\partial a^{(\mathcal L)}} \left[-\sum_{j=1}^{\mathcal C} y_j \log \hat y_j \right] \\ & = \frac{\partial}{\partial a^{(\mathcal L)}} \left[- (y_1 \log \hat y_1 + y_2 \log \hat y_2 + \cdots + y_{\mathcal C} \log \hat y_{\mathcal C}) \right] \\ & = \frac{\partial}{\partial a^{(\mathcal L)}} \left[ - (y_1 \log a_1^{(\mathcal L)} + y_2 \log a_2^{(\mathcal L)} + \cdots + y_{\mathcal C} \log a_{\mathcal C}^{(\mathcal L)})\right] \end{aligned} a(L)L=a(L)[j=1Cyjlogy^j]=a(L)[(y1logy^1+y2logy^2++yClogy^C)]=a(L)[(y1loga1(L)+y2loga2(L)++yClogaC(L))]
很明显, L \mathscr L L表示各维度的连加和,是一个标量;而此时的 a ( L ) a^{(\mathcal L)} a(L)是一个 1 × C 1 \times \mathcal C 1×C的向量。其求导结果表示如下:
标量对向量求导见文章末尾链接,侵删。
∂ L ∂ a ( L ) = [ ∂ L ∂ a 1 ( L ) , ⋯   , ∂ L ∂ a C ( L ) ] = { ∂ ∂ a 1 ( L ) [ − ( y 1 log ⁡ a 1 ( L ) ⏟ a 1 ( L ) 相关 + ⋯ + y C log ⁡ a C ( L ) ⏟ a 1 ( L ) 无关 ) ] , ⋯   , ∂ ∂ a C ( L ) [ − ( y 1 log ⁡ a 1 ( L ) + ⋯ ⏟ a C ( L ) 无关 + y C log ⁡ a C ( L ) ⏟ a C ( L ) 相关 ) ] } = [ − y 1 a 1 ( L ) , ⋯   , − y C a C ( L ) ] = − ( y 1 , ⋯   , y C ) ( a 1 ( L ) , ⋯   , a C ( L ) ) = − y y ^ \begin{aligned} \frac{\partial \mathscr L}{\partial a^{(\mathcal L)}} & = \left[\frac{\partial \mathscr L}{\partial a_1^{(\mathcal L)}},\cdots,\frac{\partial \mathscr L}{\partial a_{\mathcal C}^{(\mathcal L)}}\right]\\ & = \left\{\frac{\partial}{\partial a_1^{(\mathcal L)}} \left[-(\underbrace{y_1 \log a_1^{(\mathcal L)}}_{a_1^{(\mathcal L) 相关}} + \underbrace{\cdots + y_{\mathcal C} \log a_{\mathcal C}^{(\mathcal L)}}_{a_1^{(\mathcal L)无关}})\right],\cdots,\frac{\partial}{\partial a_{\mathcal C}^{(\mathcal L)}} \left[-(\underbrace{y_1 \log a_1^{(\mathcal L)} + \cdots}_{a_{\mathcal C}^{(\mathcal L)无关}} + \underbrace{y_{\mathcal C} \log a_{\mathcal C}^{(\mathcal L)}}_{a_{\mathcal C}^{(\mathcal L)相关}})\right]\right\} \\ & = \left[-\frac{y_1}{a_1^{(\mathcal L)}},\cdots,-\frac{y_{\mathcal C}}{a_{\mathcal C}^{(\mathcal L)}}\right] \\ & = -\frac{(y_1,\cdots,y_{\mathcal C})}{\left(a_1^{(\mathcal L)},\cdots,a_{\mathcal C}^{(\mathcal L)} \right)} \\ & = -\frac{y}{\hat y} \end{aligned} a(L)L=[a1(L)L,,aC(L)L]= a1(L) (a1(L)相关 y1loga1(L)+a1(L)无关 +yClogaC(L)) ,,aC(L) (aC(L)无关 y1loga1(L)++aC(L)相关 yClogaC(L)) =[a1(L)y1,,aC(L)yC]=(a1(L),,aC(L))(y1,,yC)=y^y
继续向前传播,计算 ∂ L ∂ Z ( L ) \begin{aligned}\frac{\partial \mathscr L}{\partial \mathcal Z^{(\mathcal L)}}\end{aligned} Z(L)L
∂ L ∂ Z ( L ) = ∂ L ∂ a ( L ) ⋅ ∂ a ( L ) ∂ Z ( L ) \frac{\partial \mathscr L}{\partial \mathcal Z^{(\mathcal L)}} = \frac{\partial \mathscr L}{\partial a^{(\mathcal L)}} \cdot \frac{\partial a^{(\mathcal L)}}{\partial \mathcal Z^{(\mathcal L)}} Z(L)L=a(L)LZ(L)a(L)
关于 ∂ a ( L ) ∂ Z ( L ) \begin{aligned}\frac{\partial a^{(\mathcal L)}}{\partial \mathcal Z^{(\mathcal L)}}\end{aligned} Z(L)a(L),由于 a ( L ) , Z ( L ) a^{(\mathcal L)},\mathcal Z^{(\mathcal L)} a(L),Z(L)均是 1 × C 1 \times \mathcal C 1×C的向量。其导数结果表示如下:
这是一个 C × C × 1 \mathcal C \times \mathcal C \times 1 C×C×1的三维张量。
∂ a ( L ) ∂ Z ( L ) = [ ∂ a ( L ) ∂ z 1 ( L ) , ⋯   , ∂ a ( L ) ∂ z C ( L ) ] C × C × 1 T = { ∂ ∂ z 1 ( L ) [ exp ⁡ ( Z ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] , ⋯   , ∂ ∂ z C ( L ) [ exp ⁡ ( Z ( L ) ) ∑ i = 1 C exp ⁡ ( z C ( L ) ) ] } C × C × 1 T \begin{aligned} \frac{\partial a^{(\mathcal L)}}{\partial \mathcal Z^{(\mathcal L)}} & = \left[\frac{\partial a^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}},\cdots,\frac{\partial a^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\right]_{\mathcal C \times \mathcal C \times 1}^T \\ & = \left\{\frac{\partial}{\partial z_1^{(\mathcal L)}}\left[\frac{\exp(\mathcal Z^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right],\cdots,\frac{\partial}{\partial z_{\mathcal C}^{(\mathcal L)}} \left[\frac{\exp(\mathcal Z^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_{\mathcal C}^{(\mathcal L)})}\right]\right\}_{\mathcal C \times \mathcal C \times 1}^T \end{aligned} Z(L)a(L)=[z1(L)a(L),,zC(L)a(L)]C×C×1T={z1(L)[i=1Cexp(zi(L))exp(Z(L))],,zC(L)[i=1Cexp(zC(L))exp(Z(L))]}C×C×1T
这里以第一项为例,不可否认的是,它是一个 1 × C 1 \times \mathcal C 1×C向量结果。并且 z 1 ( L ) z_1^{(\mathcal L)} z1(L)是一个标量,它的导数结果表示如下:
其中 exp ⁡ ( Z ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) \begin{aligned}\frac{\exp(\mathcal Z^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\end{aligned} i=1Cexp(zi(L))exp(Z(L))是输出结果 a ( L ) a^{(\mathcal L)} a(L)的第一个分量。记作 a 1 ( L ) a_1^{(\mathcal L)} a1(L).
∂ ∂ z 1 ( L ) [ exp ⁡ ( Z ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] = { ∂ ∂ z 1 ( L ) [ exp ⁡ ( z 1 ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] ⏟ a 1 ( L ) , ⋯   , ∂ ∂ z 1 ( L ) [ exp ⁡ ( z C ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] ⏟ a C ( L ) } 1 × C \frac{\partial}{\partial z_1^{(\mathcal L)}}\left[\frac{\exp(\mathcal Z^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right] = \left\{\frac{\partial}{\partial z_1^{(\mathcal L)}}\underbrace{\left[\frac{\exp(z_1^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right]}_{a_1^{(\mathcal L)}},\cdots,\frac{\partial}{\partial z_1^{(\mathcal L)}}\underbrace{\left[\frac{\exp(z_{\mathcal C}^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right]}_{a_{\mathcal C}^{(\mathcal L)}}\right\}_{1 \times \mathcal C} z1(L)[i=1Cexp(zi(L))exp(Z(L))]= z1(L)a1(L) [i=1Cexp(zi(L))exp(z1(L))],,z1(L)aC(L) [i=1Cexp(zi(L))exp(zC(L))] 1×C
继续以第一项为例,关于 ∂ a 1 ( L ) ∂ z 1 ( L ) \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} z1(L)a1(L)结果表示如下:
除法求导~
其中 [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] ′ \left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]' [i=1Cexp(zi(L))]中与 z 1 ( L ) z_1^{(\mathcal L)} z1(L)相关的只有第一项。因此该项结果为: exp ⁡ ( z i ( L ) ) \exp(z_i^{(L)}) exp(zi(L)).
∂ a 1 ( L ) ∂ z 1 ( L ) = ∂ ∂ z 1 ( L ) [ exp ⁡ ( Z ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] = [ exp ⁡ ( z 1 ( L ) ) ] ′ ⋅ ∑ i = 1 C exp ⁡ ( z i ( L ) ) − exp ⁡ ( z 1 ( L ) ) ⋅ [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] ′ [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] 2 = exp ⁡ ( z 1 ( L ) ) ⋅ ∑ i = 1 C exp ⁡ ( z i ( L ) ) − [ exp ⁡ ( z 1 ( L ) ) ] 2 [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] 2 \begin{aligned} \frac{\partial a_1^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}} & = \frac{\partial}{\partial z_1^{(\mathcal L)}}\left[\frac{\exp(\mathcal Z^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right] \\ & = \frac{\left[\exp(z_1^{(\mathcal L)})\right]' \cdot \sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)}) - \exp(z_1^{(\mathcal L)}) \cdot \left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]'}{\left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]^2} \\ & = \frac{\exp(z_1^{(\mathcal L)}) \cdot \sum_{i=1}^{\mathcal C}\exp(z_i^{(\mathcal L)}) - \left[\exp(z_1^{(L)})\right]^2}{\left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]^2} \end{aligned} z1(L)a1(L)=z1(L)[i=1Cexp(zi(L))exp(Z(L))]=[i=1Cexp(zi(L))]2[exp(z1(L))]i=1Cexp(zi(L))exp(z1(L))[i=1Cexp(zi(L))]=[i=1Cexp(zi(L))]2exp(z1(L))i=1Cexp(zi(L))[exp(z1(L))]2
分子提出 exp ⁡ ( z 1 ( L ) ) \exp(z_1^{(\mathcal L)}) exp(z1(L)),分母平方项展开:
∂ a 1 ( L ) ∂ z 1 ( L ) = exp ⁡ ( z 1 ( L ) ) ⋅ [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) − exp ⁡ ( z 1 ( L ) ) ] [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] 2 = exp ⁡ ( z 1 ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ⋅ ∑ i = 1 C exp ⁡ ( z i ( L ) ) − exp ⁡ ( z 1 ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) = exp ⁡ ( z 1 ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ⋅ [ 1 − exp ⁡ ( z 1 ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] = a 1 ( L ) ⋅ ( 1 − a 1 ( L ) ) \begin{aligned} \frac{\partial a_1^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}} & = \frac{\exp(z_1^{(\mathcal L)}) \cdot \left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)}) - \exp(z_1^{(\mathcal L)})\right]}{\left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]^2} \\ & = \frac{\exp(z_1^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})} \cdot \frac{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)}) - \exp(z_1^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})} \\ & = \frac{\exp(z_1^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})} \cdot \left[1 - \frac{\exp(z_1^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})}\right] \\ & = a_1^{(\mathcal L)} \cdot (1 - a_1^{(\mathcal L)}) \end{aligned} z1(L)a1(L)=[i=1Cexp(zi(L))]2exp(z1(L))[i=1Cexp(zi(L))exp(z1(L))]=i=1Cexp(zi(L))exp(z1(L))i=1Cexp(zi(L))i=1Cexp(zi(L))exp(z1(L))=i=1Cexp(zi(L))exp(z1(L))[1i=1Cexp(zi(L))exp(z1(L))]=a1(L)(1a1(L))

同理,关于两个下标参数 p , q p,q p,q;当 p = q p=q p=q时,有:
∂ a q ( L ) ∂ z p ( L ) = a p ( L ) ⋅ ( 1 − a p ( L ) ) p , q ∈ { 1 , 2 , ⋯   , C } ; p = q \frac{\partial a_q^{(\mathcal L)}}{\partial z_p^{(\mathcal L)}} = a_p^{(\mathcal L)} \cdot (1 - a_p^{(\mathcal L)}) \quad p,q \in \{1,2,\cdots,\mathcal C\};p = q zp(L)aq(L)=ap(L)(1ap(L))p,q{1,2,,C};p=q
p ≠ q p \neq q p=q时,对应结果表示为:
其中 [ ∂ exp ⁡ ( z q ( L ) ) ∂ z p ( L ) ] p ≠ q = 0 \begin{aligned}\left[\frac{\partial \exp(z_q^{(\mathcal L)})}{\partial z_p^{(\mathcal L)}}\right]_{p \neq q} = 0\end{aligned} [zp(L)exp(zq(L))]p=q=0恒成立。
∂ a q ( L ) ∂ z p ( L ) = 0 ⋅ ∑ i = 1 C exp ⁡ ( z i ( L ) ) − exp ⁡ ( z q ( L ) ) ⋅ exp ⁡ ( z p ( L ) ) [ ∑ i = 1 C exp ⁡ ( z i ( L ) ) ] 2 = − e x p ( z q ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) ⋅ exp ⁡ ( z p ( L ) ) ∑ i = 1 C exp ⁡ ( z i ( L ) ) = − a p ( L ) ⋅ a q ( L ) \begin{aligned} \frac{\partial a_q^{(\mathcal L)}}{\partial z_p^{(\mathcal L)}} & = \frac{0 \cdot \sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)}) - \exp(z_q^{(\mathcal L)})\cdot \exp(z_p^{(\mathcal L)})}{\left[\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})\right]^2} \\ & = - \frac{exp(z_q^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})} \cdot \frac{\exp(z_p^{(\mathcal L)})}{\sum_{i=1}^{\mathcal C} \exp(z_i^{(\mathcal L)})} \\ & = -a_p^{(\mathcal L)} \cdot a_q^{(\mathcal L)} \end{aligned} zp(L)aq(L)=[i=1Cexp(zi(L))]20i=1Cexp(zi(L))exp(zq(L))exp(zp(L))=i=1Cexp(zi(L))exp(zq(L))i=1Cexp(zi(L))exp(zp(L))=ap(L)aq(L)
至此, [ ∂ a ( L ) ∂ Z ( L ) ] C × C × 1 \begin{aligned}\left[\frac{\partial a^{(\mathcal L)}}{\partial \mathcal Z^{(\mathcal L)}}\right]_{\mathcal C \times \mathcal C \times 1}\end{aligned} [Z(L)a(L)]C×C×1中的所有项均可进行表示。将该三维张量进行压缩(删除最后一个维度),可以得到一个雅可比矩阵 ( Jacobian Matrix ) (\text{Jacobian Matrix}) (Jacobian Matrix)
矩阵中的每一个元素均可使用上述两种方式进行表达。
∂ a ( L ) ∂ Z ( L ) = [ ∂ a 1 ( L ) ∂ z 1 ( L ) ∂ a 1 ( L ) ∂ z 2 ( L ) ⋯ ∂ a 1 ( L ) ∂ z C ( L ) ∂ a 2 ( L ) ∂ z 1 ( L ) ∂ a 2 ( L ) ∂ z 2 ( L ) ⋯ ∂ a 2 ( L ) ∂ z C ( L ) ⋮ ⋮ ⋱ ⋮ ∂ a C ( L ) ∂ z 1 ( L ) ∂ a C ( L ) ∂ z 2 ( L ) ⋯ ∂ a C ( L ) ∂ z C ( L ) ] C × C \frac{\partial a^{(\mathcal L)}}{\partial \mathcal Z^{(\mathcal L)}} = \begin{bmatrix} \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \vdots & \vdots &\ddots & \vdots\\ \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \end{bmatrix}_{\mathcal C \times \mathcal C} Z(L)a(L)= z1(L)a1(L)z1(L)a2(L)z1(L)aC(L)z2(L)a1(L)z2(L)a2(L)z2(L)aC(L)zC(L)a1(L)zC(L)a2(L)zC(L)aC(L) C×C
此时,对 ∂ L ∂ Z ( L ) \begin{aligned}\frac{\partial \mathscr L}{\partial \mathcal Z^{(\mathcal L)}}\end{aligned} Z(L)L进行表达:
其结果是一个 1 × C 1 \times \mathcal C 1×C的向量格式。
∂ L ∂ a ( L ) ⋅ ∂ a ( L ) ∂ Z ( L ) = [ − y 1 a 1 ( L ) , ⋯   , − y C a C ( L ) ] ⋅ [ ∂ a 1 ( L ) ∂ z 1 ( L ) ∂ a 1 ( L ) ∂ z 2 ( L ) ⋯ ∂ a 1 ( L ) ∂ z C ( L ) ∂ a 2 ( L ) ∂ z 1 ( L ) ∂ a 2 ( L ) ∂ z 2 ( L ) ⋯ ∂ a 2 ( L ) ∂ z C ( L ) ⋮ ⋮ ⋱ ⋮ ∂ a C ( L ) ∂ z 1 ( L ) ∂ a C ( L ) ∂ z 2 ( L ) ⋯ ∂ a C ( L ) ∂ z C ( L ) ] C × C = [ − ∑ i = 1 C y i a i ( L ) ⋅ ∂ a i ( L ) ∂ z 1 ( L ) , ⋯   , − ∑ i = 1 C y i a i ( L ) ⋅ ∂ a i ( L ) ∂ z C ( L ) ] 1 × C = [ − ∑ i = 1 C y i a i ( L ) ⋅ ∂ a i ( L ) ∂ z j ( L ) ] 1 × C j = 1 , 2 , ⋯   , C \begin{aligned} \frac{\partial \mathscr L}{\partial a^{(\mathcal L)}} \cdot \frac{\partial a^{(\mathcal L)}}{\partial\mathcal Z^{(\mathcal L)}} & = \left[-\frac{y_1}{a_1^{(\mathcal L)}},\cdots,-\frac{y_{\mathcal C}}{a_{\mathcal C}^{(\mathcal L)}}\right] \cdot \begin{bmatrix} \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_1^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_2^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \vdots & \vdots &\ddots & \vdots\\ \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}}\end{aligned} & \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_2^{(\mathcal L)}}\end{aligned} & \cdots& \begin{aligned}\frac{\partial a_{\mathcal C}^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\end{aligned} \\ \end{bmatrix}_{\mathcal C \times \mathcal C} \\ & = \left[- \sum_{i=1}^{\mathcal C} \frac{y_i}{a_i^{(\mathcal L)}} \cdot \frac{\partial a_i^{(\mathcal L)}}{\partial z_1^{(\mathcal L)}},\cdots,- \sum_{i=1}^{\mathcal C} \frac{y_i}{a_i^{(\mathcal L)}} \cdot \frac{\partial a_i^{(\mathcal L)}}{\partial z_{\mathcal C}^{(\mathcal L)}}\right]_{1 \times \mathcal C} \\ & = \left[- \sum_{i=1}^{\mathcal C} \frac{y_i}{a_i^{(\mathcal L)}} \cdot \frac{\partial a_i^{(\mathcal L)}}{\partial z_j^{(\mathcal L)}}\right]_{1 \times \mathcal C} \quad j =1,2,\cdots,\mathcal C \end{aligned} a(L)LZ(L)a(L)=[a1(L)y1,,aC(L)yC] z1(L)a1(L)z1(L)a2(L)z1(L)aC(L)z2(L)a1(L)z2(L)a2(L)z2(L)aC(L)zC(L)a1(L)zC(L)a2(L)zC(L)aC(L) C×C=[i=1Cai(L)yiz1(L)ai(L),,i=1Cai(L)yizC(L)ai(L)]1×C=[i=1Cai(L)yizj(L)ai(L)]1×Cj=1,2,,C
∂ a i ( L ) ∂ z j ( L ) ( i , j ∈ { 1 , 2 , ⋯   , C } ) = { a i ( L ) ( 1 − a j ( L ) ) i = j − a i ( L ) ⋅ a j ( L ) i ≠ j \begin{aligned}\frac{\partial a_i^{(\mathcal L)}}{\partial z_j^{(\mathcal L)}}(i,j \in \{1,2,\cdots,\mathcal C\}) = \begin{cases}a_i^{(\mathcal L)}(1 - a_j^{(\mathcal L)}) \quad i = j \\ -a_i^{(\mathcal L)} \cdot a_j^{(\mathcal L)} \quad i \neq j \end{cases}\end{aligned} zj(L)ai(L)(i,j{1,2,,C})={ai(L)(1aj(L))i=jai(L)aj(L)i=j两种情况代入到上式中:
可以消掉 a i ( L ) a_i^{(\mathcal L)} ai(L).
需要注意的是,这里的连加号 ∑ i = 1 C \sum_{i=1}^{\mathcal C} i=1C是均满足条件时的累加结果。如果只有一项满足条件,那么 C = 1 \mathcal C = 1 C=1,以此类推。
− ∑ i = 1 C y i a i ( L ) ⋅ ∂ a i ( L ) ∂ z j ( L ) = { ∑ i = 1 C y i ⋅ a j ( L ) − y i i = j ∑ i = 1 C y i ⋅ a j ( L ) i ≠ j - \sum_{i=1}^{\mathcal C} \frac{y_i}{a_i^{(\mathcal L)}} \cdot \frac{\partial a_i^{(\mathcal L)}}{\partial z_j^{(\mathcal L)}} = \begin{cases} \begin{aligned} & \sum_{i=1}^{\mathcal C} y_i \cdot a_j^{(\mathcal L)} - y_i \quad i = j \\ & \sum_{i=1}^{\mathcal C} y_i \cdot a_j^{(\mathcal L)} \quad i \neq j \end{aligned} \end{cases} i=1Cai(L)yizj(L)ai(L)= i=1Cyiaj(L)yii=ji=1Cyiaj(L)i=j
关于 [ ∂ L ∂ a ( L ) ⋅ ∂ a ( L ) ∂ Z ( L ) ] 1 × C \begin{aligned} \left[\frac{\partial \mathscr L}{\partial a^{(\mathcal L)}} \cdot \frac{\partial a^{(\mathcal L)}}{\partial\mathcal Z^{(\mathcal L)}}\right]_{1 \times \mathcal C}\end{aligned} [a(L)LZ(L)a(L)]1×C中的结果,其每一项内连加项中,只有一项是 i = j i = j i=j的情况。因而对 1 × C 1 \times \mathcal C 1×C向量中的每一项均执行如下操作:
就是分成 i = j i = j i=j 1 1 1项与 i ≠ j i \neq j i=j C − 1 \mathcal C - 1 C1项分别运算。
其中 ∑ i = 1 C y i \begin{aligned}\sum_{i=1}^{\mathcal C}y_i\end{aligned} i=1Cyi是真实标签向量各分量之和。而真实标签中只有 { 0 , 1 } \{0,1\} {0,1}两种元素(是该分类的为 1 1 1,不是该分类的为 0 0 0)因此, ∑ i = 1 C y i \begin{aligned}\sum_{i=1}^{\mathcal C}y_i\end{aligned} i=1Cyi = 1.
− ∑ i = 1 C y i a i ( L ) ⋅ ∂ a i ( L ) ∂ z j ( L ) = − y j + y j ⋅ a j ( L ) ⏟ i = j + ∑ i ≠ j y i ⋅ a i ( L ) ⏟ i ≠ j = − y j + ( y j ⋅ a j ( L ) + ∑ i ≠ j y i ⋅ a j ( L ) ) = − y j + a j ( L ) ⋅ ∑ i = 1 C y i = a j ( L ) − y j \begin{aligned} -\sum_{i=1}^{\mathcal C} \frac{y_i}{a_i^{(\mathcal L)}} \cdot \frac{\partial a_i^{(\mathcal L)}}{\partial z_j^{(\mathcal L)}} & = \underbrace{-y_j + y_j \cdot a_j^{(\mathcal L)}}_{i = j} + \underbrace{\sum_{i \neq j} y_i \cdot a_i^{(\mathcal L)}}_{i \neq j} \\ & = -y_j + \left(y_j \cdot a_j^{(\mathcal L)} + \sum_{i \neq j} y_i \cdot a_j^{(\mathcal L)}\right) \\ & = -y_j + a_j^{(\mathcal L)} \cdot \sum_{i=1}^{\mathcal C}y_i \\ & = a_j^{(\mathcal L)} - y_j \end{aligned} i=1Cai(L)yizj(L)ai(L)=i=j yj+yjaj(L)+i=j i=jyiai(L)=yj+ yjaj(L)+i=jyiaj(L) =yj+aj(L)i=1Cyi=aj(L)yj
这仅仅是一个分量的结果,所有分量的结果组成一个 1 × C 1 \times \mathcal C 1×C向量
[ a j ( L ) − y j ] 1 × C j = 1 , 2 , ⋯   , C ⇒ a ( L ) − y \left[a_j^{(\mathcal L)} - y_j\right]_{1 \times \mathcal C} \quad j = 1,2,\cdots,\mathcal C \Rightarrow a^{(\mathcal L)} - y [aj(L)yj]1×Cj=1,2,,Ca(L)y

相关参考:
向量对向量求导
关于 Softmax 回归的反向传播求导数过程

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静静的喝酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值