矩阵求导基础与最小二乘法应用解析

公式速查

请添加图片描述
请添加图片描述

矩阵求导

符号规定

符号含义
A \mathbf{A} A X \mathbf{X} X矩阵
a \mathbf{a} a x \mathbf{x} x(列)向量
a a a x x x标量

为什么要矩阵求导?

在计算机世界中,数据通常使用矩阵(或向量、张量)来表示。使用矩阵来表示和操作数据有助于更高效地处理多维数据;使用矩阵表达有助于简化数学公式,形式简洁;矩阵求导几乎是求解优化问题不可避免的必学内容(例如:随机梯度下降法,即求目标函数相对于各个参数的偏导数,多维)

优点:速度块;numpy 数值计算库

矩阵求导的本质

矩阵求导的本质:对于 d A d B \frac{\mathrm{d} \mathbf{A}}{\mathrm{d} \mathbf{B}} dBdA,即矩阵 A \mathbf{A} A 中的每一个元素对矩阵 B \mathbf{B} B 中的每一个元素求导

存在两种布局:分子布局和分母布局,两者得到的结果为转置关系,下文均采用分母布局

Q:什么叫做向量函数?【答:输出为向量(或矩阵)的函数】

例 1:

f ( x ) = [ f 1 ( x ) f 2 ( x ) ] \begin{align} f(x) = \begin{bmatrix} f_1(x) \\ f_2(x) \end{bmatrix} \end{align} f(x)=[f1(x)f2(x)]

上述函数完成从 R → R 2 \mathbb{R} \rightarrow \mathbb{R}^{2} RR2 的映射

例 2:

f ( x ) = [ f 1 ( x ) = x 1 + x 2 f 2 ( x ) = x 1 2 + x 2 2 f 3 ( x ) = x 1 3 + x 2 3 f 4 ( x ) = x 1 4 + x 2 4 ] \begin{align} f(x) = \begin{bmatrix} f_1(x)=x_1 + x_2 & f_2(x)=x_1^2+x_2^2 \\ f_3(x)=x_1^3+x_2^3 & f_4(x)=x_1^4+x_2^4 \end{bmatrix} \end{align} f(x)=[f1(x)=x1+x2f3(x)=x13+x23f2(x)=x12+x22f4(x)=x14+x24]

上述函数完成从 R → R 2 × 2 \mathbb{R} \rightarrow \mathbb{R}^{2 \times 2} RR2×2 的映射

求导秘术:YX 拉伸

两句口诀:标量不变,向量拉伸;前面横向拉,后面纵向拉

说明:针对 Y 对 X 求导,前面指的是 Y,后面指的是 X;YX 拉伸对应分母布局

下面主要介绍标量对向量、向量对标量、向量对向量三种求导情况

例 1:【标量对向量求导 — 纵向拉

已知: f ( x ) = f ( x 1 , x 2 , … , f n ) f(x) = f(x_1, x_2, …, f_n) f(x)=f(x1,x2,,fn) x = [ x 1 x 2 . . . x n ] x = \begin{bmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{bmatrix} x= x1x2...xn

d f ( x ) d x = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 . . . ∂ f ( x ) ∂ x n ] \begin{align} \frac{\mathrm{d} f(x)}{\mathrm{d} x} &= \begin{bmatrix} \frac{\partial f(x)}{\partial x_1} \\ \frac{\partial f(x)}{\partial x_2} \\ ... \\ \frac{\partial f(x)}{\partial x_n} \\ \end{bmatrix} \end{align} dxdf(x)= x1f(x)x2f(x)...xnf(x)

例 2:【向量对标量求导 —— 横向拉

已知: f ( x ) = [ f 1 ( x ) f 2 ( x ) . . . f n ( x ) ] f(x) = \begin{bmatrix} f_1(x) \\ f_2(x) \\ ... \\ f_n(x) \end{bmatrix} f(x)= f1(x)f2(x)...fn(x) x x x 为标量

d f ( x ) d x = [ ∂ f 1 ( x ) ∂ x ∂ f 2 ( x ) ∂ x . . . ∂ f n ( x ) ∂ x ] \begin{align} \frac{\mathrm{d} f(x)}{\mathrm{d} x} &= \begin{bmatrix} \frac{\partial f_1(x)}{\partial x} & \frac{\partial f_2(x)}{\partial x} & ... & \frac{\partial f_n(x)}{\partial x} \end{bmatrix} \end{align} dxdf(x)=[xf1(x)xf2(x)...xfn(x)]

例 3:【向量对向量求导 —— 先横向拉后纵向拉 or 先纵向拉后横向拉

已知: f ( x ) = [ f 1 ( x 1 , x 2 , . . . , x n ) f 2 ( x 1 , x 2 , . . . , x n ) . . . f n ( x 1 , x 2 , . . . , x n ) ] f(x) = \begin{bmatrix} f_1(x_1, x_2, ..., x_n) \\ f_2(x_1, x_2, ..., x_n) \\ ... \\ f_n(x_1, x_2, ..., x_n) \end{bmatrix} f(x)= f1(x1,x2,...,xn)f2(x1,x2,...,xn)...fn(x1,x2,...,xn) x = [ x 1 x 2 . . . x n ] x = \begin{bmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{bmatrix} x= x1x2...xn

d f ( x ) d x = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 . . . ∂ f ( x ) ∂ x n ] = [ ∂ 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 ] n × m \begin{align} \frac{\mathrm{d} f(x)}{\mathrm{d} x} &= \begin{bmatrix} \frac{\partial f(x)}{\partial x_1} \\ \frac{\partial f(x)}{\partial x_2} \\ ... \\ \frac{\partial f(x)}{\partial x_n} \end{bmatrix} &= \begin{bmatrix} \frac{\partial f_1(x)}{\partial x_1} & \frac{\partial f_2(x)}{\partial x_1} & ... & \frac{\partial f_m(x)}{\partial x_1} \\ \frac{\partial f_1(x)}{\partial x_2} & \frac{\partial f_2(x)}{\partial x_2} & ... & \frac{\partial f_m(x)}{\partial x_2} \\ ... & ... & ... & ... \\ \frac{\partial f_1(x)}{\partial x_n} & \frac{\partial f_2(x)}{\partial x_n} & ... & \frac{\partial f_m(x)}{\partial x_n} \end{bmatrix}_{n \times m} \end{align} dxdf(x)= x1f(x)x2f(x)...xnf(x) = x1f1(x)x2f1(x)...xnf1(x)x1f2(x)x2f2(x)...xnf2(x)............x1fm(x)x2fm(x)...xnfm(x) n×m

常用矩阵求导公式推导

例 1: f ( x ) = a T x f(x) = \mathbf{a}^T \mathbf{x} f(x)=aTx,其中 a ∈ R n × 1 \mathbf{a} \in \mathbb{R}^{n \times 1} aRn×1 x ∈ R n × 1 \mathbf{x} \in \mathbb{R}^{n \times 1} xRn×1

f ( x ) = a 1 x 1 + a 2 x 2 + . . . + a n x n = ∑ i = 1 n a i x i f(x) = a_1 x_1 + a_2 x_2 + ... + a_n x_n = \sum_{i=1}^n a_ix_i f(x)=a1x1+a2x2+...+anxn=i=1naixi

d f ( x ) d x = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 . . . ∂ f ( x ) ∂ x n ] = [ a 1 a 2 . . . a n ] = a \begin{align} \frac{\mathrm{d} f(x)}{\mathrm{d} x} &= \begin{bmatrix} \frac{\partial f(x)}{\partial x_1} \\ \frac{\partial f(x)}{\partial x_2} \\ ... \\ \frac{\partial f(x)}{\partial x_n} \end{bmatrix} \\ &= \begin{bmatrix} a_1 \\ a_2 \\ ... \\ a_n \end{bmatrix} \\ &= \mathbf{a} \end{align} dxdf(x)= x1f(x)x2f(x)...xnf(x) = a1a2...an =a

向量内积: a T x = x T a = [ a , x ] \mathbf{a}^T \mathbf{x} = \mathbf{x}^T \mathbf{a} = [\mathbf{a}, \mathbf{x}] aTx=xTa=[a,x]

