【机器学习中的矩阵求导】(七)矩阵向量化复习 |
您所在的位置:网站首页 › 矩阵×矩阵的 › 【机器学习中的矩阵求导】(七)矩阵向量化复习 |
学习总结
(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)=(C⊤⊗A)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(A⊙X)=diag(Vec(A))Vec(X) 文章目录 学习总结一、几个梯度的栗子1.1 迹函数相对于矩阵的梯度1.2 行列式相对于矩阵的梯度 二、实值函数相对于实向量的梯度2.1 实值标量函数对向量的梯度2.2 实值向量函数对向量的梯度2.3 简单练习 三、矩阵向量化vec3.1 向量化定义3.2 向量化算子性质 四、Python实现Kronecker积等Reference 一、几个梯度的栗子 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 矩阵的行列式对矩阵求导: 其实就是标量函数对向量的求导,在之前我们用过定义法求导: 寻找较复杂的实值函数求导更方便的方法,不是每次都先针对任意一个分量,再进行排列。 标量对向量求导的基本法则(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)=c1∂x∂f(x)+c2∂x∂g(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}) ∂x∂f(x)g(x)=f(x)∂x∂g(x)+∂x∂f(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) ∂x∂f(x)/g(x)=g2(x)1(g(x)∂x∂f(x)−f(x)∂x∂g(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)=[∂x1∂f(x),∂x2∂f(x),⋯,∂xn∂f(x)]T=∂x∂f(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} ∂xj∂Aix=∂xj∂Aijxj=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}) ∂x∂f(x)=[∂x∂f1(x),∂x∂f2(x),⋯,∂x∂fm(x)]=⎣⎢⎢⎢⎢⎡∂x1∂f1(x)∂x2∂f1(x)⋮∂xn∂f1(x)∂x1∂f2(x)∂x2∂f2(x)⋮∂xn∂f2(x)……⋱…∂x1∂fm(x)∂x2∂fm(x)⋮∂xn∂fm(x)⎦⎥⎥⎥⎥⎤=∇xf(x) 向量函数对于向量的求导,相当于向量函数中的每一个分量函数对向量求导。行向量函数对列向量自变量求导形成矩阵;列向量函数对行向量自变量求导也可以形成矩阵。 2.3 简单练习向量化算子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)=(a11a21⋯am1;a12a22⋯am2;⋯;a1na2n⋯amn)⊤ 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)=(C⊤⊗A)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)=(I⊗A)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)=(C⊤⊗I)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(A⊙X)=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 矩阵。 (1)https://numpy.org/doc/stable/reference/generated/numpy.kron.html#numpy.kron (2)numpy中dot()、outer()、multiply()以及matmul()的区别 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |