Fork me on GitHub

TF-char7-反向传播法

TF-char7-反向传播法

第七章讲解的是关于$\color{red}{反向传播法}$,主要的知识点包含:

  • 导数和梯度基础知识点
  • 神经网络中常用的几个激活函数的导数
  • 损失函数的梯度计算
  • 全连接的梯度计算
    • 单个神经元
    • 全连接层
  • 反向传播法

导数和梯度

导数

函数的输出值的增量和自变量的增量的比值在$\triangle x$趋于0的时极限为$a$,定义为:
$$
a=\lim _{\Delta x \rightarrow 0} \frac{\Delta y}{\Delta x}=\lim {\Delta x \rightarrow 0} \frac{f\left(x+x{0}\right)-f(x)}{\Delta x}
$$
导数表征了函数在某个方向上的变化率。

在网络中自变量是网络参数集$\theta={w_1,b_1,…,w_n,b_n}$,对于每个参数的偏导数为:
$$
\nabla \mathcal{L}=\left(\frac{\partial \mathcal{L}}{\partial \theta_{1}}, \frac{\partial \mathcal{L}}{\partial \theta_{2}}, \frac{\partial \mathcal{L}}{\partial \theta_{3}}, \ldots \frac{\partial \mathcal{L}}{\partial \theta_{n}}\right)
$$
梯度下降法沿着向量的形式更新梯度值
$$
\theta^{\prime}=\theta-\eta * \nabla \mathcal{L}
$$
如果希望求解函数的最大值,需要按着梯度方向更新:
$$
\theta^{\prime}=\theta+\eta * \nabla \mathcal{L}
$$
称之为梯度上升法

梯度

向量$\left(\frac{\partial \mathcal{L}}{\partial \theta_{1}}, \frac{\partial \mathcal{L}}{\partial \theta_{2}}, \frac{\partial \mathcal{L}}{\partial \theta_{3}}, \ldots \frac{\partial \mathcal{L}}{\partial \theta_{n}}\right)$叫做函数的梯度

  • 有所有的梯度组成
  • 表征方向

重要导数

$$
©^`=0
$$

$$
(xa)`=ax^{a-1}
$$

$$
(ax)`=a^xln
$$

$$
(ex)`=e^x
$$

$$
(log_ax)^`=\frac{1}{xlna}
$$

$$
(lnx)^`=\frac{1}{x}
$$

$$
(sinx)^`=coxs
$$

$$
(cosx)^`=-sinx
$$

$$
(tanx)`=\frac{1}{cos2x}=sec^2x
$$

$$
(cotx)`=-\frac{1}{sin2x} = -csc^2x
$$

$$
(arcsinx)`=\frac{1}{\sqrt{1-x2}}
$$

$$
(arccosx)`=-\frac{1}{\sqrt{1-x2}}
$$

$$
(secx)^` = secx * tanx
$$

$$
(cscx)^`= -cscx * cotx
$$

$$
(arctanx)`=\frac{1}{1+x2}
$$

$$
(arccotx)`=-\frac{1}{1+x2}
$$

$$
(shx)^`=chx
$$

$$
(chx)^`=shx
$$

导数形式

$$
(f+g){\prime}=f{\prime}+g^{\prime}
$$

$$
(fg)`=f^`*g+f*g`
$$

$$
(\frac{f}{g})`=\frac{f^`g-fg`}{g^2} ,g \neq 0
$$

$$
\frac{df(g(x))}{dx} = \frac{df(u)}{du}\frac{dg(x)}{dx}=f^(u)*g^(x)
$$

激活函数导数

Sigmoid函数

