学习总结

(1)矩阵乘法。
Vec ⁡ ( ABC ⁡ ) = ( C ⊤ ⊗ A ) Vec ⁡ ( B ) \operatorname{Vec}(\operatorname{ABC})=\left(\mathbf{C}^{\top} \otimes \mathbf{A}\right) \operatorname{Vec}(\mathbf{B}) Vec(ABC)=(CA)Vec(B)

(2)矩阵转置。 K m n {K}_{m n} Kmn m n mn mn× m n mn mn 的交换矩阵:
Vec ⁡ ( A ⊤ ) = K m n V e c ( A ) \operatorname{Vec}\left(\mathbf{A}^{\top}\right)=\mathbf{K}_{m n} \mathbf{V e c}(\mathbf{A}) Vec(A)=KmnVec(A)
(3)逐元素乘法。其中 diag ⁡ ( V e c ( A ) ) \operatorname{diag}(\mathbf{V e c}(\mathbf{A})) diag(Vec(A)) m n mn mn× m n mn mn 的对角矩阵,元素也是按照A矩阵向量化后的元素排列。
V e c ( A ⊙ X ) = diag ⁡ ( V e c ( A ) ) V e c ( X ) \mathbf{V e c}(\mathbf{A} \odot \mathbf{X})=\operatorname{diag}(\mathbf{V e c}(\mathbf{A})) \mathbf{V e c}(\mathbf{X}) Vec(AX)=diag(Vec(A))Vec(X)

一、几个梯度的栗子

1.1 迹函数相对于矩阵的梯度

迹函数对矩阵求导:
∂ ( t r ( Z Z T ) ) ∂ Z = ∂ ( t r ( Z T Z ) ) ∂ Z = 2 Z \frac{\partial\left(t r\left(\mathbf{Z Z}^{T}\right)\right)}{\partial \mathbf{Z}}=\frac{\partial\left(t r\left(\mathbf{Z}^{T} \mathbf{Z}\right)\right)}{\partial \mathbf{Z}}=2 \mathbf{Z} Z(tr(ZZT))=Z(tr(ZTZ))=2Z
在这里插入图片描述

1.2 行列式相对于矩阵的梯度

矩阵的行列式对矩阵求导:
在这里插入图片描述

二、实值函数相对于实向量的梯度

2.1 实值标量函数对向量的梯度

其实就是标量函数对向量的求导,在之前我们用过定义法求导:
寻找较复杂的实值函数求导更方便的方法,不是每次都先针对任意一个分量,再进行排列。

