# 布局

• 分子布局（numerator layout）
• 分母布局（denominator layout）

y=⎡⎣⎢⎢⎢⎢⎢y1y2⋮ym⎤⎦⎥⎥⎥⎥⎥

\mathbf{y}=\begin{bmatrix}y_{1}\\ y_{2}\\ \vdots\\ y_{m} \end{bmatrix}

∂y∂x=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢∂y1∂x∂y2∂x⋮∂ym∂x⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

\frac{\partial\mathbf{y}}{\partial x}=\begin{bmatrix}\frac{\partial y_{1}}{\partial x}\\ \frac{\partial y_{2}}{\partial x}\\ \vdots\\ \frac{\partial y_{m}}{\partial x} \end{bmatrix}

∂y∂x=[∂y1∂x∂y2∂x⋯∂ym∂x]

\frac{\partial\mathbf{y}}{\partial x}=\begin{bmatrix}\frac{\partial y_{1}}{\partial x} & \frac{\partial y_{2}}{\partial x} & \cdots & \frac{\partial y_{m}}{\partial x}\end{bmatrix} %]]>

# 求导的类别

1. 向量对标量
2. 标量对向量
3. 向量对向量
4. 矩阵对向量
5. 向量对矩阵

∂y∂x=[∂y1∂x∂y2∂x⋯∂ym∂x]

\frac{\partial\mathbf{y}}{\partial x}=\begin{bmatrix}\frac{\partial y_{1}}{\partial x} & \frac{\partial y_{2}}{\partial x} & \cdots & \frac{\partial y_{m}}{\partial x}\end{bmatrix} %]]>

∂y∂x=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢∂y∂x1∂y∂x2⋮∂y∂xm⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

\frac{\partial y}{\partial\mathbf{x}}=\begin{bmatrix}\frac{\partial y}{\partial x_{1}}\\ \frac{\partial y}{\partial x_{2}}\\ \vdots\\ \frac{\partial y}{\partial x_{m}} \end{bmatrix}

x=⎡⎣⎢⎢⎢⎢x1x2⋮xn⎤⎦⎥⎥⎥⎥

\mathbf{x}=\begin{bmatrix}x_{1}\\ x_{2}\\ \vdots\\ x_{n} \end{bmatrix}

y=⎡⎣⎢⎢⎢⎢⎢y1y2⋮ym⎤⎦⎥⎥⎥⎥⎥

\mathbf{y}=\begin{bmatrix}y_{1}\\ y_{2}\\ \vdots\\ y_{m} \end{bmatrix}

∂y∂x=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢∂y1∂x1∂y1∂x2⋮∂y1∂xn∂y2∂x1∂y2∂x2⋮∂y2∂xn⋯⋯⋱⋯∂ym∂x1∂ym∂x2⋮∂ym∂xn⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

\frac{\partial\mathbf{y}}{\partial\mathbf{x}}=\begin{bmatrix}\frac{\partial y_{1}}{\partial x_{1}} & \frac{\partial y_{2}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{1}}\\ \frac{\partial y_{1}}{\partial x_{2}} & \frac{\partial y_{2}}{\partial x_{2}} & \cdots & \frac{\partial y_{m}}{\partial x_{2}}\\ \vdots & \vdots & \ddots & \vdots\\ \frac{\partial y_{1}}{\partial x_{n}} & \frac{\partial y_{2}}{\partial x_{n}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}} \end{bmatrix} %]]>

∂y∂x=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢∂y11∂x∂y12∂x⋮∂y1n∂x∂y21∂x∂y22∂x⋮∂y2n∂x⋯⋯⋱⋯∂ym1∂x∂ym2∂x⋮∂ymn∂x⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

\frac{\partial\mathbf{y}}{\partial x}=\begin{bmatrix}\frac{\partial y_{11}}{\partial x} & \frac{\partial y_{21}}{\partial x} & \cdots & \frac{\partial y_{m1}}{\partial x}\\ \frac{\partial y_{12}}{\partial x} & \frac{\partial y_{22}}{\partial x} & \cdots & \frac{\partial y_{m2}}{\partial x}\\ \vdots & \vdots & \ddots & \vdots\\ \frac{\partial y_{1n}}{\partial x} & \frac{\partial y_{2n}}{\partial x} & \cdots & \frac{\partial y_{mn}}{\partial x} \end{bmatrix} %]]> 标量对矩阵求导：