Sigmoid函数容易出现梯度弥散现象,表达式为:
$$
\sigma(x)=\frac{1}{1+e^{-x}}
$$
求导过程:
$$
\begin{aligned} \frac{d}{d x} \sigma(x)& =\frac{d}{d x}\left(\frac{1}{1+e^{-x}}\right) \& =\frac{\mathrm{e}{-x}}{\left(1+\mathrm{e}{-x}\right)^{2}} \& =\frac{\left(1+\mathrm{e}{-x}\right)-1}{\left(1+\mathrm{e}{-x}\right)^{2}} \& =\frac{1+e{-x}}{\left(1+e{-x}\right){2}}-\left(\frac{1}{1+e{-x}}\right)^{2} \& =\sigma(x)-\sigma(x)^{2} \& =\sigma(1-\sigma) \end{aligned}
$$
这个非常重要:$\sigma^`(x)=\sigma(1-\sigma)$

image

1
2
3
4
5
6
import numpy as np
def sigmoi(x):
return 1 / (1 + np.exp(-x)) # 原函数形式

def derivative(x):
return sigmoid(x) * (1 - sigmoid(x)) # 导数形式

ReLU函数

解决了梯度弥散现象,表达式为
$$
ReLU(x) := max(0,x)
$$

$$
\frac{d}{d x} \operatorname{Re} L U=\left{\begin{array}{ll}{1} & {x \geq 0} \ {0} & {x<0}\end{array}\right.
$$

1
2
3
4
5
def derivative(x):
d = np.array(x, copy=True)
d[x < 0] = 0
d[x >= 0] = 1
return d

LeakyReLU 函数

表达式为
$$
\text {LeakyReLU}=\left{\begin{array}{cc}{x} & {x \geq 0} \ {p * x} & {x<0}\end{array}\right.
$$
导数为
$$
\frac{d}{d x} \text {LeakyReLU}=\left{\begin{array}{ll}{1} & {x \geq 0} \ {p} & {x<0}\end{array}\right.
$$
p一般是较小的值

1
2
3
4
def derivative(x, p):
dx = np.ones_like(x)
dx[x < 0] = p
return dx

Tanh 函数

表达式
$$
\begin{align}tanh(x) & = \frac{ex-e{-x}}{ex+e{-x}}\& = 2 * sigmoid(2x) - 1 \\end{align}
$$
导数为
$$
tanh(x) = 1-tanh^2(x)
$$

1
2
3
4
5
6
7
8
def sigmoid(x):
return 1 / (1+np.exp(-x))

def tanh(x):
return 2 * sigmoid(2*x) - 1

def derivative(x):
return 1-tanh(x) ** 2

损失函数梯度

均方误差函数梯度

MSE表达式为
$$
\mathcal{L}=\frac{1}{2} \sum_{k=1}{K}\left(y_{k}-o_{k}\right){2}
$$
偏导数为
$$
\frac{\partial \mathcal{L}}{\partial o_{i}}=\frac{1}{2} \sum_{k=1}^{K} \frac{\partial}{\partial o_{i}}\left(y_{k}-o_{k}\right)^{2}
$$
利用链式法则分解成
$$
\frac{\partial \mathcal{L}}{\partial o_{i}}=\frac{1}{2} \sum_{k=1}^{K} 2 *\left(y_{k}-o_{k}\right) * \frac{\partial\left(y_{k}-o_{k}\right)}{\partial o_{i}}
$$

$$
\frac{\partial \mathcal{L}}{\partial o_{i}}=\sum_{k=1}^{K}\left(y_{k}-o_{k}\right) *-1 * \frac{\partial o_{k}}{\partial o_{i}}
$$
当且仅当$k=i$时才是1,其他为0,上式可以表达成
$$
\frac{\partial \mathcal{L}}{\partial o_{i}}=(o_i-y_i)
$$

交叉熵函数梯度

在计算交叉熵损失函数时,一般将 Softmax 函数与交叉熵函数统一实现

Softmax函数梯度

表达式
$$
p_i=\frac{e{z_i}}{\sumK_{k=1}e^{z_k}}
$$

导数公式为
$$
\frac{\partial p_{i}}{\partial z_{j}}=\left{\begin{array}{ll}{p_{i}\left(1-p_{j}\right)} ;i=j \ {-p_{i} \cdot p_{j}} ;i \neq j \end{array}\right.
$$

交叉熵损失函数

表达式为
$$
\mathcal{L}=-\sum_{k} y_{k} \log \left(p_{k}\right)
$$
网络输出logits对变量$z_i$的导数为
$$
\frac{\partial \mathcal{L}}{\partial z_{i}}=-\sum_{k} y_{k} \frac{\partial \log \left(p_{k}\right)}{\partial z_{i}}
$$

$$
=-\sum_{k} y_{k} \frac{\partial \log \left(p_{k}\right)}{\partial p_{k}} \times \frac{\partial p_{k}}{\partial z_{i}}
$$

$$
=-\sum_{k} y_{k} \frac{1}{p_{k}} \times \frac{\partial p_{k}}{\partial z_{i}}
$$

其中$\frac{\partial p_{k}}{\partial z_{i}}$为Sotfmax函数的偏导数结果。分$k=i,k\neq i$讨论上面的式子,得到
$$
\frac{\partial \mathcal{L}}{\partial z_{i}}=p_{i}\left(y_{i}+\sum_{k \neq i} y_{k}\right)-y_{i}
$$

全连接层梯度

单个神经元梯度

采用的Sigmoid为激活函数
$$
o{1}=\sigma\left(w{1} x+b^{1}\right)
$$

上图解释

  • x是网络的输入,总共有J个节点
  • w是权值;
  • 其中输入第$j$个节点到输出$𝑜1$的权值连接记为$𝑤_{j1}1$。比如j1表示上一层的j号节点到当前层的1号节点
  • 通过权值和偏置变成了z
  • z通过激活函数变成了$\sigma$
  • 由于是单节点,$o1_1=o1=\sigma(z_1)$

损失函数为
$$
\mathcal{L}=\frac{1}{2}\left(o_{0}{1}-t\right){2}
$$
求偏导数$\frac {\partial \mathcal{L}}{\partial w_{j1}}$
$$
\frac{\partial \mathcal{L}}{\partial w_{j 1}}=\left(o_{1}-t\right) \frac{\partial o_{1}}{\partial w_{j 1}}
$$
将$o_1=\sigma(z_1)$分解,并且有$\sigma^{\prime}=\sigma(1-\sigma)$
$$
\frac{\partial \mathcal{L}}{\partial w_{j 1}}=\left(o_{1}-t\right) \frac{\partial \sigma\left(z_{1}\right)}{\partial w_{j 1}}
$$

$$
=\left(o_{1}-t\right) \sigma\left(z_{1}\right)\left(1-\sigma\left(z_{1}\right)\right) \frac{\partial z_{1}^{1}}{\partial w_{j 1}}
$$

其中$\sigma(z_1)=o_1$
$$
\frac{\partial \mathcal{L}}{\partial w_{j 1}}=\left(o_{1}-t\right) o_{1}\left(1-o_{1}\right) \frac{\partial z_{1}^{1}}{\partial w_{j 1}}
$$
其中$\frac{\partial z_{1}^{1}}{\partial w_{j 1}}=x_{j}
$可得:
$$
\frac{\partial \mathcal{L}}{\partial w_{j 1}}=\left(o_{1}-t\right) o_{1}\left(1-o_{1}\right) x_j
$$
总结:

误差对权值$w_{j1}$的偏导数只和输出值$o_1$真实$t$以及当前权值连接的输入$x_j$有关系


全连接层梯度

多输出的全连接层具有多个输出节点$o1_1,…,o1_K$,同时具有多个真实值标签$t_1,…,t_K$

MSE函数表示为

image

反向传播法

下图的相关介绍

  1. 输出层节点数为K,输出为$oK=[oK_1,…,o^K_K]$,
  2. 倒数第二层的节点数为J,输出为$oJ=[oJ_1,…,o^J_J]$
  3. 倒数第三层的节点数为I,输出为$oI=[oI_1,…,o^I_I]$

image

推导过程

image

image

每层的偏导公式为

  1. 倒数第一层

$$
\begin{array}{c}{\frac{\partial \mathcal{L}}{\partial w_{j k}}=\delta_{k}^{K} o_{j}} \ {\delta_{k}^{K}=o_{k}\left(1-o_{k}\right)\left(o_{k}-t_{k}\right)}\end{array}
$$

  1. 倒数第二层

$$
\begin{array}{c}{\frac{\partial \mathcal{L}}{\partial w_{i j}}=\delta_{j}^{J} o_{i}} \ {\delta_{j}^{J}=o_{j}\left(1-o_{j}\right) \sum_{k} \delta_{k}^{K} w_{j k}}\end{array}
$$

  1. 倒数第三层

$$
\begin{array}{c}{\frac{\partial \mathcal{L}}{\partial w_{n i}}=\delta_{i}^{I} o_{n}} \ {\delta_{i}^{I}=o_{i}\left(1-o_{i}\right) \sum_{j} \delta_{j}^{J} w_{i j}}\end{array}
$$

其中$o_n$为倒数第三层的输入,即倒数第四层的输出。规律:迭代每层的节点$\delta_{k}^{K},\quad \delta_{j}^{J}, \quad \delta_{i}^{I}, \quad \ldots$可以求出当前层的偏导数,从而得到权值矩阵W的梯度。

本文标题:TF-char7-反向传播法

发布时间:2020年05月12日 - 00:05

原始链接:http://www.renpeter.cn/2020/05/12/TF-char7-%E5%8F%8D%E5%90%91%E4%BC%A0%E6%92%AD%E6%B3%95.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea