矩阵求导的本质与分子布局、分母布局的本质(一)

这篇文章,通过查阅大量资料,总结出矩阵求导的本质与分子布局、分母布局,希望对初学的同学、想理解本质的同学提供一些帮助。

在开始学习之前,我们引入《动手学深度学习》里面的一个实例:

对于前两个求导式,第一印象是不是感觉写反了?是不是应该为:
∇ x Ax = A \nabla_x\textbf{Ax}=\textbf{A} xAx=A
∇ x x T A = A T \nabla_x\textbf{x}^T\textbf{A}=\textbf{A}^T xxTA=AT
相信看完这篇文章的小伙伴就能明白为什么?

首先需要说明的是,理解本文不需要有多么高深的数学理论,只需要了解本科阶段高等数学的偏导如何求、线性代数的矩阵的定义,请大家放心食用。

文章中若没有说明,则约定向量均为列向量,如我们定义 x = [ x 1 , x 2 , x 3 ] T \textbf{x}=[x_1,x_2,x_3]^T x=[x1,x2,x3]T

一、基础知识引入(函数、标量、向量和矩阵)

首先考虑函数:
f u n c t i o n ( i n p u t ) function(input) function(input)
针对函数和输入的类型,我们可以将这个函数分为不同的类别。

1.1函数是一个标量

这时我们称函数是一个实值标量函数,用细体小写字母 f f f表示。

1.1.1输入是一个标量

我们称函数的变元是标量,用细体小写字母 x x x表示。

例如我们最常见的函数的定义形式:
f ( x ) = θ 0 + θ 1 x f(x)=\theta_0+\theta_1x f(x)=θ0+θ1x

1.1.2输入是一个向量

我们称函数的变元是向量,用粗体小写字母 x \textbf{x} x表示。

如下所示:

x = [ x 1 , x 2 , x 3 ] T \textbf{x}=[x_1,x_2,x_3]^T x=[x1,x2,x3]T,则有:
f ( x ) = a 1 x 1 2 + a 2 x 2 2 + a 3 x 3 2 + a 4 x 2 x 2 f(\textbf{x})=a_1x_1^2+a_2x_2^2+a_3x_3^2+a_4x_2x_2 f(x)=a1x12+a2x22+a3x32+a4x2x2

1.1.3输入是一个矩阵

我们称函数的变元是矩阵。用粗体大写字母 X \textbf{X} X表示。

如下所示:

X 3 × 2 = ( x i j ) i = 1 , j = 1 3 , 2 \textbf{X}_{3\times 2}=(x_{ij})_{i=1,j=1}^{3,2} X3×2=(xij)i=1,j=13,2,则有:
f ( X ) = a 1 x 11 2 + a 2 x 12 2 + a 3 x 21 2 + a 4 x 22 2 + a 5 x 31 2 + a 6 x 32 2 f(\textbf{X})=a_1x_{11}^2+a_2x_{12}^2+a_3x_{21}^2+a_4x_{22}^2+a_5x_{31}^2+a_6x_{32}^2 f(X)=a1x112+a2x122+a3x212+a4x222+a5x312+a6x322

1.2函数是一个向量

我们称函数是一个实向量函数,用粗体小写字母 f \textbf{f} f表示。

含义: f \textbf{f} f是由若干个 f f f组成的一个向量。

同样的,变元分三种:标量、向量、矩阵。符号的含义与上文一致。

1.2.1输入是一个标量

例如:
f 3 × 1 ( x ) = [ f 1 ( x ) f 2 ( x ) f 3 ( x ) ] = [ x + 1 2 x + 1 3 x + 1 ] \textbf{f}_{3\times 1}(x)=\left[ \begin{matrix} f_1(x)\\ f_2(x)\\ f_3(x) \end{matrix} \right]=\left[ \begin{matrix} x+1\\ 2x+1\\ 3x+1 \end{matrix} \right] f3×1(x)= f1(x)f2(x)f3(x) = x+12x+13x+1

1.2.2输入是一个向量

例如:

x = [ x 1 , x 2 , x 3 ] T \textbf{x}=[x_1,x_2,x_3]^T x=[x1,x2,x3]T
f 3 × 1 ( x ) = [ f 1 ( x ) f 2 ( x ) f 3 ( x ) ] = [ x 1 + x 2 + x 3 x 1 2 + 2 x 2 + 2 x 3 x 1 x 2 + x 2 + x 3 ] \textbf{f}_{3\times 1}(x)=\left[ \begin{matrix} f_1(\textbf{x})\\ f_2(\textbf{x})\\ f_3(\textbf{x}) \end{matrix} \right]=\left[ \begin{matrix} x_1+x_2+x_3\\ x_1^2+2x_2+2x_3\\ x_1x_2+x_2+x_3 \end{matrix} \right] f3×1(x)= f1(x)f2(x)f3(x) = x1+x2+x3x12+2x2+2x3x1x2+x2+x3

1.2.3输入是一个矩阵

X 3 × 2 = ( x i j ) i = 1 , j = 1 3 , 2 \textbf{X}_{3\times 2}=(x_{ij})_{i=1,j=1}^{3,2} X3×2=(xij)i=1,j=13,2
f 3 × 1 ( X ) = [ f 1 ( X ) f 2 ( X ) f 3 ( X ) ] = [ x 11 + x 12 + x 21 + x 22 + x 31 + x 32 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 + x 11 x 12 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 + x 11 x 12 ] \textbf{f}_{3\times 1}(\textbf{X})=\left[ \begin{matrix} f_1(\textbf{X})\\ f_2(\textbf{X})\\ f_3(\textbf{X}) \end{matrix} \right]=\left[ \begin{matrix} x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32}\\ x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32}+x_{11}x_{12}\\ x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32}+x_{11}x_{12} \end{matrix} \right] f3×1(X)= f1(X)f2(X)f3(X) = x11+x12+x21+x22+x31+x32x11+x12+x21+x22+x31+x32+x11x12x11+x12+x21+x22+x31+x32+x11x12

1.3函数是一个矩阵

此时我们称函数是一个实矩阵函数,用粗体大写字母 F \textbf{F} F表示。

含义: F \textbf{F} F是由若干个 f f f组成的一个矩阵。

同样的,输入继续分三种。

1.3.1输入是一个标量

例如:
F 3 × 2 ( x ) = [ f 11 ( x ) f 12 ( x ) f 21 ( x ) f 22 ( x ) f 31 ( x ) f 32 ( x ) ] = [ x + 1 x + 2 x + 3 x + 4 x + 6 x + 7 ] \textbf{F}_{3\times 2}(x)=\left[ \begin{matrix} f_{11}(x) & f_{12}(x)\\ f_{21}(x) & f_{22}(x)\\ f_{31}(x) & f_{32}(x) \end{matrix} \right]=\left[ \begin{matrix} x+1 & x+2\\ x+3 & x+4\\ x+6 & x+7 \end{matrix} \right] F3×2(x)= f11(x)f21(x)f31(x)f12(x)f22(x)f32(x) = x+1x+3x+6x+2x+4x+7

1.3.2输入是一个向量