∂y∂X=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢∂y∂x11∂y∂x21⋮∂y∂xp1∂y∂x12∂y∂x22⋮∂y∂xp2⋯⋯⋱⋯∂y∂x1q∂y∂x2q⋮∂y∂xpq⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

\frac{\partial y}{\partial\mathbf{X}}=\begin{bmatrix}\frac{\partial y}{\partial x_{11}} & \frac{\partial y}{\partial x_{12}} & \cdots & \frac{\partial y}{\partial x_{1q}}\\ \frac{\partial y}{\partial x_{21}} & \frac{\partial y}{\partial x_{22}} & \cdots & \frac{\partial y}{\partial x_{2q}}\\ \vdots & \vdots & \ddots & \vdots\\ \frac{\partial y}{\partial x_{p1}} & \frac{\partial y}{\partial x_{p2}} & \cdots & \frac{\partial y}{\partial x_{pq}} \end{bmatrix} %]]>

# 从简单的例子说起

y=aTx

\mathbb{y} = \mathbb{a}^\mathrm{T}\mathbb{x}

∂y∂x=a

\frac{\partial y}{\partial x} = a

y=Ax

\mathbb{y} = \mathrm{A}\mathrm{x}

∂y∂x=AT

\frac{\partial y}{\partial x} = \mathrm{A}^\mathrm{T}

y=Au(x)

\mathbb{y} = \mathrm{A}\mathrm{u}(x)

∂y∂x=∂u∂xAT

\frac{\partial y}{\partial x} = \frac{\partial u}{\partial x} \mathrm{A}^{\mathrm{T}}

y=a(x)u(x)

\mathbb{y} = \mathrm{a(x)}\mathrm{u}(x)

∂y∂x=∂u∂xa+∂a∂xuT

\frac{\partial y}{\partial x} = \frac{\partial u}{\partial x} \mathrm{a}+\frac{\partial a}{\partial x} \mathrm{u}^{\mathrm{T}}

a(x)u(x)=Bx=Cx

\begin{split} a(x)&=Bx\\ u(x)&=Cx \end{split}

∂y∂x=CTa+BTuT

\frac{\partial y}{\partial x} =\mathrm{C}^{\mathrm{T}}\mathrm{a}+\mathrm{B}^{\mathrm{T}}\mathrm{u}^{\mathrm{T}}

f=xTAy(x)

\mathrm{f} = \mathbf{x}^{\mathrm{T}}\mathbf{Ay(x)} 那么，

∂f∂x=Ay+∂y∂xATx

\frac{\partial f}{\partial x} =Ay+\frac{\partial y}{\partial x} A^T x

f∂f∂x=xTAx=(A+AT)x

\begin{split} &\mathrm{f}& = \mathbf{x}^{\mathrm{T}}\mathrm{A}\mathbf{x}\\ &\frac{\partial f}{\partial x} &= (A+A^T)x \end{split}

f=aTxxTb,a,b,x∈Rm×1

\mathbb{f} = \mathbf{a}^{\mbox{T}}\mathbf{xx}^{\mbox{T}}\mathbf{b} ,\mathbf{a,b,x}\in\mathbb{R}^{m\times1}

∂f∂x=a(xTb)+b(aTx)=(abT+baT)x

\frac{\partial f}{\partial x} = a(x^Tb) + b(a^Tx) = (ab^T+ba^T)x

# 实例

## SVM的对偶形式转换

SVM的原形式（primary form）是：

minw,bs.t.12wTwyn(wTxn+b)≥1

\begin{split} &\min_{w,b} \quad &\frac{1}{2} w^Tw\\ &s.t. & y_n(w^Tx_n+b) \ge1 \end{split}

SVM的对偶形式（dual form）是：

minw,bmaxα≥0maxα≥0minw,b12wTw+∑n=1Nαn[1−yn(wTxn+b)]12wTw+∑n=1Nαn[1−yn(wTxn+b)]

\begin{split} &\min_{w,b} \max_{\alpha\ge 0} & \frac{1}{2} w^Tw + \sum_{n=1}^N \alpha_n [1- y_n(w^Tx_n+b)] \\ &\max_{\alpha\ge 0} \min_{w,b} &\frac{1}{2} w^Tw + \sum_{n=1}^N \alpha_n [1- y_n(w^Tx_n+b)]\end{split}

w∑n=1Nαnyn=∑n=1Nαnynxn=0

\begin{split} w &= \sum_{n=1}^N \alpha_n y_n x_n\\ \sum_{n=1}^N \alpha_n y_n &= 0 \end{split}