标量对向量求导的基本法则(PS:和我们以前标量对标量求导的法则类似):

  • 常量对向量的求导结果为0
  • 线性法则:如果 f f f g g g都是实值函数, c 1 c1 c1 c 2 c2 c2为常数,则: ∂ ( c 1 f ( x ) + c 2 g ( x ) ∂ x = c 1 ∂ f ( x ) ∂ x + c 2 ∂ g ( x ) ∂ x \frac{\partial\left(c_{1} f(\mathbf{x})+c_{2} g(\mathbf{x})\right.}{\partial \mathbf{x}}=c_{1} \frac{\partial f(\mathbf{x})}{\partial \mathbf{x}}+c_{2} \frac{\partial g(\mathbf{x})}{\partial \mathbf{x}} x(c1f(x)+c2g(x)=c1xf(x)+c2xg(x)
  • 乘法法则:如果 f f f g g g都是实值函数,则: ∂ f ( x ) g ( x ) ∂ x = f ( x ) ∂ g ( x ) ∂ x + ∂ f ( x ) ∂ x g ( x ) \frac{\partial f(\mathbf{x}) g(\mathbf{x})}{\partial \mathbf{x}}=f(\mathbf{x}) \frac{\partial g(\mathbf{x})}{\partial \mathbf{x}}+\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}} g(\mathbf{x}) xf(x)g(x)=f(x)xg(x)+xf(x)g(x)如果不是实值函数,则不能这样用乘法法则
  • 除法法则:如果 f f f g g g都是实值函数,且 g ( x ) ≠ 0 g(x)≠0 g(x)=0,则: ∂ f ( x ) / g ( x ) ∂ x = 1 g 2 ( x ) ( g ( x ) ∂ f ( x ) ∂ x − f ( x ) ∂ g ( x ) ∂ x ) \frac{\partial f(\mathbf{x}) / g(\mathbf{x})}{\partial \mathbf{x}}=\frac{1}{g^{2}(\mathbf{x})}\left(g(\mathbf{x}) \frac{\partial f(\mathbf{x})}{\partial \mathbf{x}}-f(\mathbf{x}) \frac{\partial g(\mathbf{x})}{\partial \mathbf{x}}\right) xf(x)/g(x)=g2(x)1(g(x)xf(x)f(x)xg(x))

PS:标量对矩阵求导,也有类似上面的法则。

简单来说,标量函数对向量的求导:
∇ x f ( x ) = [ ∂ f ( x ) ∂ x 1 , ∂ f ( x ) ∂ x 2 , ⋯   , ∂ f ( x ) ∂ x n ] T = ∂ f ( x ) ∂ x \nabla_{\mathbf{x}} f(\mathbf{x})=\left[\frac{\partial f(\mathbf{x})}{\partial x_{1}}, \frac{\partial f(\mathbf{x})}{\partial x_{2}}, \cdots, \frac{\partial f(\mathbf{x})}{\partial x_{n}}\right]^{T}=\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}} xf(x)=[x1f(x),x2f(x),,xnf(x)]T=xf(x)

  • 以列向量为自变量的标量函数,其对于自变量的梯度仍然为一阶数相同的列向量
  • 梯度的每个分量代表着函数在该分量方向上的变化率。

2.2 实值向量函数对向量的梯度

即向量对向量求导。

(1)先回顾之前的定义法:
y = A x \mathbf{y} = \mathbf{A} \mathbf{x} y=Ax是向量。

  • A \mathbf{A} A为n×m矩阵
  • x \mathbf{x} x为m维向量; y \mathbf{y} y为n维向量
  • 先分别求【矩阵的第 i i i 行和向量的内积】对向量的第 j j j 分量求导,定义法: ∂ A i x ∂ x j = ∂ A i j x j ∂ x j = A i j \frac{\partial \mathbf{A}_{\mathbf{i}} \mathbf{x}}{\partial \mathbf{x}_{\mathbf{j}}}=\frac{\partial A_{i j} x_{j}}{\partial \mathbf{x}_{\mathbf{j}}}=A_{i j} xjAix=xjAijxj=Aij
  • 所以结果是矩阵 A \mathbf{A} A ( i , j ) (i,j) (i,j)位置的值,排列组成的结果 A \mathbf{A} A,而非 A T \mathbf{A}^{T} AT

