首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >TF-char7-反向传播法

TF-char7-反向传播法

作者头像
皮大大
发布2021-03-01 11:17:41
发布2021-03-01 11:17:41
4020
举报

TF-char7-反向传播法

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

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

导数和梯度

导数

,定义为:

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}

导数表征了函数在某个方向上的变化率。

,对于每个参数的偏导数为:

\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}
代码语言:javascript
复制
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.
代码语言:javascript
复制
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.
代码语言:javascript
复制
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)
代码语言:javascript
复制
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}}

当且仅当时才是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对变量的导数为

\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 \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_{j 1}}=\left(o_{1}-t\right) \frac{\partial o_{1}}{\partial w_{j 1}}

\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}}
\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 \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函数表示为

反向传播法

下图的相关介绍

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

推导过程

每层的偏导公式为

  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的梯度。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-5-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • TF-char7-反向传播法
  • 导数和梯度
    • 导数
    • 梯度
    • 重要导数
    • 导数形式
  • 激活函数导数
    • Sigmoid函数
    • ReLU函数
    • LeakyReLU 函数
    • Tanh 函数
  • 损失函数梯度
    • 均方误差函数梯度
    • 交叉熵函数梯度
      • Softmax函数梯度
      • 交叉熵损失函数
  • 全连接层梯度
    • 单个神经元梯度
    • 全连接层梯度
  • 反向传播法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档