We shall meet in place where there is no darkness.

0%

矩阵微积分(一):标量对矩阵求导

矩阵微积分是多元函数微积分在矩阵空间中进行表达时采用的一种简化形式,常用于机器学习中,可通过将多个变量记为向量或矩阵的形式来简化运算。但目前矩阵微积分的相关概念定义尚未统一,运算方式亦繁乱无绪;若按许多人之习惯做法,显式依照定义进行计算,则又因为对向量/矩阵进行逐元素运算而破坏整体性,计算难度颇高,多须借助查表方能完成运算。本文通过引入矩阵微分算子的方式重新维护了整体性,大幅降低了运算难度,同时亦使得其定义更契合已有的标量微积分运算。

本文是矩阵求导术(上)的笔记,对全文缺漏处进行了部分完善,同时在证明与例题处亦重新进行了推导,十分感谢作者长躯鬼侠

符号约定

在引入矩阵微积分进行计算的公式中,往往混杂标量、向量、矩阵,若按传统习惯使用同一风格字体进行表述,公式复杂时难以进行分辨;为简化辨认,约定如下符号使用规范:

  • 用拉丁字母表示的数学常量(e,i\mathrm{e}, \mathrm{i}, e.g.)、函数/算子名称(dfdx,sinx\dfrac{\mathrm{d}f}{\mathrm{d}x},\sin{x} , e.g.)、其他相关记号(argmin\textrm{argmin}, e.g.),用 Roman 正体表示。
  • 用拉丁字母表示的标量(x,y,zx,y,z, e.g.)、用希腊字母表示的数学常量或标量(π,α\pi, \alpha, e.g.),用 Italic 斜体表示。
  • 向量(x,y,π,α\mathbf{x}, \mathbf{y},{\boldsymbol \pi},{\boldsymbol \alpha}, e.g.),用小写 Bold 粗体表示。
  • 矩阵(A,B,Π,Σ\mathbf{A}, \mathbf{B}, {\bf \Pi, \Sigma}, e.g.),用大写 Bold 粗体表示。
  • 域(R,C\mathfrak{R,C}, e.g.),用 Fraktur 哥特体表示。
  • 若非特殊注明,计算过程均采用分子记法,文中所有函数均属于 C1C^{1}

定义

标量 f:RnRf: \mathfrak{R}^{n} \rightarrow \mathfrak{R}对向量 xRn\mathbf{x} \in \mathfrak{R}^{n}求导,所得结果为ffx\bf x 各分量分别求导构成的向量,记为

fx=[fx1fx2fx3fxn]=xf\frac{\partial f}{\partial \mathbf{x}}= \begin{bmatrix} \frac{\partial f}{\partial x_1} & \frac{\partial f}{\partial x_2} & \frac{\partial f}{\partial x_3} & \cdots & \frac{\partial f}{\partial x_n} \end{bmatrix}^{\top} = \nabla_{\mathbf{x}}f

其中 \nabla是 Nabla 算子,表示 ffx\mathbf{x} 的梯度。

标量 g:Rn×mRg: \mathfrak{R}^{n \times m} \rightarrow \mathfrak{R}对矩阵 XRn×m\mathbf{X} \in \mathfrak{R}^{n \times m}求导,所得结果为ggX\bf X 各元素分别求导构成的矩阵,记为

gX=[gXij]n×m\frac{\partial g}{\partial \mathbf{X}}=[\frac{\partial g}{\partial X_{ij}}]_{n \times m}

我们已知,ffx\mathbf{x} 的全微分为

df=i=1nfxidxi=fxdx\mathrm{d}f = \sum\limits_{i=1}^{n}\frac{\partial f}{\partial x_{i}}\mathrm{d}x_{i}=\frac{\partial f}{\partial \mathbf{x}}^{\top}\mathrm{d}\mathbf{x}

其中

dx=[dx1dx2dxn]\mathrm{d} \mathbf{x} = \begin{bmatrix} \mathrm{d}x_{1} & \mathrm{d}x_{2} & \cdots & \mathrm{d}x_{n} \end{bmatrix}^{\top}

相似的,我们可定义 ggX\mathbf{X} 的微分为

dg=tr(gXdX)\mathrm{d}g=\mathrm{tr}(\frac{\partial g}{\partial \mathbf{X}}^{\top}\mathrm{d}\mathbf{X})

其中 tr\mathrm{tr} 表示矩阵的迹,且

dX=[dXij]n×m\mathrm{d}\mathbf{X} = [\mathrm{d}X_{ij}]_{n \times m}

此时,微分算子 d\mathrm{d} 的运算对象是一个矩阵;我们称此时进行的运算为矩阵微分