(2)回到这里,首先已知:
f ( x ) = [ f 1 ( x ) , f 2 ( x ) , ⋯   , f m ( x ) ] \mathbf{f}(\mathbf{x})=\left[f_{1}(\mathbf{x}), f_{2}(\mathbf{x}), \cdots, f_{m}(\mathbf{x})\right] f(x)=[f1(x),f2(x),,fm(x)]
实值向量函数对于实向量的梯度为:
∂ f ( x ) ∂ x = [ ∂ f 1 ( x ) ∂ x , ∂ f 2 ( x ) ∂ x , ⋯   , ∂ f m ( x ) ∂ x ] = [ ∂ f 1 ( x ) ∂ x 1 ∂ f 2 ( x ) ∂ x 1 … ∂ f m ( x ) ∂ x 1 ∂ f 1 ( x ) ∂ x 2 ∂ f 2 ( x ) ∂ x 2 … ∂ f m ( x ) ∂ x 2 ⋮ ⋮ ⋱ ⋮ ∂ f 1 ( x ) ∂ x n ∂ f 2 ( x ) ∂ x n … ∂ f m ( x ) ∂ x n ] = ∇ x f ( x ) \frac{\partial \mathbf{f}(\mathbf{x})}{\partial \mathbf{x}}=\left[\frac{\partial f_{1}(\mathbf{x})}{\partial \mathbf{x}}, \frac{\partial f_{2}(\mathbf{x})}{\partial \mathbf{x}}, \cdots, \frac{\partial f_{m}(\mathbf{x})}{\partial \mathbf{x}}\right]=\left[\begin{array}{cccc} \frac{\partial f_{1}(\mathbf{x})}{\partial x_{1}} & \frac{\partial f_{2}(\mathbf{x})}{\partial x_{1}} & \ldots & \frac{\partial f_{m}(\mathbf{x})}{\partial x_{1}} \\ \frac{\partial f_{1}(\mathbf{x})}{\partial x_{2}} & \frac{\partial f_{2}(\mathbf{x})}{\partial x_{2}} & \ldots & \frac{\partial f_{m}(\mathbf{x})}{\partial x_{2}} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial f_{1}(\mathbf{x})}{\partial x_{n}} & \frac{\partial f_{2}(\mathbf{x})}{\partial x_{n}} & \ldots & \frac{\partial f_{m}(\mathbf{x})}{\partial x_{n}} \end{array}\right]=\nabla_{\mathbf{x}} \mathbf{f}(\mathbf{x}) xf(x)=[xf1(x),xf2(x),,xfm(x)]=x1f1(x)x2f1(x)xnf1(x)x1f2(x)x2f2(x)xnf2(x)x1fm(x)x2fm(x)xnfm(x)=xf(x)

  • 向量函数对于向量的求导,相当于向量函数中的每一个分量函数对向量求导。
  • 行向量函数对列向量自变量求导形成矩阵;
  • 列向量函数对行向量自变量求导也可以形成矩阵。

2.3 简单练习

在这里插入图片描述

三、矩阵向量化vec

3.1 向量化定义

向量化算子vec很常用,设 A = [ a i j ] m × n \mathbf{A}=\left[\mathbf{a}_{\mathbf{i j}}\right]_{\mathbf{m} \times \mathbf{n}} A=[aij]m×n,则按照每列拼接起来组成的向量:
V ec ⁡ ( A ) = ( a 11 a 21 ⋯ a m 1 ; a 12 a 22 ⋯ a m 2 ; ⋯   ; a 1 n a 2 n ⋯ a m n ) ⊤ \mathbf{V} \operatorname{ec}(\mathbf{A})=\left(\mathbf{a}_{11} \mathbf{a}_{21} \cdots \mathbf{a}_{\mathrm{m} 1} ; \mathbf{a}_{12} \mathbf{a}_{22} \cdots \mathbf{a}_{\mathrm{m} 2} ; \cdots ; \mathbf{a}_{1 \mathrm{n}} \mathbf{a}_{2 \mathrm{n}} \cdots \mathbf{a}_{\mathrm{mn}}\right)^{\top} Vec(A)=(a11a21am1;a12a22am2;;a1na2namn)

3.2 向量化算子性质