例 2: f ( x ) = x T A x f(x) = \mathbf{x}^T \mathbf{A} \mathbf{x} f(x)=xTAx,其中 x ∈ R n × 1 \mathbf{x} \in \mathbb{R}^{n \times 1} xRn×1 A ∈ R n × n \mathbf{A} \in \mathbb{R}^{n \times n} ARn×n

f ( x ) = x T A x = [ x 1 x 2 . . . x n ] [ a 11 a 12 . . . a 1 n a 21 a 22 . . . a 2 n . . . a n 1 a n 2 . . . a n n ] [ x 1 x 2 . . . x n ] = [ x 1 x 2 . . . x n ] [ a 11 x 1 + a 12 x 2 + . . . + a 1 n x n a 21 x 1 + a 22 x 2 + . . . + a 2 n x n . . . a n 1 x 1 + a n 2 x 2 + . . . + a n n x n ] = ( a 11 x 1 + a 12 x 2 + . . . + a 1 n x n ) x 1 + . . . \begin{align} f(x) &= \mathbf{x}^T \mathbf{A} \mathbf{x} \\ &= \begin{bmatrix} x_1 & x_2 & ... & x_n \end{bmatrix} \begin{bmatrix} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ ... \\ a_{n1} & a_{n2} & ... & a_{nn} \\ \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{bmatrix} \\ &= \begin{bmatrix} x_1 & x_2 & ... & x_n \end{bmatrix} \begin{bmatrix} a_{11}x_1 + a_{12}x_2 + ... + a_{1n}x_n \\ a_{21}x_1 + a_{22}x_2 + ... + a_{2n}x_n \\ ... \\ a_{n1}x_1 + a_{n2}x_2 + ... + a_{nn}x_n \\ \end{bmatrix} \\ &= (a_{11}x_1 + a_{12}x_2 + ... + a_{1n}x_n)x_1 + ... \end{align} f(x)=xTAx=[x1x2...xn] a11a21...an1a12a22an2.........a1na2nann x1x2...xn =[x1x2...xn] a11x1+a12x2+...+a1nxna21x1+a22x2+...+a2nxn...an1x1+an2x2+...+annxn =(a11x1+a12x2+...+a1nxn)x1+...

d f ( x ) d x = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 . . . ∂ f ( x ) ∂ x n ] = [ ( a 11 x 1 + a 12 x 2 + . . . + a 1 n x n ) + ( a 11 x 1 + a 21 x 2 + . . . + a n 1 x n ) ( a 21 x 1 + a 22 x 2 + . . . + a 2 n x n ) + ( a 12 x 1 + a 22 x 2 + . . . + a n 2 x n ) . . . ( a n 1 x 1 + a n 2 x 2 + . . . + a n n x n ) + ( a 1 n x 1 + a 2 n x 2 + . . . + a n n x n ) ] = ( A + A T ) x \begin{align} \frac{\mathrm{d} f(x)}{\mathrm{d} x} &= \begin{bmatrix} \frac{\partial f(x)}{\partial x_1} \\ \frac{\partial f(x)}{\partial x_2} \\ ... \\ \frac{\partial f(x)}{\partial x_n} \end{bmatrix} \\ &= \begin{bmatrix} (a_{11}x_1 + a_{12}x_2 + ... + a_{1n}x_n) + (a_{11}x_1 + a_{21}x_2 + ... + a_{n1}x_n) \\ (a_{21}x_1 + a_{22}x_2 + ... + a_{2n}x_n) + (a_{12}x_1 + a_{22}x_2 + ... + a_{n2}x_n) \\ ... \\ (a_{n1}x_1 + a_{n2}x_2 + ... + a_{nn}x_n) + (a_{1n}x_1 + a_{2n}x_2 + ... + a_{nn}x_n) \end{bmatrix} \\ &= (\mathbf{A} + \mathbf{A}^T)\mathbf{x} \end{align} dxdf(x)= x1f(x)x2f(x)...xnf(x) = (a11x1+a12x2+...+a1nxn)+(a11x1+a21x2+...+an1xn)(a21x1+a22x2+...+a2nxn)+(a12x1+a22x2+...+an2xn)...(an1x1+an2x2+...+annxn)+(a1nx1+a2nx2+...+annxn) =(A+AT)x