矩阵微分的运算法则

  • 线性、矩阵乘法、转置、迹、常量的微分

    1. d(X±Y)=dX±dY\mathrm{d}(\mathbf{X \pm Y})=\mathrm{d}{\mathbf{X}} \pm \mathrm{d}\mathbf{Y}d(rX)=rdX,  rR{\rm d}(r{\bf X}) = r{\rm d}{\bf X},\;r \in {\frak R}

    2. d(XY)=(dX)Y+XdY\mathrm{d}(\mathbf{XY})=(\mathrm{d}\mathbf{X})\mathbf{Y}+\mathbf{X}\mathrm{d}\mathbf{Y}

    3. d(X)=(dX)\mathrm{d}(\mathbf{X}^{\top}) = (\mathrm{d}\mathbf{X})^{\top}

    4. d(tr(X))=tr(dX)\mathrm{d}(\mathrm{tr}(\mathbf{X})) = \mathrm{tr}(\mathrm{d}\mathbf{X})

    5. d(A)=0{\rm d}({\bf A})={\bf 0}A{\bf A} 是常量。

      推论:d(AXB)=A(dX)B{\rm d}({\bf AXB})={\bf A}({\rm d}{\bf X}){\bf B}A,B\bf A,B 是常量。

    证明略。

  • 矩阵的逆

    dX1=X1(dX)X1\mathrm{d}\mathbf{X}^{-1}=-\mathbf{X}^{-1}(\mathrm{d}\mathbf{X})\mathbf{X}^{-1}

    证:由 XX1=I\mathbf{X}\mathbf{X}^{-1}=\mathbf{I} 两侧取微分即得。

  • Jacobi’s Formula

    dX=tr(XdX)\mathrm{d}|\mathbf{X}|=\mathrm{tr}(\mathbf{X}^{*}\mathrm{d}\mathbf{X})

    其中 X\mathbf{X}^{*}X\mathbf{X} 的伴随矩阵(而非X\mathbf{X} 的复共轭)。

    特别地,当 X\mathbf{X} 可逆时,有

    dX=Xtr(X1dX)\mathrm{d}|\mathbf{X}|=|\mathbf{X}|\mathrm{tr}(\mathbf{X}^{-1}\mathrm{d}\mathbf{X})

    证:

    LEMMA.{\bf L{\scriptsize EMMA}}.A,BRn×n\mathbf{A,B} \in \mathfrak{R}^{n \times n},有tr(AB)=i=1nj=1nAijBij\mathrm{tr}(\mathbf{A^{\top}B})=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\mathbf{A}_{ij}\mathbf{B}_{ij}

    PROOF.{\bf P{\scriptsize ROOF}}.由矩阵乘法,(AB)jk=i=1nAjiBik(\mathbf{AB})_{jk} = \sum\limits_{i=1}^{n}\mathbf{A}_{ji}\mathbf{B}_{ik},故有(AB)jk=i=1nAijBik(\mathbf{A^{\top}B})_{jk}=\sum\limits_{i=1}^{n}\mathbf{A}_{ij}\mathbf{B}_{ik}

    因此,tr(AB)=i=1n(AB)ii=i=1nj=1nAijBij\mathrm{tr}(\mathbf{A^{\top}B}) = \sum\limits_{i=1}^{n}(\mathbf{A^{\top}B})_{ii} = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\mathbf{A}_{ij}\mathbf{B}_{ij} 。证毕。

    JACOBIS  FORMULA.{\bf J{\scriptsize ACOBI'S}\;F{\scriptsize ORMULA}}.XRn×n{\bf X} \in {\frak R}^{n \times n},有dX=tr(XdX){\rm d}|{\bf X}|= {\rm tr}({\bf X^{*}}{\rm d}{\bf X})

    PROOF.{\bf P{\scriptsize ROOF}}.X\bf X 的第ii 行做 Laplace 展开,有

    X=j=1n(X)ijXij  ,  i{1,2,,n}|{\bf X}|=\sum\limits_{j=1}^{n}({\bf X^{*}})^{\top}_{ij}X_{ij}\;,\;\forall i\in\{1,2, \: \cdots \:, n\}

    又,矩阵的行列式运算可视作 det:Rn2R{\rm det}: {\frak R}^{n^2} \rightarrow {\frak R},因此可使用向量的全微分公式

    dX=i=1nj=1nXXijdXij{\rm d|{\bf X|}}=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\frac{\partial {\bf |X|}}{\partial X_{ij}}{\rm d}{X_{ij}}

    现在,需要求出 XXij\dfrac{\partial {\bf |X|}}{\partial X_{ij}}。考虑 X{\bf |X|}的 Laplace 展开式中 ii 可任取{1,2,.n}\{1,2,\:\cdots\:.n\} 中的任一数值,不妨假定其与XijX_{ij} 保持一致。因此,

    XXij=k=1n(X)ikXikXij=k=1n(X)ikXikXij=k=1n(X)ikXijXik+k=1nXikXij(X)ik\begin{aligned} \frac{\partial {\bf |X|}}{\partial X_{ij}}&= \frac{\partial\sum\limits_{k=1}^{n}({\bf X^{*}})^{\top}_{ik}X_{ik}}{\partial X_{ij}}\\ &=\sum\limits_{k=1}^{n}\frac{\partial ({\bf X^{*}})^{\top}_{ik}X_{ik}}{\partial X_{ij}}\\ &=\sum\limits_{k=1}^{n}\frac{\partial({\bf X^{*}})^{\top}_{ik}}{\partial X_{ij}}X_{ik}+\sum\limits_{k=1}^{n}\frac{\partial X_{ik}}{\partial X_{ij}}({\bf X^{*}})^{\top}_{ik} \end{aligned}

    考虑代数余子式的构造方式,可得知 (X)ik({\bf X^*})^{\top}_{ik}并非XijX_{ij} 的函数。因此,

    k=1n(X)ikXijXik=0\sum\limits_{k=1}^{n}\frac{\partial ({\bf X^*})^{\top}_{ik}}{\partial X_{ij}}X_{ik}=0

    类似地,由矩阵元素相互之间的独立性,可得知 Xik  (kj)X_{ik}\;(k \neq j)并非XijX_{ij} 的函数。因此,

    k=1nXikXij(X)ik=XijXij(X)ij=(X)ij\sum\limits_{k=1}^{n}\frac{\partial X_{ik}}{\partial X_{ij}}({\bf X^*})^{\top}_{ik}=\frac{\partial X_{ij}}{\partial X_{ij}}({\bf X^*})^{\top}_{ij}=({\bf X^*})^{\top}_{ij}

    因此,

    XXij=(X)ij\frac{\partial {\bf |X|}}{\partial X_{ij}}=({\bf X^*})^{\top}_{ij}

    故有

    dX=i=1nj=1n(X)ijdXij{\rm d}{\bf |X|}=\sum_{i=1}^{n}\sum_{j=1}^{n}({\bf X^*})^{\top}_{ij}{\rm d}X_{ij}

    由前证引理,有

    dX=tr(XdX){\rm d}{\bf |X|}={\rm tr}({\bf X^*}{\rm d}{\bf X})

    证毕。

  • 矩阵的 Hadamard 积

    定义矩阵 ARn×m{\bf A} \in {\frak R}^{n \times m}BRn×m{\bf B} \in {\frak R}^{n \times m}的 Hadamard 积为[AijBij]n×m[A_{ij}B_{ij}]_{n \times m},记作AB{\bf A}\odot {\bf B} ,则有结论

    d(AB)=dAB+AdB{\rm d}({ {\bf A \odot B})}={\rm d}{\bf A \odot B}+{\bf A}\odot{\rm d}{\bf B}

    证明略。

  • 逐元素标量函数

    我们规定:若有一个函数 f:RRf:{\frak R \rightarrow R}和矩阵 ARn×m{\bf A} \in {\frak R}^{n \times m},则定义记号 f(A)=[f(Aij)]n×mf({\bf A}) = [f(A_{ij})]_{n \times m}f(A)=[f(Aij)]n×mf^{\:'}({\bf A})=[f^{\:'}(A_{ij})]_{n \times m} 。 则有结论

    df(A)=f(A)dA{\rm d}f({\bf A})=f^{\:'}({\bf A})\odot{\rm d}{\bf A}

    证明略。

  • 全微分法则

    A1Rm1×n1,A2Rm2×n2,,ApRmp×np{\bf A}_1 \in {\frak R}^{m_1 \times n_1},{\bf A}_2 \in {\frak R}^{m_2 \times n_2},\cdots,{\bf A}_p \in {\frak R}^{m_p \times n_p},考虑 fR\exists f \in {\frak R}f=σ(A1,A2,,Ap)f = \sigma({\bf A}_1,{\bf A}_2,\cdots,{\bf A}_p) ,则有

    df=i=1ptr(fAidAi){\rm d}f=\sum_{i=1}^{p}{\rm tr}(\frac{\partial f}{\partial {\bf A}_i}^\top{\rm d}{\bf A}_i)

    证明略。

标量对矩阵求导

求导法则与迹技巧

f:Rn×mRf:{\frak R^{\it n \times m} \rightarrow R}, ARn×m{\bf A} \in {\frak R}^{n \times m};考虑从矩阵微分的定义式 df=tr(fAdA){\rm d}f={\rm tr}(\dfrac{\partial f}{\partial {\bf A}}^{\top}{\rm d}{\bf A})无法获得fA\dfrac{\partial f}{\partial {\bf A}} 的显式解,在左侧的df{\rm d}f 已知的情况下,必须将其写为右侧的形式方可求得导数fA\dfrac{\partial f}{\partial {\bf A}} 。为此,我们引入矩阵的迹运算的一些性质,称作迹技巧

  • 标量套上迹:a=tr(a),  aRa = {\rm tr}(a),\;a \in {\frak R}

  • 转置:tr(A)=tr(A){\rm tr}({\bf A^{\top}})={\rm tr}({\bf A})

  • 线性:tr(A±B)=tr(A)±tr(B),  tr(rA)=rtr(A),  rR{\rm tr}({\bf A \pm B})={\rm tr}({\bf A}) \pm {\rm tr}({\bf B}),\;{\rm tr}(r{\bf A})=r{\rm tr}({\bf A}),\;r\in {\frak R}

  • 矩阵乘法:

    1. A,BRn×m{\bf A,B^{\top}} \in {\frak R^{\it n \times m}},则tr(AB)=tr(BA)=i=1nj=1mAijBji{\rm tr}({\bf AB}) = {\rm tr}({\bf BA}) = \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}A_{ij}B_{ji}

    2. A1,A2,,AnRn×n{\bf A_1,A_2,\cdots,A_n} \in {\frak R}^{n \times n} ,则

      tr(A1A2A3An)=tr(A2A3AnA1)=tr(A3A4AnA1A2)==tr(An1AnA1An2)=tr(AnA1An2An1)\begin{aligned} {\rm tr}({\bf A_1 A_2 A_3 \cdots A_n}) &= {\rm tr}({\bf A_2 A_3 \cdots A_n A_1}) \\ &= {\rm tr}({\bf A_3 A_4 \cdots A_n A_1 A_2}) \\ &= \cdots \\ &= {\rm tr}({\bf A_{n-1}A_n A_1 \cdots A_{n-2}}) \\ &= {\rm tr}({\bf A_n A_1 \cdots A_{n-2}A_{n-1}}) \end{aligned}

这称为循环不变性。更一般地,对 A1,A2,An\bf A_1 , A_2, \cdots A_n 不全为方阵的情况,在所有可能出现的循环乘积中,凡是存在的循环乘积其迹都相同。

当且仅当 A1A2AnRn×n{\bf A_1 A_2 \cdots A_n} \in {\frak R}^{n \times n}且均为对称矩阵时,这些矩阵以所有可能的n!n! 个顺序排列构成的乘积的迹均相同。

  • 矩阵乘法与 Hadamard 积交换

    A,B,CRn×m{\bf A,B,C} \in {\frak R}^{n \times m} ,则

    tr(A(BC))=tr((AB)C)=i=1nj=1mAijBijCij{\rm tr}({\bf A}^{\top}({\bf B \odot C})) = {\rm tr}(({\bf A \odot B})^\top {\bf C})=\sum\limits_{i=1}^n \sum\limits_{j=1}^m A_{ij}B_{ij}C_{ij}

由此可以得出结论:已知 f:Rn×mRf:{\frak R^{\it n \times m} \rightarrow R}, ARn×m{\bf A} \in {\frak R}^{n \times m}时,若 ff是由矩阵加减乘法、逆、行列式、逐元素函数等运算构成,则可运用矩阵微分的运算法则在有限次内得到 df{\rm d}f,由于df{\rm d}f 是标量,故可直接套上迹运算符后,将tr(df){\rm tr}({\rm d}f) 内除dA{\rm d}\bf A 外其他项交换至其左侧,写成tr(fAdA){\rm tr}(\dfrac{\partial f}{\partial \bf A}^\top {\rm d}{\bf A}) 的形式,即可获得导数。

复合函数求导法则

f:Rn×mRf:{\frak R^{\it n \times m} \rightarrow R}σ:Rp×qRn×m\sigma : {\frak R^{\it p \times q} \rightarrow R^{\it n \times m}}XRp×q{\bf X} \in {\frak R}^{p \times q}YRn×m{\bf Y} \in {\frak R}^{n \times m},且Y=σ(X){\bf Y} = \sigma({\bf X}) ,在已知fY\dfrac {\partial f}{\partial {\bf Y}} 的情况下,如何求fX\dfrac{\partial f}{\partial {\bf X}}

显然,此处链式法则 fX=fYYX\dfrac{\partial f}{\partial {\bf X}}=\dfrac{\partial f}{\partial {\bf Y}}\dfrac{\partial {\bf Y}}{\partial {\bf X}}不成立,因为此处 YX\dfrac{\partial {\bf Y}}{\partial {\bf X}}是未定义的。因此,考虑矩阵微分定义式 df=tr(fYdY){\rm d} f={\rm tr}(\dfrac{\partial f}{\partial {\bf Y}}^\top {\rm d}{\bf Y}),可将 Y=σ(X){\bf Y}=\sigma({\bf X}) 代入dY{\rm d}\bf Y 后将原式写作df=tr(g(fY,X)dX){\rm d}f={\rm tr}(g(\dfrac{\partial f}{\partial \bf Y},\bf X)^\top {\rm d}{\bf X}) 的形式,此时即有fX=g(fY,X)\dfrac{\partial f}{\partial \bf X}=g({\dfrac{\partial f}{\partial \bf Y}, \bf X})

EXAMPLE.{\bf E{\scriptsize XAMPLE}}.ARn×p,  BRq×m{\bf A} \in {\frak R}^{n \times p},\;{\bf B} \in {\frak R}^{q \times m}Y=AXB\bf Y=AXB ,已知fY\dfrac{\partial f}{\partial \bf Y} ,求fX\dfrac{\partial f}{\partial \bf X}

SOLUTION.{\bf S{\scriptsize OLUTION}}.

df=tr(fYdY)=tr(fYd(AXB))=tr(fYA(dX)B)=tr(BfYAdX)=tr((AfYB)dX)\begin{aligned} {\rm d}f &= {\rm tr}({\frac{\partial f}{\partial \bf Y}}^\top {\rm d}{\bf Y}) \\ &= {\rm tr}({\frac{\partial f}{\partial \bf Y}}^\top{\rm d}({\bf AXB})) \\ &= {\rm tr}({\frac{\partial f}{\partial \bf Y}}^\top{\bf A}({\rm d}{\bf X}){\bf B}) \\ &= {\rm tr}({\bf B}{\frac{\partial f}{\partial \bf Y}}^\top{\bf A}{\rm d}{\bf X}) \\ &= {\rm tr}(({ {\bf A}^\top {\frac{\partial f}{\partial \bf Y}} {\bf B}^\top})^\top {\rm d}{\bf X}) \end{aligned}

一些例子

EXAMPLE  1.{\bf E{\scriptsize XAMPLE}\;1}.

aRm,  bRn,  XRn×m,  fR{\bf a} \in {\frak R}^{m},\;{\bf b}\in{\frak R}^{n},\;{\bf X}\in{\frak R}^{n \times m}, \; f \in \frak R,且已知 f=aXbf = {\bf a^\top Xb} ,求fX\dfrac{\partial f}{\partial X}

SOLUTION.{\bf S{\scriptsize OLUTION}}. 易得

df=d(aXb)=a(dX)b{\rm d}f = {\rm d}({\bf a^\top Xb}) = {\bf a^\top}({\rm d}{\bf X}){\bf b}

df=tr(df){\rm d}f={\rm tr}({\rm d}f)

df=tr(a(dX)b)=tr(badX)=tr((ab)dX)\begin{aligned} {\rm d}f &= {\rm tr}({\bf a^\top}({\rm d}{\bf X}){\bf b}) \\ &= {\rm tr}({\bf ba^\top}{\rm d}{\bf X}) \\ &= {\rm tr}(({\bf ab^\top})^\top {\rm d}{\bf X}) \end{aligned}

因此

fX=ab\frac{\partial f}{\partial \bf X}={\bf ab^\top}

NOTE.{\bf N{\scriptsize OTE}}.不能直接(aXb)X=aXXb\dfrac{\partial ({\bf a^\top Xb})}{\partial \bf X}={\bf a^\top}\dfrac{\partial \bf X}{\partial \bf X}{\bf b} 。这是不合法的,且我们并未定义标量对矩阵导数的运算法则。

EXAMPLE  2.{\bf E{\scriptsize XAMPLE}\;2}.

exp(x)=ex,  aRm,  bRn,  XRn×m,fR{\rm exp}(x)={\rm e}^x,\; {\bf a} \in {\frak R}^m,\; {\bf b} \in {\frak R}^n, \; {\bf X} \in {\frak R}^{n \times m}, f \in {\frak R},且已知 f=aexp(Xb)f={\bf a^\top}{\rm exp}({\bf Xb}) ,求fX\dfrac{\partial f}{\partial \bf X} .

SOLUTION.{\bf S{\scriptsize OLUTION}}. 易得

df=d(aexp(Xb))=ad(exp(Xb))=a(exp(Xb)d(Xb))=a(exp(Xb)(dX)b)=tr(a(exp(Xb)(dX)b))=tr((a(exp(Xb))(dX)b))=tr(b(a(exp(Xb))dX))=tr((aexp(Xb))b)dX)\begin{aligned} {\rm d}f &= {\rm d}({\bf a^\top}{\rm exp}({\bf Xb})) = {\bf a^\top}{\rm d}({\rm exp}({\bf Xb})) \\ &= {\bf a^\top}({\rm exp}({\bf Xb}) \odot {\rm d}({\bf Xb})) \\ &= {\bf a^\top}({\rm exp}({\bf Xb}) \odot ({\rm d}{\bf X}){\bf b}) \\ &= {\rm tr}({\bf a^\top}({\rm exp}({\bf Xb}) \odot ({\rm d}{\bf X}){\bf b})) \\ &= {\rm tr}(({\bf a} \odot ({\rm exp}({\bf Xb}))^\top ({\rm d}{\bf X}){\bf b})) \\ &= {\rm tr}({\bf b}({\bf a} \odot ({\rm exp}({\bf Xb}))^\top {\rm d}{\bf X})) \\ &= {\rm tr}(({\bf a} \odot {\rm exp}({\bf Xb})){\bf b}^\top)^\top {\rm d}{\bf X}) \\ \end{aligned}