(1)线性性质。很好理解,这里vec是线性算子 Vec ⁡ ( k 1 A + k 2 B ) = k 1 Vec ⁡ ( A ) + k 2 Vec ⁡ B \operatorname{Vec}\left(\mathbf{k}_{1} \mathbf{A}+\mathbf{k}_{2} \mathbf{B}\right)=\mathbf{k}_{1} \operatorname{Vec}(\mathbf{A})+\mathbf{k}_{2} \operatorname{Vec} \mathbf{B} Vec(k1A+k2B)=k1Vec(A)+k2VecB
(2)矩阵乘法。后2个是特例,故记住第一个即可
Vec ⁡ ( ABC ⁡ ) = ( C ⊤ ⊗ A ) Vec ⁡ ( B ) \operatorname{Vec}(\operatorname{ABC})=\left(\mathbf{C}^{\top} \otimes \mathbf{A}\right) \operatorname{Vec}(\mathbf{B}) Vec(ABC)=(CA)Vec(B) V e c ( A X ) = ( I ⊗ A ) V e c ( X ) \mathbf{V e c}(\mathbf{A X})=(\mathbf{I} \otimes \mathbf{A}) \mathbf{V} \mathbf{e c}(\mathbf{X}) Vec(AX)=(IA)Vec(X) V e c ( X C ) = ( C ⊤ ⊗ I ) V e c ( X ) \mathbf{V e c}(\mathbf{X} \mathbf{C})=\left(\mathbf{C}^{\top} \otimes \mathbf{I}\right) \mathbf{V e c}(\mathbf{X}) Vec(XC)=(CI)Vec(X)

(3)矩阵转置。 K m n {K}_{m n} Kmn m n mn mn× m n mn mn 的交换矩阵:
Vec ⁡ ( A ⊤ ) = K m n V e c ( A ) \operatorname{Vec}\left(\mathbf{A}^{\top}\right)=\mathbf{K}_{m n} \mathbf{V e c}(\mathbf{A}) Vec(A)=KmnVec(A)

(4)逐元素乘法。其中 diag ⁡ ( V e c ( A ) ) \operatorname{diag}(\mathbf{V e c}(\mathbf{A})) diag(Vec(A)) m n mn mn× m n mn mn 的对角矩阵,元素也是按照A矩阵向量化后的元素排列。
V e c ( A ⊙ X ) = diag ⁡ ( V e c ( A ) ) V e c ( X ) \mathbf{V e c}(\mathbf{A} \odot \mathbf{X})=\operatorname{diag}(\mathbf{V e c}(\mathbf{A})) \mathbf{V e c}(\mathbf{X}) Vec(AX)=diag(Vec(A))Vec(X)

四、Python实现Kronecker积等

可以参考numpy的官方文档。

from numpy import dot,cross,kron
# cross ref:https://docs.scipy.org/doc/numpy/reference/generated/numpy.cross.html#numpy.cross
# dot,kron ref:https://docs.scipy.org/doc/numpy/reference/routines.linalg.html
from scipy.linalg import hadamard
# hadamard ref:https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.hadamard.html#scipy.linalg.hadamard

这里举个求Kronecker积和向量的外积的栗子:

import numpy as np
a = np.array([[1], [2]])
b = np.array([[3], [4]])
kron1 = np.kron(a, b)
outer = np.outer(a, b)
kron2 = np.kron(a, b.T)

结果如下,可以发现Kronecker积结果是,a的1乘b向量,a的2乘b向量,然后两个向量拼接起来。并且如果a向量和b向量的转置进行Kronecker积,其结果和a和b做向量外积outer结果相同。

复习:K=kron(A,B),获得 A 和 B 的 Kronecker 张量积。如果 A 是 m×n 矩阵,而 B 是 p×q 矩阵,则 kron(A,B) 是通过获取 A 元素与矩阵 B 元素之间的所有可能积而形成的一个 mp×nq 矩阵
在这里插入图片描述【外积】即两个向量的向量积,即两个向量的组成的平面的法向量。
符号表示:a× b
向量积的大小:|a|·|b|·sin<a,b>.
栗子:(x1,y1,z1)×(x2,y2,z2)=(y1z2-y2z1,z1x2-z2x1,x1y2-x2y1)
在这里插入图片描述

Reference

(1)https://numpy.org/doc/stable/reference/generated/numpy.kron.html#numpy.kron
(2)numpy中dot()、outer()、multiply()以及matmul()的区别

Logo

一站式 AI 云服务平台

更多推荐