矩阵求导简明方法:元素方法和矩阵方法

矩阵求导方法:元素方法和矩阵方法

本篇运算定义:相同角标代表求和

$I\in\mathbb{R}^{n*n}$

$a\in\mathbb{C}^{1\ast m}$$,b\in\mathbb{C}^{m \ast 1}$

$A\in\mathbb{C}^{m*n}$

$A\in\mathbb{C}^{m*n}$

$A\in\mathbb{C}^{n*n}$

$A\in\mathbb{C}^{m\ast l}$$,B\in\mathbb{C}^{l \ast n}$

$x\in\mathbb{R}^{m \ast 1}$$,y\in\mathbb{R}^{m \ast 1}$

$A\in\mathbb{R}^{m\ast m}$$,B\in\mathbb{R}^{n\ast n }$

$A,B\in\mathbb{R}^{m*n}$

$A\in \mathbb{R}^{m \ast n}$$,B\in \mathbb{R}^{p \ast q}$

$G\in\mathbb{R}^{p \ast n}$$,F\in\mathbb{R}^{q \ast n}$

向量化:

$A\in \mathbb{R}^{m*n}$

$K_{mn}\in \mathbb{R}^{mn*mn}$

去向量化:


一些常用性质:(注意证明思路即可)

$tr(A^TB)=tr((a_{ik})^Tb_{kj})=tr(a_{ki}b_{kj})=a_{ki}b_{ki}$

$(A+B)\oplus(C+D)=A\oplus B+C\oplus D$

$AB\oplus CD=(A\oplus C)(B\oplus D)$

$A\oplus B\oplus C=(A\oplus B)\oplus C=A\oplus (B\oplus C)$

$ (A\oplus B) \ast (C\oplus D)=(A \ast C)\oplus(B \ast D)—其中A,C\in \mathbb{R}^{m \ast m},B,D\in \mathbb{R}^{n \ast n}$

$(A+B) \ast (C+D)=A \ast C+A \ast D+B \ast C+B \ast D$

$tr(A^T(B \ast C))=tr((A^T \ast B^T)C)$

$AB\otimes CD=(A\otimes C)(B\otimes D)——其中A\in \mathbb{R}^{m \ast n},B\in \mathbb{R}^{n \ast p},C\in \mathbb{R}^{s \ast r},D\in \mathbb{R}^{r \ast t}$

$A\otimes (B+C)=A\otimes B+A\otimes C$

$(A+ B)\otimes(C+D)=A\otimes C+A\otimes D+B\otimes C+B\otimes D$

$(A\otimes B)^T=A^T\otimes B^T$

$A\otimes (B\otimes C)=(A\otimes B)\otimes C$

$K_{mn}^T=K_{mn}^{-1}=K_{nm}$

$K_{n1}=K_{1n}=I_n$

$K_{pm}(A\otimes B)K_{nq}=B\otimes A——其中A\in \mathbb{R}^{m\ast n},B\in \mathbb{R}^{p\ast q}$

$tr(A^TB)=vec(A)^Tvec(B)$

$tr(ABC)=vec(A^T)^T(I_p\otimes B)vec(C)$

$tr(ABCD)=vec(D)^T(A\otimes C^T)vec(B^T)$

$vec(A\ast B)=vec(A)\ast vec(B)=diag(vec(A))vec(B)—-diag(a)表示向量以a每个元素为对角元素的对角阵$

$\bigstar向量化操作,乘积矩阵到向量的纽带$

$vec(ABC)=(C^T\otimes A)vec(B)$

$vec(AC)=(I_p\otimes A)vec(C)=(C^T\otimes I_m)vec(A)——其中A\in \mathbb{R}^{m\ast p},B\in \mathbb{R}^{p\ast q},C\in \mathbb{R}^{q\ast n}$


矩阵求导部分
对元素求导方法

与我们学过的求导定义类似,求导是要求出目标量随着一个变量的变化,我们这边把变量看成元素


举个例子

回想我们所知道的f(x)对x求导的定义

很自然想到

而实际上


由上面的结果,我们发现只要我们把定义放在元素对元素上,就与我们之前学过的求导定义一致了!

我们之前所学的关于求导的技术都可以移植过来,比如链式法则等

计算时我们要求


例1、$f(x)=x^TAx$

例2、$X\in \mathbb{R}^{m\ast n},a,b\in \mathbb{R}^{n\ast 1},f(X)=a^TXX^Tb$


接下来的问题是我们能不能定义一套东西,能将这些元素变回矩阵形式

对矩阵定义求导

定义:$X\in \mathbb{R}^{m\ast n},F(X)\in \mathbb{R}^{p\ast q},\bigtriangledown_XF(X)\in \mathbb{R}^{pq\ast mn}$

对上面的例子我们恢复成矩阵形式


例1、$f(x)=x^TAx$


但对一般情况,变回矩阵较为麻烦且没有必要,我们可以通过矩阵的方法直接对矩阵求导

矩阵求导方法

1.取微元 2.向量化

取微元常见性质:

例2、$X\in \mathbb{R}^{m\ast n},a,b\in \mathbb{R}^{n\ast 1},f(X)=a^TXX^Tb$(利用$vec(ABC)=(C^T\otimes A)vec(B)$)

例3、logistic回归

$l=-yx^Tw+log(1+exp(x^Tw)),y=\{0,1\},x,w\in \mathbb{R}^{n*1}$

例4、多元logistic回归

$l=-y^Tlog(softmax(Wx))=-y^TWx+log(1^Texp(Wx)),y\in\mathbb{R}^{m\ast 1},x\in \mathbb{R}^{n\ast 1},W\in \mathbb{R}^{m\ast n}-softmax(a)=\frac{exp(a)}{1^Texp(a)}$