因此

fX=(aexp(Xb))b\frac{\partial f}{\partial \bf X}=({\bf a} \odot {\rm exp}({\bf Xb})){\bf b}^\top

EXAMPLE  3.{\bf E{\scriptsize XAMPLE}\;3}.

WRl×m,  XRm×n,  YRl×n,{\bf W} \in {\frak R}^{l \times m},\;{\bf X}\in{\frak R}^{m \times n},\;{\bf Y}\in{\frak R}^{l \times n},对称矩阵 MRl×l,  σ:RR,  fR{\bf M} \in {\frak R}^{l \times l},\;\sigma:{\frak R \rightarrow R},\;f \in {\frak R},且已知f=tr(YMY),  Y=σ(WX)f={\rm tr}({ {\bf Y^\top MY}}),\;{\bf Y}=\sigma({\bf WX}) ,求fX\dfrac{\partial f}{\partial \bf X}

SOLUTION.{\bf S{\scriptsize OLUTION}}. 易得

df=d(tr(YMY))=tr(d(YMY))=tr((dY)MY+Yd(MY))=tr((dY)MY+YMdY)=tr((dY)MY)+tr(YMdY)\begin{aligned} {\rm d}f &= {\rm d}({\rm tr}({\bf Y^\top MY})) = {\rm tr}({\rm d}({\bf Y^\top MY})) \\ &= {\rm tr}(({\rm d}{\bf Y^\top}){\bf MY}+{\bf Y^\top}{\rm d}({\bf MY})) \\ &= {\rm tr}(({\rm d}{\bf Y})^\top{\bf MY}+{\bf Y^\top M}{\rm d}{\bf Y}) \\ &= {\rm tr}(({\rm d}{\bf Y})^\top{\bf MY})+{\rm tr}({\bf Y^\top M}{\rm d}{\bf Y}) \\ \end{aligned}