minα12∑n=1Ns.t.∑n=1Nαnynαn∑m=1NαnαmynymxmTxn−∑n=1Nαn=0≥0

\begin{split}\min_\alpha \frac{1}{2}\sum_{n=1}^N&\sum_{m=1}^N \alpha_n \alpha_m y_n y_m {x_m}^T x_n - \sum_{n=1}^N \alpha_n \\ s.t. \quad \sum_{n=1}^N \alpha_n y_n &= 0 \\ \alpha_n &\ge 0 \end{split}

## Soft-SVM对偶形式转换

SVM的原形式（primary form）是：

minw,b,εs.t.12wTw+C∑n=1Nεnyn(wTxn+b)≥1−εnεn≥0

\begin{split} &\min_{w,b,\varepsilon} \quad &\frac{1}{2} w^Tw + C \sum_{n=1}^N \varepsilon_n \\ &s.t. & y_n(w^Tx_n+b) \ge1-\varepsilon_n \\ & &\varepsilon_n \ge 0 \end{split}

minα12∑n=1Ns.t.∑n=1Nαnyn0≤αn∑m=1NαnαmynymxmTxn−∑n=1Nαn=0≤C

\begin{split}\min_\alpha \frac{1}{2}\sum_{n=1}^N&\sum_{m=1}^N \alpha_n \alpha_m y_n y_m {x_m}^T x_n - \sum_{n=1}^N \alpha_n \\ s.t. \quad \sum_{n=1}^N \alpha_n y_n &= 0 \\ 0 \le\alpha_n &\le C \end{split}

## 线性回归

Ein(w)=1N∑n=1N(wTx−y)2=1N∥XW−Y∥2

{E}_{in} (w) = \frac{1}{N} \sum_{n=1}^N (w^T x -y)^2=\frac{1}{N} \Vert XW-Y\Vert ^2

∇Ein(w)=2NXT(XW−Y)

\nabla E_{in}(w)=\frac{2}{N} X^T(XW-Y)

WW=(XTX)−1XTY=X†Y

\begin{split} W &= (X^TX)^{-1} X^TY\\ W &=X^\dagger Y \end{split}

## logistic回归

θ(s)h(x)=es1+es=11+e−s=θ(wTx)

\begin{split} \theta(s) &= \frac{e^s}{1+e^s} = \frac{1}{1+e^{-s}}\\ h(x)&= \theta(w^Tx) \end{split} 接着，根据最大似然，并且利用 1−h(x)=h(−x)1-h(x) = h(-x)的性质，最大化点出现的概率：

max∏θ(ynwTxn)min∑n=1Nln(1+exp(−ynwTxn))

\begin{split} &\max \prod \theta(y_n w^T x_n)\\ &\min \sum_{n=1}^N ln(1+exp(-y_nw^Tx_n)) \end{split}

s.t.min∑n=1Nln(1+exp(−ynwTxn))∑n=1Nθ(−ynwTxn)(−ynxn)=0

\begin{split} &\min \sum_{n=1}^N ln(1+exp(-y_nw^Tx_n)) \\ s.t. & \sum_{n=1}^N \theta(-y_n w^T x_n)(-y_nx_n) = 0 \end{split}

GD:

∇Ein(wt)wt+1=1N∑n=1Nθ(−ynwTxn)(−ynxn)=wt−η∇Ein(wt)

\begin{split} \nabla E_{in} (w_t) &= \frac{1}{N} \sum_{n=1}^N \theta(-y_n w^T x_n)(-y_nx_n) \\ w_{t+1}&= w_t - \eta \nabla E_{in} (w_t) \end{split}

SGD:

wt+1=wt−ηθ(−ynwTxn)(−ynxn)

w_{t+1}= w_t -\eta \theta(-y_n w^T x_n)(-y_nx_n)

# 参考资料

1. 闲话矩阵求导

99 篇文章33 人订阅

0 条评论

## 相关文章

3904

2320

### 硅谷网红从基础教深度学习中的数学，视频演示不可错过

Siraj Raval 作为深度学习领域的自媒体人在欧美可以说是无人不知、无人不晓。 凭借在 Youtube 上的指导视频，Siraj Raval 在全世界吸...

4979

5847

2023

3888

1957

1294

3206

### 一文让你入门CNN，附3份深度学习视频资源

CNN简介 文末附三份深度学习视频资源 后台回复关键词（20180310） 目录： 一些视频资源和文章 CNN简介 图像即四维张量？ 卷积的定义 CNN如何工作...

3987