Feedforward NN

每一层的神经元仅接受前一层神经元的输出, 并输入到下一层神经元, 神经元间的连接不会形成环. 有种 Markov chain 的味道.

  • \(L\): 层数;
  • \(m^{(l)}\): 第 \(l\) 层神经元的个数;
  • \(f_l\): 第 \(l\) 层 activation function;
  • \(W^{(l)}\): 第 \(l-1\) 层到第 \(l\) 层 weight matrix;
  • \(b^{(l)}\): 第 \(l\) 层 bias;
  • \(z^{(l)}\): 第 \(l\) 层净输入;
  • \(a^{(l)}\): 第 \(l\) 层输出.

\[\begin{align*} z^{(l)} &= W^{(l)}a^{(l-1)} + b^{(l)},\\ a^{(l)} &= f_l(z^{(l)}). \end{align*} \]

Backpropagation Algorithm

若采用 cross-entropy 损失函数, 则

\[L(y, \hat y) = - y'\log \hat y, \]

其中 \(y\) 为 one-hot 向量表示, \(\hat y = a^{(L)}\).

其中对 \(f\colon \mathbb R \to \mathbb R\), \(f((x_1,\dots,x_n)') := (f(x_1),\dots,f(x_n))'\). Matrix calculus 采用 denominator-layout notation.

给定训练集 \(\{(x^{(n)}, y^{(n)})\}_{n=1}^N\), 考虑对 \(W\)\(L^2\) 正则化的结构化风险函数

\[R(W,b) = \frac1N\sum_{n=1}^N L(y^{(n)}, \hat y^{(n)}) + \frac12\lambda \Vert W\Vert_F^2. \]

Remark: 不对 bias 做正则化的原因.

Overfitting usually requires the output of the model to be sensitive to small changes in the input data (i.e. to exactly interpolate the target values, you tend to need a lot of curvature in the fitted function). The bias parameters don't contribute to the curvature of the model, so there is usually little point in regularising them as well. [2]

顺便一提 ridge regression 不对 bias (intercept) 做正则化的原因是, 这样做的结果会依赖于 \(y\) 的原点的选取.

我们要通过梯度下降法更新参数, 需要计算 \(\frac{\partial L}{\partial W^{(l)}}\)\(\frac{\partial L}{\partial b^{(l)}}\).

记第 \(l\) 层神经元的误差项

\[\begin{align*} \delta^{(l)} &= \frac{\partial L}{\partial z^{(l)}}\\ &= \frac{\partial a^{(l)}}{\partial z^{(l)}}\frac{\partial z^{(l+1)}}{\partial a^{(l)}}\frac{\partial L}{\partial z^{(l+1)}}\\ &= \mathrm{diag}(f'_l(z^{(l)})) (W^{(l+1)})' \delta^{(l+1)}\\ &= f'_l(z^{(l)})\circ \left[(W^{(l+1)})' \delta^{(l+1)}\right], \end{align*} \]

这就是误差的反向传播, 其中 \(\circ\) 表示 Hadamard product. 则

\[\begin{align*} \frac{\partial L}{\partial w_{ij}^{(l)}} &= \frac{\partial z^{(l)}}{\partial w_{ij}^{(l)}}\frac{\partial L}{\partial z^{(l)}}\\ &= \mathbb I_i(a_j^{(l-1)})\delta^{(l)}\\ &= \delta_i^{(l)}a_j^{(l-1)}, \end{align*} \]

其中 \(\mathbb I_i(x)\) 表示第 \(i\) 个元素为 \(x\), 其余为 \(0\) 的行向量, 由此可得

\[\frac{\partial L}{\partial W^{(l)}} = \delta^{(l)}(a^{(l-1)})'. \]

另一方面,

\[\begin{align*} \frac{\partial L}{\partial b^{(l)}} &= \frac{\partial z^{(l)}}{\partial b^{(l)}}\frac{\partial L}{\partial z^{(l)}}\\ &= I_{m^{(l)}}\delta^{(l)} = \delta^{(l)}. \end{align*} \]

BP 算法即为,

  1. 前馈计算 \(a\);
  2. 反向传播计算 \(\delta\);
  3. 用梯度下降法更新参数.

Remark: 初始化参数时, 若令 \(W = 0\), \(b = 0\), 则训练出来的网络同一层中各个参数都相同, 所以需要随机参数初始化.

Vanishing Gradient Problem

注意到误差项 \(\delta\) 中包含激活函数的导数, 当导数接近 0 时, 误差经过每层传递都会不断衰减, 当网络层数很深时, 梯度就几乎消失了, 导致网络很难训练.

一个简单有效的缓解办法是使用导数比较大的激活函数, 比如 ReLU.

ReLU 的缺点是, 训练中容易导致神经元死亡, 称为 dying ReLU problem. 在训练时, 如果参数在一次不恰当的更新后, 第一个隐藏层中的某个 ReLU 神经元在所有的训练数据上都不能被激活, 那么这个神经元自身参数的梯度永远都会是 0, 在以后的训练过程中永远不能被激活. 为了避免这个问题, 可以使用 leaky ReLU, PReLU 等变种.

References

[1] 邱锡鹏. (2019). 神经网络与深度学习. https://nndl.github.io/
[2] Dikran Marsupial. (2018). No regularisation term for bias unit in neural network. https://stats.stackexchange.com/questions/153605/no-regularisation-term-for-bias-unit-in-neural-network

posted @ 2019-07-26 16:12  resolvent  阅读(447)  评论(0编辑  收藏  举报