此时考虑迹的转置不变性,有

tr((dY)MY)=tr(YMdY){\rm tr}(({\rm d}{\bf Y})^\top{\bf MY})={\rm tr}({\bf Y^\top M^\top}{\rm d}{\bf Y})

因此

df=tr(Y(M+M)dY)=tr((2MY)dY){\rm d}f={\rm tr}({\bf Y^\top}({\bf M+M^\top}){\rm d}{\bf Y})={\rm tr}(({\bf 2M} {\bf Y})^\top {\rm d}{\bf Y})

Y=σ(WX){\bf Y}=\sigma({\bf WX}) 代入得

df=tr((2Mσ(WX))dσ(WX))=tr((2Mσ(WX))(σ  (WX)(WdX)))=tr((2Mσ(WX)σ  (WX)(WdX)))=tr((W(2Mσ(WX)σ  (WX)))dX)\begin{aligned} {\rm d}f &= {\rm tr}(({\bf 2M}\sigma({\bf WX}))^\top{\rm d}\sigma({\bf WX})) \\ &= {\rm tr}(({\bf 2M}\sigma({\bf WX}))^\top(\sigma^{\;'}({\bf WX}) \odot ({\bf W}{\rm d}{\bf X}))) \\ &= {\rm tr}(({\bf 2M}\sigma({\bf WX}) \odot\sigma^{\;'}({\bf WX})^\top ({\bf W}{\rm d}{\bf X}))) \\ &= {\rm tr}(({\bf W^\top}({\bf 2M}\sigma({\bf WX}) \odot\sigma^{\;'}({\bf WX})))^\top {\rm d}{\bf X}) \\ \end{aligned}

因此

fX=W(2Mσ(WX)σ  (WX))\frac{\partial f}{\partial \bf X} = {\bf W^\top}({\bf 2M}\sigma({\bf WX}) \odot\sigma^{\;'}({\bf WX}))

EXAMPLE  4.    LINEAR  REGRESSION.{\bf E{\scriptsize XAMPLE}\;4}.\;\;{\bf L{\scriptsize INEAR} \;R{\scriptsize EGRESSION}}.

YRm,  wRn,  XRm×n{\bf Y}\in{\frak R}^m,\;{\bf w}\in{\frak R}^n,\;{\bf X}\in{\frak R}^{m \times n},求argminwXwy22{\rm arg}\min\limits_{\bf w}||{\bf Xw-y}||^2_2

SOLUTION.{\bf S{\scriptsize OLUTION}}.l=Xwy22{\bf l}=||{\bf Xw-y}||^2_2 ,此时只需令wl=lw=0\nabla _{\bf w}l=\dfrac{\partial l}{\partial \bf w}={\bf 0} 。又易得

dl=d((Xwy)(Xwy))=(d(Xwy))(Xwy)+(Xwy)d(Xwy)=(dw)X(Xwy)+(Xwy)Xdw=tr((dw)X(Xwy))+tr((Xwy)Xdw)=tr(2(Xwy)Xdw)=tr((2X(Xwy))dw)\begin{aligned} {\rm d}l &= {\rm d}(({\bf Xw-y})^\top ({\bf Xw-y})) \\ &= ({\rm d}({\bf Xw-y})^\top)({\bf Xw-y})+({\bf Xw-y})^\top{\rm d}({\bf Xw-y}) \\ &= ({\rm d}{\bf w})^\top {\bf X}^\top({\bf Xw-y})+({\bf Xw-y})^\top{\bf X}{\rm d}{\bf w} \\ &= {\rm tr}(({\rm d}{\bf w})^\top {\bf X}^\top({\bf Xw-y}))+{\rm tr}(({\bf Xw-y})^\top{\bf X}{\rm d}{\bf w}) \\ &= {\rm tr}(2({\bf Xw-y})^\top{\bf X}{\rm d}{\bf w}) \\ &= {\rm tr}((2{\bf X}^\top({\bf Xw-y}))^\top{\rm d}{\bf w}) \end{aligned}

因此

lw=2X(Xwy)=0\frac{\partial l}{\partial \bf w}=2{\bf X^\top(Xw-y)}={\bf 0}

解得

w=(XX)1Xy{\bf w}=({\bf X^\top X})^{-1}{\bf X^\top y}

EXAMPLE  5.{\bf E{\scriptsize XAMPLE}\; 5}.MAXIMUM  LIKELIHOOD  ESTIMATION  OF  VARIANCE.{\bf M{\scriptsize AXIMUM}\;L{\scriptsize IKELIHOOD}\;E{\scriptsize STIMATION}\;O{\scriptsize F}\;V{\scriptsize ARIANCE}}.

x1,  x2,  xN    N(μ,  Σ){\bf x}_1,\;{\bf x}_2,\;\cdots{\bf x}_N\;\sim\;\mathcal{N}({\boldsymbol \mu},\;{\bf \Sigma}),求Σ{\bf \Sigma} 的最大似然估计,即,对函数

l=lnΣ+1Ni=1N(xixˉ)Σ1(xixˉ)l={\rm ln}|{\bf \Sigma}|+\frac{1}{N}\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})^\top {\bf \Sigma}^{-1} ({\bf x}_i-{\bf \bar x})

Σ{\bf \Sigma}使得lΣ=0\dfrac{\partial l}{\partial {\bf \Sigma}}={\bf 0}

其中,x1,  x2,  ,  xNRm{\bf x}_1,\;{\bf x}_2,\;\cdots,\;{\bf x}_N \in {\frak R}^mxˉ=1Ni=1Nxi{\bf \bar x}=\dfrac{1}{N}\sum\limits_{i=1}^N {\bf x}_iΣRm×m{\bf \Sigma} \in {\frak R}^{m \times m} 且是对称正定矩阵,lRl \in \frak Rln\rm ln 表示自然对数。

SOLUTION.{\bf S{\scriptsize OLUTION}}. 易得

dl=dlnΣ+1Ni=1Nd((xixˉ)Σ1(xixˉ)){\rm d}l={\rm d}{\rm ln}|{\bf \Sigma}|+\frac{1}{N}\sum_{i=1}^{N}{\rm d}(({\bf x}_i-{\bf \bar x})^\top {\bf \Sigma}^{-1} ({\bf x}_i-{\bf \bar x}))

dlnΣ=Σ1dΣ=Σ1Σtr(Σ1dΣ)=tr(Σ1dΣ)\begin{aligned} {\rm dln}|{\bf \Sigma}| &= |{\bf \Sigma}|^{-1}{\rm d}|{\bf \Sigma}| \\ &= |{\bf \Sigma}|^{-1}|{\bf \Sigma}|{\rm tr}({\bf \Sigma}^{-1} {\rm d}{\bf \Sigma}) \\ &= {\rm tr}({\bf \Sigma}^{-1} {\rm d}{\bf \Sigma}) \end{aligned}

1Ni=1Nd((xixˉ)Σ1(xixˉ))=1Ni=1N(xixˉ)(dΣ1)(xixˉ)=1Ni=1N(xixˉ)Σ1(dΣ)Σ1(xixˉ)\begin{aligned} \frac{1}{N}\sum_{i=1}^{N}{\rm d}(({\bf x}_i-{\bf \bar x})^\top {\bf \Sigma}^{-1} ({\bf x}_i-{\bf \bar x})) &= \frac{1}{N}\sum_{i=1}^{N}({\bf x}_i-{\bf \bar x})^\top({\rm d}{\bf \Sigma}^{-1})({\bf x}_i-{\bf \bar x}) \\ &= -\frac{1}{N}\sum_{i=1}^{N}({\bf x}_i-{\bf \bar x})^\top{\bf \Sigma}^{-1}({\rm d}{\bf \Sigma}){\bf \Sigma}^{-1}({\bf x}_i-{\bf \bar x}) \end{aligned}

因此

dl=tr(Σ1dΣ)1Ni=1N(xixˉ)Σ1(dΣ)Σ1(xixˉ)=tr(Σ1dΣ)1Ni=1Ntr((xixˉ)Σ1(dΣ)Σ1(xixˉ))=tr(Σ1dΣ)1Ni=1Ntr(Σ1(xixˉ)(xixˉ)Σ1dΣ)=tr((Σ1Σ1(i=1N(xixˉ)(xixˉ))Σ1)dΣ)\begin{aligned} {\rm d}l &= {\rm tr}({\bf \Sigma}^{-1} {\rm d}{\bf \Sigma})-\frac{1}{N}\sum_{i=1}^{N}({\bf x}_i-{\bf \bar x})^\top{\bf \Sigma}^{-1}({\rm d}{\bf \Sigma}){\bf \Sigma}^{-1}({\bf x}_i-{\bf \bar x}) \\ &= {\rm tr}({\bf \Sigma}^{-1} {\rm d}{\bf \Sigma})-\frac{1}{N}\sum_{i=1}^{N}{\rm tr}(({\bf x}_i-{\bf \bar x})^\top{\bf \Sigma}^{-1}({\rm d}{\bf \Sigma}){\bf \Sigma}^{-1}({\bf x}_i-{\bf \bar x})) \\ &= {\rm tr}({\bf \Sigma}^{-1} {\rm d}{\bf \Sigma})-\frac{1}{N}\sum_{i=1}^{N}{\rm tr}({\bf \Sigma}^{-1}({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top{\bf \Sigma}^{-1}{\rm d}{\bf \Sigma}) \\ &= {\rm tr}(({\bf \Sigma}^{-1}-{\bf \Sigma}^{-1}\left(\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top\right){\bf \Sigma}^{-1}){\rm d}{\bf \Sigma}) \end{aligned}

lΣ=(Σ1Σ1(i=1N(xixˉ)(xixˉ))Σ1)\frac{\partial l}{\partial \bf \Sigma}=({\bf \Sigma}^{-1}-{\bf \Sigma}^{-1}\left(\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top\right){\bf \Sigma}^{-1})^\top

lΣ=0\dfrac{\partial l}{\partial \bf \Sigma}={\bf 0} ,得

Σ1Σ1(i=1N(xixˉ)(xixˉ))Σ1=0Σ1(i=1N(xixˉ)(xixˉ))Σ1=Σ1(i=1N(xixˉ)(xixˉ))Σ1=IΣ=i=1N(xixˉ)(xixˉ)\begin{aligned} {\bf \Sigma}^{-1}-{\bf \Sigma}^{-1}\left(\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top\right){\bf \Sigma}^{-1} &= {\bf 0} \\ {\bf \Sigma}^{-1}\left(\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top\right){\bf \Sigma}^{-1} &= {\bf \Sigma}^{-1} \\ \left(\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top\right){\bf \Sigma}^{-1} &= {\bf I} \\ {\bf \Sigma} &= \sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top \end{aligned}

Σ\bf \Sigma 的最大似然估计为

Σ=i=1N(xixˉ)(xixˉ){\bf \Sigma}=\sum_{i=1}^N ({\bf x}_i-{\bf \bar x})({\bf x}_i-{\bf \bar x})^\top

EXAMPLE  6.{\bf E{\scriptsize XAMPLE}\;6}.MULTINOMIAL  LOGISTIC  REGRESSION.{\bf M{\scriptsize ULTINOMIAL}\;L{\scriptsize OGISTIC}\;R{\scriptsize EGRESSION}}.

定义 Softmax\rm Softmax函数 S:RmR\mathcal{S} : {\frak R}^m \rightarrow {\frak R}S(x)=exp(x)1exp(x)\mathcal{S} ({\bf x})=\dfrac{ {\rm exp}({\bf x})}{ {\bf 1}^\top {\rm exp}({\bf x})} ,其中1{\bf 1} 指全11 向量。

yRm{\bf y} \in {\frak R}^m除一个元素为 11,其他元素均为 00xRn{\bf x} \in {\frak R}^nWRm×n{\bf W} \in {\frak R}^{m \times n}lRl \in {\frak R}。已知l=ylnS(Wx)l=-{\bf y}^\top \ln\mathcal{S}({\bf Wx}) ,求lW\dfrac{\partial l}{\partial \bf W}

SOLUTION.{\bf S{\scriptsize OLUTION}}.先对ll 做化简,易得

l=ylnS(Wx)=ylnexp(Wx)1exp(Wx)=y(lnexp(Wx)1ln(1exp(Wx))=yWx+ln(1exp(Wx))\begin{aligned} l &= -{\bf y}^\top \ln\mathcal{S}({\bf Wx}) \\ &=-{\bf y}^\top\ln\frac{\exp({\bf Wx})}{ {\bf 1}^\top\exp({\bf Wx})} \\ &= -{\bf y}^\top(\ln \exp({\bf Wx})-{\bf 1}\ln ({\bf 1}^\top\exp({\bf Wx})) \\ &= -{\bf y^\top Wx}+\ln({\bf 1}^\top \exp({\bf Wx})) \end{aligned}

NOTE  1.{\bf N{\scriptsize OTE}\;1}.注意 1exp(Wx)R{\bf 1}^\top \exp({\bf Wx}) \in {\frak R},而lnexp(Wx)Rm\ln\exp({\bf Wx}) \in {\frak R}^m ,因此要用1{\bf 1} 补全成向量。

NOTE  2.{\bf N{\scriptsize OTE}\;2}.y1=1{\bf y}^\top{\bf 1}=1

因此

dl=d(yWx+ln(1exp(Wx)))=y(dW)x+dln(1exp(Wx))=y(dW)x+1(exp(Wx)((dW)x))1exp(Wx)=y(dW)x+exp(Wx)((dW)x)1exp(Wx)\begin{aligned} {\rm d}l &= {\rm d}(-{\bf y^\top Wx}+\ln({\bf 1}^\top \exp({\bf Wx}))) \\ &= -{\bf y}^\top({\rm d}{\bf W}){\bf x}\,+\,{\rm d}\ln({\bf 1}^\top\exp({\bf Wx})) \\ &= -{\bf y}^\top({\rm d}{\bf W}){\bf x}\,+\,\frac{ {\bf 1}^\top\left(\exp\left({\bf Wx}\right)\odot\left(\left({\rm d}{\bf W}\right){\bf x}\right)\right)}{ {\bf 1}^\top \exp({\bf Wx})} \\ &= -{\bf y}^\top({\rm d}{\bf W}){\bf x}\,+\,\frac{\exp({\bf Wx})^\top(({\rm d}{\bf W}){\bf x})}{ {\bf 1}^\top \exp({\bf Wx})} \end{aligned}

NOTE.{\bf N{\scriptsize OTE}}.1(uv)=uv{\bf 1^\top}({\bf u \odot v})={\bf u^\top v}

因此

dl=tr(y(dW)x)+tr(exp(Wx)1exp(Wx)(dW)x)=tr(y(dW)x)+tr(S(Wx)(dW)x)=tr(x(S(Wx)y)dW)=tr((S(Wxy)x)dW)\begin{aligned} {\rm d}l &= -{\rm tr}({\bf y}^\top({\rm d}{\bf W}){\bf x}) + {\rm tr}\left(\frac{\exp({\bf Wx})^\top}{ {\bf 1}^\top \exp({\bf Wx})}({\rm d}{\bf W}){\bf x}\right) \\ &= -{\rm tr}({\bf y}^\top({\rm d}{\bf W}){\bf x}) + {\rm tr}\left(\mathcal{S}({\bf Wx})^\top({\rm d}{\bf W}){\bf x}\right) \\ &= {\rm tr}({\bf x}(\mathcal{S}({\bf Wx})^\top-{\bf y^\top}){\rm d}{\bf W}) \\ &= {\rm tr}\left( \left( \mathcal{S}\left({\bf Wx}-{\bf y}\right) {\bf x}^\top \right)^\top {\rm d}{\bf W} \right) \end{aligned}

因此

lW=S(Wxy)x\frac{\partial l}{\partial \bf W}=\mathcal{S}({\bf Wx-y}){\bf x}^\top

EXAMPLE  7.{\bf E{\scriptsize XAMPLE}\;7}.BACKPROPAGATION  OF  THE  2LAYER  NEURAL  NETWORK.{\bf B{\scriptsize ACKPROPAGATION}\;O{\scriptsize F}\;T{\scriptsize HE}\;2{\scriptsize-LAYER}\;N{\scriptsize EURAL}\;N{\scriptsize ETWORK}}.

定义 Sigmoid\rm Sigmoid函数 σ:RR\sigma : {\frak R \rightarrow R}σ(x)=11+exp(x)\sigma(x)=\dfrac{1}{1+{\rm exp}(-x)}

yRm{\bf y} \in {\frak R}^m除一个元素为 11 ,其他元素均为00W2Rm×p{\bf W}_2 \in {\frak R}^{m \times p}W1Rp×n{\bf W}_1 \in {\frak R}^{p \times n}xRn{\bf x} \in {\frak R}^nlRl \in {\frak R}

已知 l=ylnS(W2σ(W1x))l = -{\bf y^\top}\ln\mathcal{S}({ {\bf W}_2\sigma({\bf W}_1{\bf x})}),求 lW1\dfrac{\partial l}{\partial {\bf W}_{1}}lW2\dfrac{\partial l}{\partial {\bf W}_2}

SOLUTION.{\bf S{\scriptsize OLUTION}}.a2=W2σ(W1x){\bf a}_2 = { {\bf W}_2\sigma({\bf W}_1{\bf x})} ,易得

dl=ydlnS(a2)=yd(lnexp(a2)1ln(1exp(a2)))=yda2+dln(1exp(a2))=yda2+1(exp(a2)d(a2))1exp(a2)=yda2+exp(a2)1exp(a2)da2=(S(a2)y)da2\begin{aligned} {\rm d}l &= -{\bf y^\top}{\rm d}\ln\mathcal{S}({\bf a}_2) \\ &= -{\bf y^\top}{\rm d}(\ln\exp({\bf a}_2)-{\bf 1}\ln({\bf 1^\top}\exp({\bf a}_2))) \\ &= -{\bf y^\top}{\rm d}{\bf a}_2 + {\rm d}\ln({\bf 1^\top}\exp({\bf a}_2)) \\ &= -{\bf y^\top}{\rm d}{\bf a}_2 + \frac{ {\bf 1^\top}(\exp({\bf a}_2)\odot{\rm d}({\bf a}_2))}{ {\bf 1^\top}\exp({\bf a}_2)} \\ &= -{\bf y^\top}{\rm d}{\bf a}_2 + \frac{\exp({\bf a}_2)^\top}{ {\bf 1^\top}\exp({\bf a}_2)}{\rm d}{\bf a}_2 \\ &= (\mathcal{S}({\bf a}_2)-{\bf y})^\top{\rm d}{\bf a}_2 \end{aligned}

因此

la2=S(a2)y\frac{\partial l}{\partial {\bf a}_2} = \mathcal{S}({\bf a}_2)-{\bf y}

又,设 a1=W1x{\bf a}_1 = {\bf W}_1 {\bf x}h1=σ(a1){\bf h}_1 = \sigma({\bf a}_1) ,则a2=W2h1{\bf a}_2 = {\bf W}_2{\bf h}_1 。因此

dl=tr(la2W2dh1)+tr(h1la2dW2)=tr((W2la2)dh1)+tr((la2h1)dW2)\begin{aligned} {\rm d}l &= {\rm tr}\left( \frac{\partial l}{\partial {\bf a}_2}^\top {\bf W}_2{\rm d}{\bf h}_1 \right) + {\rm tr}\left({\bf h}_1 \frac{\partial l}{\partial {\bf a}_2}^\top {\rm d}{\bf W}_2 \right) \\ &= {\rm tr}\left( \left({\bf W}_2^\top\frac{\partial l}{\partial {\bf a}_2}\right)^\top {\rm d}{\bf h}_1 \right) + {\rm tr}\left( \left( \frac{\partial l}{\partial {\bf a}_2} {\bf h}_1^\top \right)^\top {\rm d}{\bf W}_2 \right) \\ \end{aligned}

分别记

dl1=tr((W2la2)dh1)dl2=tr((la2h1)dW2)\begin{aligned} {\rm d}l_1 &= {\rm tr}\left( \left({\bf W}_2^\top\frac{\partial l}{\partial {\bf a}_2}\right)^\top {\rm d}{\bf h}_1 \right) \\ {\rm d}l_2 &= {\rm tr}\left( \left( \frac{\partial l}{\partial {\bf a}_2} {\bf h}_1^\top \right)^\top {\rm d}{\bf W}_2 \right) \end{aligned}

由全微分法则,有

lh1=W2la2lW2=la2h1=(S(W2h1)y)σ(W1x)\begin{aligned} \frac{\partial l}{\partial {\bf h}_1} &= {\bf W}_2^\top\frac{\partial l}{\partial {\bf a}_2} \\ \frac{\partial l}{\partial {\bf W}_2} &= \frac{\partial l}{\partial {\bf a}_2} {\bf h}_1^\top = (\mathcal{S}({\bf W}_2{\bf h}_1)-{\bf y})\sigma({\bf W}_1{\bf x})^\top \end{aligned}

又,考虑

dl1=tr(lh1dh1)=tr(lh1dσ(a1))=tr(lh1(σ(a1)da1))=tr((lh1σ(a1))da1)\begin{aligned} {\rm d}l_1 &= {\rm tr}\left( \frac{\partial l}{\partial {\bf h}_1}^\top {\rm d}{\bf h}_1 \right) \\ &= {\rm tr}\left(\frac{\partial l}{\partial {\bf h}_1}^\top {\rm d}\sigma\left( {\bf a}_1 \right) \right) \\ &= {\rm tr}\left( \frac{\partial l}{\partial {\bf h}_1}^\top \left( \sigma^{'}\left( {\bf a}_1 \right) \odot {\rm d}{\bf a}_1 \right) \right) \\ &= {\rm tr}\left( \left( \frac{\partial l}{\partial {\bf h}_1} \odot \sigma^{'}\left( {\bf a}_1 \right) \right)^\top {\rm d}{\bf a}_1 \right) \\ \end{aligned}

可得

la1=lh1σ(a1)\frac{\partial l}{\partial {\bf a}_1} = \frac{\partial l}{\partial {\bf h}_1} \odot \sigma^{'}\left( {\bf a}_1 \right)

继续使用复合函数求导法则,得

dl1=tr(la1da1)=tr(la1(dW1)x)=tr(xla1dW1)=tr((la1x)dW1)\begin{aligned} {\rm d}l_1 &= {\rm tr}\left( \frac{\partial l}{\partial {\bf a}_1}^\top {\rm d}{\bf a}_1 \right) \\ &= {\rm tr}\left( \frac{\partial l}{\partial {\bf a}_1}^\top ({\rm d}{\bf W}_1){\bf x} \right) \\ &= {\rm tr}\left( {\bf x}\frac{\partial l}{\partial {\bf a}_1}^\top {\rm d}{\bf W}_1 \right) \\ &= {\rm tr}\left( (\frac{\partial l}{\partial {\bf a}_1}{\bf x}^\top)^\top {\rm d}{\bf W}_1 \right) \\ \end{aligned}

因此

lW1=la1x=(lh1σ(a1))x=((W2la2)σ(W1x))x=((W2(S(a2)y)σ(W1x))x\begin{aligned} \frac{\partial l}{\partial {\bf W}_1} &= \frac{\partial l}{\partial {\bf a}_1}{\bf x}^\top \\ &= (\frac{\partial l}{\partial {\bf h}_1} \odot \sigma^{'}\left( {\bf a}_1 \right)){\bf x^\top} \\ &= (({\bf W}_2^\top\frac{\partial l}{\partial {\bf a}_2}) \odot \sigma^{'}\left( {\bf W}_1{\bf x} \right)){\bf x^\top} \\ &= (({\bf W}_2^\top(\mathcal{S}({\bf a}_2)-{\bf y}) \odot \sigma^{'}\left( {\bf W}_1{\bf x} \right)){\bf x^\top} \\ \end{aligned}

综上,有

lW1=((W2(S(W2h1)y)σ(W1x))xlW2=(S(W2h1)y)σ(W1x)\begin{aligned} \frac{\partial l}{\partial {\bf W}_1} &= (({\bf W}_2^\top(\mathcal{S}({\bf W}_2{\bf h}_1)-{\bf y}) \odot \sigma^{'}\left( {\bf W}_1{\bf x} \right)){\bf x^\top} \\ \frac{\partial l}{\partial {\bf W}_2} &= (\mathcal{S}({\bf W}_2{\bf h}_1)-{\bf y})\sigma({\bf W}_1{\bf x})^\top \end{aligned}

  • 本文作者: Panelatta
  • 本文链接: https://bofc.tech/9727f3e4/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!