矩阵求导应用:最小二乘法

问题描述:给定一个输入矩阵 X ∈ R m × n \mathbf{X} \in \mathbb{R}^{m \times n} XRm×n 和一个目标值向量 y ∈ R m \mathbf{y} \in \mathbb{R}^m yRm,线性回归模型的参数向量为 w ∈ R n \mathbf{w} \in \mathbb{R}^n wRn,模型的预测值为: y ^ = X w \hat{\mathbf{y}} = \mathbf{X} \mathbf{w} y^=Xw。其中: X \mathbf{X} X 是输入特征矩阵,每一行表示一个样本,每一列表示一个特征; w \mathbf{w} w 是模型的待优化参数,即权重向量; y ^ \hat{\mathbf{y}} y^ 是模型的预测结果。

在线性回归问题中,最小二乘法的目标是拟合一个线性模型,使得模型的预测值与真实数据之间的误差最小化,即目标函数为: L ( w ) = ∥ X w − y ∥ 2 L(\mathbf{w}) = \|\mathbf{X}\mathbf{w} - \mathbf{y}\|^2 L(w)=Xwy2。为了找到使得损失函数最小的权重向量 w \mathbf{w} w,我们需要对损失函数 L ( w ) L(\mathbf{w}) L(w) 关于 w \mathbf{w} w 求导,并令导数为 0,进而求解最优参数。

L ( w ) = ∥ X w − y ∥ 2 = ( X w − y ) T ( X w − y ) = ( w T X T − y T ) ( X w − y ) = w T X T X w − w T X T y − y T X w + y T y \begin{align} L(\mathbf{w}) &= \|\mathbf{X}\mathbf{w} - \mathbf{y}\|^2 \\ &= (\mathbf{X}\mathbf{w} - \mathbf{y})^T (\mathbf{X}\mathbf{w} - \mathbf{y}) \\ &=(\mathbf{w}^T \mathbf{X}^T - \mathbf{y}^T) (\mathbf{X}\mathbf{w} - \mathbf{y}) \\ &= \mathbf{w}^T \mathbf{X}^T \mathbf{X}\mathbf{w} - \mathbf{w}^T \mathbf{X}^T \mathbf{y} - \mathbf{y}^T \mathbf{X}\mathbf{w} + \mathbf{y}^T \mathbf{y} \end{align} L(w)=Xwy2=(Xwy)T(Xwy)=(wTXTyT)(Xwy)=wTXTXwwTXTyyTXw+yTy

∂ L ( w ) ∂ w = ( X T X + ( X T X ) T ) w − X T y − y T X = 2 ( X T X w − X T y ) \begin{align} \frac{\partial L(\mathbf{w})}{\partial \mathbf{w}} &= (\mathbf{X}^T \mathbf{X} + (\mathbf{X}^T \mathbf{X})^T) \mathbf{w} - \mathbf{X}^T \mathbf{y} - \mathbf{y}^T\mathbf{X} \\ &= 2( \mathbf{X}^T \mathbf{X} \mathbf{w} - \mathbf{X}^T \mathbf{y}) \end{align} wL(w)=(XTX+(XTX)T)wXTyyTX=2(XTXwXTy)

∂ L ( w ) ∂ w = 0 \frac{\partial L(\mathbf{w})}{\partial \mathbf{w}} = 0 wL(w)=0 w = ( X T X ) − 1 X T y \mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1}\mathbf{X}^T\mathbf{y} w=(XTX)1XTy

参考资料

【手推机器学习】矩阵求导–合集:https://www.bilibili.com/video/BV1xk4y1B7RQ

空狐公子:https://www.bilibili.com/video/BV1fK411W7oh

西凉阿土伯:https://www.bilibili.com/video/BV13J41157Wq

Mathematics for Machine Learning 书籍下载地址:https://mml-book.github.io

《计算机视觉》by 张林:https://cslinzhang.github.io/home/CV/CV.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值