例:设 x = [ x 1 , x 2 , x 3 ] T \textbf{x}=[x_1,x_2,x_3]^T x=[x1,x2,x3]T
F 3 × 2 ( x ) = [ f 11 ( x f 12 ( x f 21 ( x f 22 ( x f 31 ( x f 32 ( x ] = [ x 1 + x 2 + x 3 2 x 1 + 2 x 2 + x 3 2 x 1 + 2 x 2 + x 3 x 1 + 2 x 2 + x 3 x 1 + 2 x 2 + x 3 x 1 + 2 x 2 + 2 x 3 ] \textbf{F}_{3\times 2}(x)=\left[ \begin{matrix} f_{11}(\textbf{x} & f_{12}(\textbf{x}\\ f_{21}(\textbf{x} & f_{22}(\textbf{x}\\ f_{31}(\textbf{x} & f_{32}(\textbf{x} \end{matrix} \right]=\left[ \begin{matrix} x_1+x_2+x_3 & 2x_1+2x_2+x_3\\ 2x_1+2x_2+x_3 & x_1+2x_2+x_3\\ x_1+2x_2+x_3 & x_1+2x_2+2x_3 \end{matrix} \right] F3×2(x)= f11(xf21(xf31(xf12(xf22(xf32(x = x1+x2+x32x1+2x2+x3x1+2x2+x32x1+2x2+x3x1+2x2+x3x1+2x2+2x3

1.3.3输入是一个矩阵

X 3 × 2 = ( x i j ) i = 1 , j = 1 3 , 2 \textbf{X}_{3\times 2}=(x_{ij})_{i=1,j=1}^{3,2} X3×2=(xij)i=1,j=13,2
F 3 × 2 ( X ) = [ f 11 ( X ) f 12 ( X ) f 21 ( X ) f 22 ( X ) f 31 ( X ) f 32 ( X ) ] = [ x 11 + x 12 + x 21 + x 22 + x 31 + x 32 2 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 3 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 4 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 5 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 6 x 11 + x 12 + x 21 + x 22 + x 31 + x 32 ] \textbf{F}_{3\times 2}(\textbf{X})=\left[ \begin{matrix} f_{11}(\textbf{X}) & f_{12}(\textbf{X})\\ f_{21}(\textbf{X}) & f_{22}(\textbf{X})\\ f_{31}(\textbf{X}) & f_{32}(\textbf{X}) \end{matrix} \right]=\left[ \begin{matrix} x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} & 2x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} \\ 3x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} & 4x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} \\ 5x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} & 6x_{11}+x_{12}+x_{21}+x_{22}+x_{31}+x_{32} \end{matrix} \right] F3×2(X)= f11(X)f21(X)f31(X)f12(X)f22(X)f32(X) = x11+x12+x21+x22+x31+x323x11+x12+x21+x22+x31+x325x11+x12+x21+x22+x31+x322x11+x12+x21+x22+x31+x324x11+x12+x21+x22+x31+x326x11+x12+x21+x22+x31+x32

二、矩阵求导的本质

在本科阶段的数学学习中,我们知道如何对一个多元函数进行求偏导。

例如,给定一个多元函数:
f ( x 1 , x 2 , x 3 ) = x 1 2 + 2 x 1 x 2 + 3 x 2 x 3 f(x_1,x_2,x_3)=x_1^2+2x_1x_2+3x_2x_3 f(x1,x2,x3)=x12+2x1x2+3x2x3
我们咳哟将 f f f x 1 , x 2 , x 3 x_1,x_2,x_3 x1,x2,x3的偏导分别求出来,即:
∂ f ∂ x 1 = 2 x 1 + 2 x 2 \frac{\partial f}{\partial x_1} =2x_1+2x_2 x1f=2x1+2x2
∂ f ∂ x 2 = 2 x 1 + 3 x 3 \frac{\partial f}{\partial x_2} =2x_1+3x_3 x2f=2x1+3x3
∂ f ∂ x 3 = 3 x 2 \frac{\partial f}{\partial x_3} =3x_2 x3f=3x2
矩阵求导也是一样的,本质就是函数中的每个 f \textbf{f} f分别对变元中的每个元素逐个求偏导,只不过写成了向量、矩阵形式而已。

我们把得出的三个结果写成列向量的形式:
∂ f ( x ) ∂ x 3 × 1 = [ ∂ f ∂ x 1 ∂ f ∂ x 2 ∂ f ∂ x 3 ] = [ 2 x 1 + 2 x 2 2 x 1 + 3 x 3 3 x 2 ] \frac{\partial f(\textbf{x})}{\partial \textbf{x}_{3\times 1}}=\left[ \begin{matrix} \frac{\partial f}{\partial x_1}\\ \frac{\partial f}{\partial x_2}\\ \frac{\partial f}{\partial x_3} \end{matrix} \right]=\left[ \begin{matrix} 2x_1+2x_2\\ 2x_1+3x_3\\ 3x_2 \end{matrix} \right] x3×1f(x)= x1fx2fx3f = 2x1+2x22x1+3x33x2
这便是一个简单的矩阵求导,以列向量的形式展开。

当然也可以使用行向量的形式展开:
∂ f ( x ) ∂ x 3 × 1 T = [ ∂ f ∂ x 1 , ∂ f ∂ x 2 , ∂ f ∂ x 3 ] = [ 2 x 1 + 2 x 2 , 2 x 1 + 3 x 3 , 3 x 2 ] \frac{\partial f(\textbf{x})}{\partial \textbf{x}_{3\times 1}^T}=\left[ \begin{matrix} \frac{\partial f}{\partial x_1},\frac{\partial f}{\partial x_2},\frac{\partial f}{\partial x_3} \end{matrix} \right]=\left[ \begin{matrix} 2x_1+2x_2,2x_1+3x_3,3x_2 \end{matrix} \right] x3×1Tf(x)=[x1f,x2f,x3f]=[2x1+2x2,2x1+3x3,3x2]
所以,如果 f u n c t i o n function function中有 m m m f f f,变元中有 n n n个元素,那么,每个 f f f对变元中的每个元素逐个求偏导后,我们就会产生 m × n m\times n m×n个结果。

以上便是矩阵求导的本质!

这里就引入了一个重要的问题,这个 m × n m\times n m×n个结果的布局,究竟是写成行向量的形式,还是写成列向量的形式?

三、矩阵求导结果的布局

从直观上来讲:

分子布局,就是分子是列向量形式,分母是行向量的形式。如果这里的 f u c t i o n fuction fuction是实向量函数 f 2 × 1 \textbf{f}_{2\times 1} f2×1的话,结果就是 2 × 3 2\times 3 2×3的矩阵了,结果可以展示为:
∂ f 2 × 1 ( x ) ∂ x 3 × 1 T = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ∂ f 1 ∂ x 3 ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ∂ f 2 ∂ x 3 ] 2 × 3 \frac{\partial \textbf{f}_{2\times 1}(\textbf{x})}{\partial \textbf{x}_{3\times 1}^T}=\left[ \begin{matrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \frac{\partial f_1}{\partial x_3}\\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \frac{\partial f_2}{\partial x_3} \end{matrix} \right]_{2\times 3} x3×1Tf2×1(x)=[x1f1x1f2x2f1x2f2x3f1x3f2]2×3
分母布局,就是分母是列向量形式,分子是行向量形式,如果这里的 f u n c t i o n function function是实向量函数 f 2 × 1 \textbf{f}_{2\times 1} f2×1的话,结果就是 3 × 2 3\times 2 3×2的矩阵了:
∂ f 2 × 1 T ( x ) ∂ x 3 × 1 = [ ∂ f 1 ∂ x 1 ∂ f 2 ∂ x 1 ∂ f 1 ∂ x 2 ∂ f 2 ∂ x 2 ∂ f 1 ∂ x 3 ∂ f 2 ∂ x 3 ] 3 × 2 \frac{\partial \textbf{f}_{2\times 1}^T(\textbf{x})}{\partial \textbf{x}_{3\times 1}}=\left[ \begin{matrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_2}{\partial x_1}\\ \frac{\partial f_1}{\partial x_2} & \frac{\partial f_2}{\partial x_2}\\ \frac{\partial f_1}{\partial x_3} & \frac{\partial f_2}{\partial x_3} \end{matrix} \right]_{3\times 2} x3×1f2×1T(x)= x1f1x2f1x3f1x1f2x2f2x3f2 3×2
直观上理解了之后,我们针对不同类型的 f u n c t i o n function function,不同类型的变元,给出严谨的布局说明。

3.1向量变元的实值标量函数

这里,表示为 f ( x ) f(\textbf{x}) f(x),且 x = [ x 1 , x 2 , ⋯   , x n ] T \textbf{x}=[x_1,x_2,\cdots,x_n]^T x=[x1,x2,,xn]T

3.1.1行向量偏导形式

D x f ( x ) = ∂ f ( x ) ∂ x T = [ ∂ f ∂ x 1 , ∂ f ∂ x 2 , ⋯   , ∂ f ∂ x n ] D_{\textbf{x}}f(\textbf{x})=\frac{\partial f(\textbf{x})}{\partial \textbf{x}^T}=\left[ \begin{matrix} \frac{\partial f}{\partial x_1},\frac{\partial f}{\partial x_2},\cdots,\frac{\partial f}{\partial x_n} \end{matrix} \right] Dxf(x)=xTf(x)=[x1f,x2f,,xnf]

3.1.2梯度向量形式

又称列向量偏导形式、列偏导向量形式。
∇ x f ( x ) = ∂ f ( x ) ∂ x = [ ∂ f ∂ x 1 , ∂ f ∂ x 2 , ⋯   , ∂ f ∂ x n ] T \nabla_{\textbf{x}}f(\textbf{x})=\frac{\partial f(\textbf{x})}{\partial \textbf{x}}=\left[ \begin{matrix} \frac{\partial f}{\partial x_1},\frac{\partial f}{\partial x_2},\cdots,\frac{\partial f}{\partial x_n} \end{matrix} \right]^T xf(x)=xf(x)=[x1f,x2f,,xnf]T
这两种形式互为转置。

3.2矩阵变元的实值标量函数

定义为:
f ( X ) f(\textbf{X}) f(X)
其中,
X m × n = ( x i j ) i , j = 1 m , n \textbf{X}_{m\times n}=(x_{ij})_{i,j=1}^{m,n} Xm×n=(xij)i,j=1m,n
介绍一个符号 v e c ( X ) vec(\textbf{X}) vec(X),作用是将矩阵 X \textbf{X} X按列堆栈来向量化。

解释一下, v e c ( X ) vec(\textbf{X}) vec(X)就是把矩阵 X \textbf{X} X的第1列、第2列,知道第 n n n列取出来,然后按顺序组成一个列向量,即:
v e c ( X ) = [ x 11 , x 21 , ⋯   , x m 1 , x 12 , x 22 , ⋯   , x m 2 , ⋯   , x 1 n , x 2 n , ⋯   , x m n ] T vec(\textbf{X})=[x_{11},x_{21},\cdots,x_{m1},x_{12},x_{22},\cdots,x_{m2},\cdots,x_{1n},x_{2n},\cdots,x_{mn}]^T vec(X)=[x11,x21,,xm1,x12,x22,,xm2,,x1n,x2n,,xmn]T

3.2.1行向量偏导形式

即先把矩阵变元 X \textbf{X} X v e c vec vec向量化,转换成向量变元,再对该向量变元:
D v e c X f ( X ) = ∂ f ( X ) ∂ v e c T ( X ) = [ ∂ f ∂ x 11 , ∂ f ∂ x 21 , ⋯   , ∂ f ∂ x m 1 , ∂ f ∂ x 12 , ∂ f ∂ x 22 , ⋯   , ∂ f ∂ x m 2 , ⋯   , ∂ f ∂ x 1 n , ∂ f ∂ x 2 n , ⋯   , ∂ f ∂ x m n ] D_{vec\textbf{X}}f(\textbf{X})=\frac{\partial f(\textbf{X})}{\partial vec^T(\textbf{X})}=\left[ \begin{matrix} \frac{\partial f}{\partial x_{11}},\frac{\partial f}{\partial x_{21}},\cdots,\frac{\partial f}{\partial x_{m1}},\frac{\partial f}{\partial x_{12}},\frac{\partial f}{\partial x_{22}},\cdots,\frac{\partial f}{\partial x_{m2}},\cdots,\frac{\partial f}{\partial x_{1n}},\frac{\partial f}{\partial x_{2n}},\cdots,\frac{\partial f}{\partial x_{mn}} \end{matrix} \right] DvecXf(X)=vecT(X)f(X)=[x11f,x21f,,xm1f,x12f,x22f,,xm2f,,x1nf,x2nf,,xmnf]

3.2.2Jacobian矩阵形式

即先把矩阵变元 X \textbf{X} X进行转置,再对转置后的每个位置的元素逐个求偏导,结果布局和转置布局一样。
D X f ( X ) = ∂ f ( X ) ∂ X m × n T = [ ∂ f ∂ x 11 ∂ f ∂ x 21 ⋯ ∂ f ∂ x m 1 ∂ f ∂ x 12 ∂ f ∂ x 22 ⋯ ∂ f ∂ x m 2 ⋮ ⋮ ⋮ ⋮ ∂ f ∂ x 1 n ∂ f ∂ x 2 n ⋯ ∂ f ∂ x m n ] n × m D_{\textbf{X}}f(\textbf{X})=\frac{\partial f(\textbf{X})}{\partial \textbf{X}_{m\times n}^T}=\left[ \begin{matrix} \frac{\partial f}{\partial x_{11}} & \frac{\partial f}{\partial x_{21}} & \cdots & \frac{\partial f}{\partial x_{m1}}\\ \frac{\partial f}{\partial x_{12}} & \frac{\partial f}{\partial x_{22}} & \cdots & \frac{\partial f}{\partial x_{m2}}\\ \vdots & \vdots & \vdots & \vdots\\ \frac{\partial f}{\partial x_{1n}} & \frac{\partial f}{\partial x_{2n}} & \cdots & \frac{\partial f}{\partial x_{mn}} \end{matrix} \right]_{n\times m} DXf(X)=Xm×nTf(X)= x11fx12fx1nfx21fx22fx2nfxm1fxm2fxmnf n×m

3.2.3梯度向量形式

即先把矩阵变 X \textbf{X} X v e c vec vec向量化,转换成向量变元,再对该变元进行计算:
D v e c X f ( X ) = ∂ f ( X ) ∂ v e c ( X ) = [ ∂ f ∂ x 11 , ∂ f ∂ x 21 , ⋯   , ∂ f ∂ x m 1 , ∂ f ∂ x 12 , ∂ f ∂ x 22 , ⋯   , ∂ f ∂ x m 2 , ⋯   , ∂ f ∂ x 1 n , ∂ f ∂ x 2 n , ⋯   , ∂ f ∂ x m n ] T D_{vec\textbf{X}}f(\textbf{X})=\frac{\partial f(\textbf{X})}{\partial vec(\textbf{X})}=\left[ \begin{matrix} \frac{\partial f}{\partial x_{11}},\frac{\partial f}{\partial x_{21}},\cdots,\frac{\partial f}{\partial x_{m1}},\frac{\partial f}{\partial x_{12}},\frac{\partial f}{\partial x_{22}},\cdots,\frac{\partial f}{\partial x_{m2}},\cdots,\frac{\partial f}{\partial x_{1n}},\frac{\partial f}{\partial x_{2n}},\cdots,\frac{\partial f}{\partial x_{mn}} \end{matrix} \right]^T DvecXf(X)=vec(X)f(X)=[x11fx21f,,xm1f,x12f,x22f,,xm2f,,x1nf,x2nf,,xmnf]T

3.2.4梯度矩阵形式

直接对原矩阵变元 X \textbf{X} X的每个位置的元素逐个求偏导,结果布局和原矩阵布局一样。
D X f ( X ) = ∂ f ( X ) ∂ X m × n T = [ ∂ f ∂ x 11 ∂ f ∂ x 12 ⋯ ∂ f ∂ x 1 n ∂ f ∂ x 21 ∂ f ∂ x 22 ⋯ ∂ f ∂ x 2 n ⋮ ⋮ ⋮ ⋮ ∂ f ∂ x m 1 ∂ f ∂ x m 2 ⋯ ∂ f ∂ x m n ] m × n D_{\textbf{X}}f(\textbf{X})=\frac{\partial f(\textbf{X})}{\partial \textbf{X}_{m\times n}^T}=\left[ \begin{matrix} \frac{\partial f}{\partial x_{11}} & \frac{\partial f}{\partial x_{12}} & \cdots & \frac{\partial f}{\partial x_{1n}}\\ \frac{\partial f}{\partial x_{21}} & \frac{\partial f}{\partial x_{22}} & \cdots & \frac{\partial f}{\partial x_{2n}}\\ \vdots & \vdots & \vdots & \vdots\\ \frac{\partial f}{\partial x_{m1}} & \frac{\partial f}{\partial x_{m2}} & \cdots & \frac{\partial f}{\partial x_{mn}} \end{matrix} \right]_{m\times n} DXf(X)=Xm×nTf(X)= x11fx21fxm1fx12fx22fxm2fx1nfx2nfxmnf m×n

3.3矩阵变元的实矩阵函数 F ( X ) \textbf{F}(\textbf{X}) F(X)

其中, X m × n = ( x i j ) i , j = 1 m , n \textbf{X}_{m\times n}=(x_{ij})_{i,j=1}^{m,n} Xm×n=(xij)i,j=1m,n
F p × q = ( f i j ) i , j = 1 p , q \textbf{F}_{p\times q}=(\textbf{f}_{ij})_{i,j=1}^{p,q} Fp×q=(fij)i,j=1p,q

3.3.1Jacobian矩阵形式

即先把矩阵变元 X \textbf{X} X按vec向量化,转换成向量变元:
v e c ( X ) = [ x 11 , x 21 , ⋯   , x m 1 , x 12 , x 22 , ⋯   , x m 2 , ⋯   , x 1 n , x 2 n , ⋯   , x m n ] T vec(\textbf{X})=[x_{11},x_{21},\cdots,x_{m1},x_{12},x_{22},\cdots,x_{m2},\cdots,x_{1n},x_{2n},\cdots,x_{mn}]^T vec(X)=[x11,x21,,xm1,x12,x22,,xm2,,x1n,x2n,,xmn]T
再把实矩阵函数 F \textbf{F} F v e c vec vec向量化,转换成实向量函数:
v e c ( F(X) ) = [ f 11 ( X ) , f 21 ( X ) , ⋯   , f p 1 ( X ) , f 12 ( X ) , f 22 ( X ) , ⋯   , f p 2 ( X ) , ⋯   , f 1 q ( X ) , f 2 q ( X ) , ⋯   , f p q ( X ) ] T vec(\textbf{F(X)})=\left[ \begin{matrix} f_{11}(\textbf{X}),f_{21}(\textbf{X}),\cdots,f_{p1}(\textbf{X}),f_{12}(\textbf{X}),f_{22}(\textbf{X}),\cdots,f_{p2}(\textbf{X}),\cdots,f_{1q}(\textbf{X}),f_{2q}(\textbf{X}),\cdots,f_{pq}(\textbf{X}) \end{matrix} \right]^T vec(F(X))=[f11(X),f21(X),,fp1(X),f12(X),f22(X),,fp2(X),,f1q(X),f2q(X),,fpq(X)]T
这样,我们就把一个矩阵变元的实矩阵函数 F(X) \textbf{F(X)} F(X),转换成了向量变元的实向量函数 f(x) \textbf{f(x)} f(x)。接写出结果布局为 p q × m n pq\times mn pq×mn的矩阵:
D X F(X) = ∂ v e c p q × 1 ( F(X) ) ∂ v e c m n × 1 T X = [ f 11 ∂ x 11 f 11 ∂ x 21 ⋯ f 11 ∂ x m 1 f 11 ∂ x 12 f 11 ∂ x 22 ⋯ f 11 ∂ x m 2 ⋯ f 11 ∂ x 1 n f 11 ∂ x 2 n ⋯ f 11 ∂ x m n f 21 ∂ x 11 f 21 ∂ x 21 ⋯ f 21 ∂ x m 1 f 21 ∂ x 12 f 21 ∂ x 22 ⋯ f 21 ∂ x m 2 ⋯ f 21 ∂ x 1 n f 21 ∂ x 2 n ⋯ f 21 ∂ x m n ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ f p 1 ∂ x 11 f p 1 ∂ x 21 ⋯ f p 1 ∂ x m 1 f p 1 ∂ x 12 f p 1 ∂ x 22 ⋯ f p 1 ∂ x m 2 ⋯ f p 1 ∂ x 1 n f p 1 ∂ x 2 n ⋯ f p 1 ∂ x m n f 12 ∂ x 11 f 12 ∂ x 21 ⋯ f 12 ∂ x m 1 f 12 ∂ x 12 f 12 ∂ x 22 ⋯ f 12 ∂ x m 2 ⋯ f 12 ∂ x 1 n f 12 ∂ x 2 n ⋯ f 12 ∂ x m n f 22 ∂ x 11 f 22 ∂ x 21 ⋯ f 22 ∂ x m 1 f 22 ∂ x 12 f 22 ∂ x 22 ⋯ f 22 ∂ x m 2 ⋯ f 22 ∂ x 1 n f 22 ∂ x 2 n ⋯ f 22 ∂ x m n ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ f p 2 ∂ x 11 f p 2 ∂ x 21 ⋯ f p 2 ∂ x m 1 f p 2 ∂ x 12 f p 2 ∂ x 22 ⋯ f p 2 ∂ x m 2 ⋯ f p 2 ∂ x 1 n f p 2 ∂ x 2 n ⋯ f p 2 ∂ x m n ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ f 1 q ∂ x 11 f 1 q ∂ x 21 ⋯ f 1 q ∂ x m 1 f 1 q ∂ x 12 f 1 q ∂ x 22 ⋯ f 1 q ∂ x m 2 ⋯ f 1 q ∂ x 1 n f 1 q ∂ x 2 n ⋯ f 1 q ∂ x m n f 2 q ∂ x 11 f 2 q ∂ x 21 ⋯ f 2 q ∂ x m 1 f 2 q ∂ x 12 f 2 q ∂ x 22 ⋯ f 2 q ∂ x m 2 ⋯ f 2 q ∂ x 1 n f 2 q ∂ x 2 n ⋯ f 2 q ∂ x m n ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ f p q ∂ x 11 f p q ∂ x 21 ⋯ f p q ∂ x m 1 f p q ∂ x 12 f p q ∂ x 22 ⋯ f p q ∂ x m 2 ⋯ f p q ∂ x 1 n f p q ∂ x 2 n ⋯ f p q ∂ x m n ] p q × m n D_{\textbf{X}}\text{F(X)}=\frac{\partial vec_{pq\times 1}(\textbf{F(X)})}{\partial vec_{mn\times 1}^T\textbf{X}}=\left[ \begin{matrix} \frac{f_{11}}{\partial x_{11}} & \frac{f_{11}}{\partial x_{21}} & \cdots & \frac{f_{11}}{\partial x_{m1}} & \frac{f_{11}}{\partial x_{12}} & \frac{f_{11}}{\partial x_{22}} & \cdots & \frac{f_{11}}{\partial x_{m2}} & \cdots & \frac{f_{11}}{\partial x_{1n}} & \frac{f_{11}}{\partial x_{2n}} & \cdots & \frac{f_{11}}{\partial x_{mn}}\\ \frac{f_{21}}{\partial x_{11}} & \frac{f_{21}}{\partial x_{21}} & \cdots & \frac{f_{21}}{\partial x_{m1}} & \frac{f_{21}}{\partial x_{12}} & \frac{f_{21}}{\partial x_{22}} & \cdots & \frac{f_{21}}{\partial x_{m2}} & \cdots & \frac{f_{21}}{\partial x_{1n}} & \frac{f_{21}}{\partial x_{2n}} & \cdots & \frac{f_{21}}{\partial x_{mn}}\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\ \frac{f_{p1}}{\partial x_{11}} & \frac{f_{p1}}{\partial x_{21}} & \cdots & \frac{f_{p1}}{\partial x_{m1}} & \frac{f_{p1}}{\partial x_{12}} & \frac{f_{p1}}{\partial x_{22}} & \cdots & \frac{f_{p1}}{\partial x_{m2}} & \cdots & \frac{f_{p1}}{\partial x_{1n}} & \frac{f_{p1}}{\partial x_{2n}} & \cdots & \frac{f_{p1}}{\partial x_{mn}}\\ \frac{f_{12}}{\partial x_{11}} & \frac{f_{12}}{\partial x_{21}} & \cdots & \frac{f_{12}}{\partial x_{m1}} & \frac{f_{12}}{\partial x_{12}} & \frac{f_{12}}{\partial x_{22}} & \cdots & \frac{f_{12}}{\partial x_{m2}} & \cdots & \frac{f_{12}}{\partial x_{1n}} & \frac{f_{12}}{\partial x_{2n}} & \cdots & \frac{f_{12}}{\partial x_{mn}}\\ \frac{f_{22}}{\partial x_{11}} & \frac{f_{22}}{\partial x_{21}} & \cdots & \frac{f_{22}}{\partial x_{m1}} & \frac{f_{22}}{\partial x_{12}} & \frac{f_{22}}{\partial x_{22}} & \cdots & \frac{f_{22}}{\partial x_{m2}} & \cdots & \frac{f_{22}}{\partial x_{1n}} & \frac{f_{22}}{\partial x_{2n}} & \cdots & \frac{f_{22}}{\partial x_{mn}}\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\ \frac{f_{p2}}{\partial x_{11}} & \frac{f_{p2}}{\partial x_{21}} & \cdots & \frac{f_{p2}}{\partial x_{m1}} & \frac{f_{p2}}{\partial x_{12}} & \frac{f_{p2}}{\partial x_{22}} & \cdots & \frac{f_{p2}}{\partial x_{m2}} & \cdots & \frac{f_{p2}}{\partial x_{1n}} & \frac{f_{p2}}{\partial x_{2n}} & \cdots & \frac{f_{p2}}{\partial x_{mn}}\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\ \frac{f_{1q}}{\partial x_{11}} & \frac{f_{1q}}{\partial x_{21}} & \cdots & \frac{f_{1q}}{\partial x_{m1}} & \frac{f_{1q}}{\partial x_{12}} & \frac{f_{1q}}{\partial x_{22}} & \cdots & \frac{f_{1q}}{\partial x_{m2}} & \cdots & \frac{f_{1q}}{\partial x_{1n}} & \frac{f_{1q}}{\partial x_{2n}} & \cdots & \frac{f_{1q}}{\partial x_{mn}}\\ \frac{f_{2q}}{\partial x_{11}} & \frac{f_{2q}}{\partial x_{21}} & \cdots & \frac{f_{2q}}{\partial x_{m1}} & \frac{f_{2q}}{\partial x_{12}} & \frac{f_{2q}}{\partial x_{22}} & \cdots & \frac{f_{2q}}{\partial x_{m2}} & \cdots & \frac{f_{2q}}{\partial x_{1n}} & \frac{f_{2q}}{\partial x_{2n}} & \cdots & \frac{f_{2q}}{\partial x_{mn}}\\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots\\ \frac{f_{pq}}{\partial x_{11}} & \frac{f_{pq}}{\partial x_{21}} & \cdots & \frac{f_{pq}}{\partial x_{m1}} & \frac{f_{pq}}{\partial x_{12}} & \frac{f_{pq}}{\partial x_{22}} & \cdots & \frac{f_{pq}}{\partial x_{m2}} & \cdots & \frac{f_{pq}}{\partial x_{1n}} & \frac{f_{pq}}{\partial x_{2n}} & \cdots & \frac{f_{pq}}{\partial x_{mn}} \end{matrix} \right]_{pq\times mn} DXF(X)=vecmn×1TXvecpq×1(F(X))= x11f11x11f21x11fp1x11f12x11f22x11fp2x11f1qx11f2qx11fpqx21f11x21f21x21fp1x21f12x21f22x21fp2x21f1qx21f2qx21fpqxm1f11xm1f21xm1fp1xm1f12xm1f22xm1fp2xm1f1qxm1f2qxm1fpqx12f11x12f21x12fp1x12f12x12f22x12fp2x12f1qx12f2qx12fpqx22f11x22f21x22fp1x22f12x22f22x22fp2x22f1qx22f2qx22fpqxm2f11xm2f21xm2fp1xm2f12xm2f22xm2fp2xm2f1qxm2f2qxm2fpqx1nf11x1nf21x1nfp1x1nf12x1nf22x1nfp2x1nf1qx1nf2qx1nfpqx2nf11x2nf21x2nfp1x2nf12x2nf22x2nfp2x2nf1qx2nf2qx2nfpqxmnf11xmnf21xmnfp1xmnf12xmnf22xmnfp2xmnf1qxmnf2qxmnfpq pq×mn

3.3.2梯度矩阵形式

即先把矩阵变元 X \textbf{X} X v e c vec vec向量化,转换成向量变元:
v e c ( X ) = [ x 11 , x 21 , ⋯   , x m 1 , x 12 , x 22 , ⋯   , x m 2 , ⋯   , x 1 n , x 2 n , ⋯   , x m n ] T vec(\textbf{X})=[x_{11},x_{21},\cdots,x_{m1},x_{12},x_{22},\cdots,x_{m2},\cdots,x_{1n},x_{2n},\cdots,x_{mn}]^T vec(X)=[x11,x21,,xm1,x12,x22,,xm2,,x1n,x2n,,xmn]T
再把实矩阵函数 F \textbf{F} F v e c vec vec向量化,转换成实向量函数:
v e c ( F(x) ) = [ f 11 ( X ) , f 21 ( X ) , ⋯   , f p 1 ( X ) , f 12 ( X ) , f 22 ( X ) , ⋯   , f p 2 ( X ) , ⋯   , f 1 q ( X ) , f 2 q ( X ) , ⋯   , f p q ( X ) ] T vec(\textbf{F(x)})=[f_{11}(\textbf{X}),f_{21}(\textbf{X}),\cdots,f_{p1}(\textbf{X}),f_{12}(\textbf{X}),f_{22}(\textbf{X}),\cdots,f_{p2}(\textbf{X}),\cdots,f_{1q}(\textbf{X}),f_{2q}(\textbf{X}),\cdots,f_{pq}(\textbf{X})]^T vec(F(x))=[f11(X),f21(X),,fp1(X),f12(X),f22(X),,fp2(X),,f1q(X),f2q(X),,fpq(X)]T
这样,我们就把一个矩阵变元的实矩阵函数转换成了向量变元的实向量函数 f(x) \textbf{f(x)} f(x)。写出结果布局为 m n × p q mn\times pq mn×pq

矩阵求导的本质与分子布局、分母布局的本质(一)_第1张图片

四. 分子布局、分母布局的本质

看到这里,相信同学们对矩阵求导结果的布局有了很全面的了解了,无非就是分子的转置、向量化,分母的转置、向量化,它们的各种组合而已。

结合上述知识,我们总结:

1、分子布局的本质:分子是标量、列向量、矩阵向量化后的列向量;分母是标量、列向量转置后的行向量、矩阵的转置矩阵、矩阵向量化后的列向量转置后的行向量。

2、分母布局的本质:分子是标量、列向量转置后的行向量、矩阵向量化后的列向量转置后的行向量;分母是标量、列向量、矩阵自己、矩阵向量化后的列向量。

思考一下,其实我们可以再简洁一些:谁转置了,就是另一方的布局。分子转置了,就是分母布局;分母转置了,就是分子布局。

你可能感兴趣的:(深度学习笔记)