深度学习 Deep Learning

机器学习技术间的包含关系:
深度学习⊆表示学习(即特征学习)⊆机器学习⊆人工智能

相比于(传统)机器学习,可以认为深度学习自动学习到特征,而(传统)机器学习中特征则由人工进行设计和提取。

multi-class (多分类中的)多类别任务。一个样本的类别有且仅有多个类别中的一个。
multi-label 多标签。一个样本的标签有多个标签中的若干个。

人工神经元

输入向量,经过线性变换后,经激活函数得出输出值的计算结构。
线性变换:

\[z=\bm w \cdot \bm x +b=\sum_i w_i x_i+b \]

激活函数一般是非线性函数。如sigmoid: \(a=\sigma(z)\)

Perceptron 感知机

激活函数是阈值函数(阈值门),单层神经网络。

多层感知机 MLP

将多个人工神经元(简称神经元)排列成网络结构,第一层称为输入层,最后一层称为输出层,剩下的中间所有层称为隐藏层,层与层间是全连接的。

多层感知机有时被认为是前馈神经网络,有时指严格的带阈值输出的感知机组合。

Neural Networks 神经网络

\(J\) : loss function.
\(L\) : nunber of layers.
\(\sigma\) : activation function(element-wise).
\(\sigma'\) : derivative of activation.
\(z^{(l)} := w^{(l)}a^{(l-1)}+b^{(l)}\)
\(a^{(l)} := \sigma(z^{(l)})\)
layer 1: the input layer. layer L: the output layer.
\(a^{(L)}\) , activation of layer \(L\) , is the output of the neural network.

assumptions:

  1. total loss of training dataset is a form of average loss over all individual training exmaples

\[J(X)=c\cdot \frac1N\sum_{x\in X} J(x) \]

where, \(c\) is a constant, \(N\) is the total number of training examples ( \(X\) ).
2. loss function \(J\) is a function of output from the neural network.

we get formulas in neural networks:

\[\begin{aligned} \delta^{(l)} :&= \nabla_{z^{(l)}} J= \nabla_{a^{(l)}}J \odot \sigma'( z^{(l)}) \\ &=\left([w^{(l+1)}]^T \delta^{(l+1)}\right) \odot \sigma'(z^{(l)}) \end{aligned} \\ \delta^{(L)} =\nabla_a J\odot \sigma'(z^{(L)}) \\ \nabla_{b^{(l)}} J = \frac{\partial J}{\partial b^{(l)}} = \delta^{(l)} \\ \nabla_{w^{(l)}} J=\frac{\partial J}{\partial w^{(l)}}=\delta^{(l)} [a^{(l-1)}]^T \]

we say \(\delta^{(l)}\) is error of layer \(l\) .
we use matrix operation to rewrite element-wise production.
let \(\Lambda(x)\) be the diagonal matrix(which is a square matrix) whose diagonal entries are the elements of vector \(x\) and whose off-diagonal entries are zeros. then $ y \odot x= \Lambda(x)y$ . so,

\[\begin{aligned} \delta^{(l)} &= [w^{(l+1)}]^T \delta^{(l+1)}\odot \sigma'(z^{(l)}) \\ &= \Lambda[\sigma'(z^{(l)})][w^{(l+1)}]^T\delta^{(l+1)} \\ &= \left(\Lambda[\sigma'(z^{l})][w^{(l+1)}]^T\right)...\left( \Lambda[\sigma'(z^{L-1})][w^{(L)}]^T \right)\left(\Lambda(\sigma'(z^{(L)}))\nabla_{a^{(L)}} J\right) \end{aligned} \]

Universal Approximation Theorem

(universality theorem) 神经网络的普遍逼近定理:神经网络可以逼近(计算)任意函数。

Vanishing Gradient Problem 梯度消失问题

Exploding Gradient Problem 梯度爆炸问题

Unstable Gradient Problem 梯度不稳定问题

神经网络结构中,所谓N层神经网络中的N指的是隐藏层加输出层的层数,不算输入层。

模型

Recurrent Neural Network (RNN) 循环神经网络

RNN的输入和输出数量关系有:一对一、一对多(输入也可以是空)、多对一、多对多(输入输出序列长度相同)、多对多(输入输出序列长度不同)。

语言模型中大多是基于词的,基于字符的很少,因为基于字符的模型比较深层,捕捉长远的依赖字词语义较难,对计算结构的算力要求高。

Gated Recurrent Unit (GRU) 门控循环单元

两种门,更新门和重置门。

Long-Short-Term Memory (LSTM) 长短期记忆

GRU要是两种门,LSTM三种门。

双向循环神经网络 Bidirectional RNN

深层网络模型会遇到梯度消失和梯度爆炸问题,梯度爆炸可以通过限制最高值,对值进行裁剪(clipping)缩放缓解。

卷积神经网络(Convolutional Neural Network, CNN)

卷积神经网络是指至少在网络中一层使用了卷积运算来替代一般矩阵乘法的神经网络。

The keys of CNNs are local connection, share weights, and the use of multiple layers.

卷积(convolution)是一种特殊的线性运算(operation)。

\[s(x)=(h*k)(x)=\int h(\tau)k(x-\tau)d\tau \]

\(k(·)\) 被称为核函数(kernel function)。

卷积运算可交换(commutative)

\[s(x)=(f * g)(x)=(g * f)(x) \]

核翻转(flip),得到互相关函数(cross-correlation), \(s(x)=\int h(x-\tau)k(\tau)d\tau\)

机器学习中常将卷积和互相关函数均称为卷积运算。

池化(pooling)利用某一位置相邻输出的总体统计特征来代替网络在该位置的输出,几乎所有卷积神经网络都会用到的操作。如,最大池化(max pooling)给出相邻区域内的最大值,对输入的少量平移,池化实现了输出的近似不变性(invariant)。

Batch Normalization

对激活前的值z(矩阵)进行规范化(均值规范化、方差规范化),然后进行缩放,再加上偏移。batch norm能加速训练,同时使得网络对权重的初始值不那么敏感。batch norm有略微的正则效果,因其对z加入一定噪音。对一个mini-batch,其batch norm过程(m为mini-batch-size):

\[ \mu=\frac1m\sum_i^m z^{(i)} \sigma^2=\frac1m\sum_i^m (z^{(i)}-\mu)^2 z_{norm}=\frac{z-\mu}{\sqrt{\sigma^2+\epsilon}} \tilde z=\gamma z_{norm} + \beta \]

测试时,因以一次一个样本进行,无小批次均值方差,可用EMA估测均值方差, \(\gamma,\beta\) 参数用训练集中的。

dropout(随机失活):将一定比例的神经元失活,使其失去对下层神经元的计算贡献。测试阶段一般会去掉dropout,因为不希望测试结果具有随机性。dropout有助于正则化,减少过拟合。

inverted dropout: 某层部分神经元失活后,会导致其期望值发生变化,将在后续引发一定程度的困难,这时需要将激活值(激活函数输出)除以dropout的保留概率,以维持无dropout时的期望。

Encoder-Decoder 编码器-解码器模型

一种从序列到序列的通用处理模型。对于输入序列S和目标序列T,模型的任务是接受输入S,将其通过编码器(Encoder)转为中间语义,表示为C,解码器根据中间语义C以及此前决策出的序列中的前序元素( \((\bm y_1,\bm y_2,...,\bm y_{i=1})\) )生成当前时刻i的元素 \(\bm y_i\)

\[\bf S=<\bm x_1,\bm x_2,...,\bm x_m> \\ \bf T=<\bm y_1,\bm y_2, ...,\bm y_n> \\ \bf C= {\mathcal F}(\bf S) \\ \bm y_i= {\mathcal G}(\bf C,\bm y_1,\bm y_2,...,\bm y_{i=1}) \]

\(\bm x_i\)\(\bm y_i\) 可以不同属一个空间,如语音转文字, \(\bm x_i\) 属于语音空间, \(\bm y_i\) 属于文字空间。

编码器-解码器模型可用于机器翻译、文本摘要(S是文章T是摘要)、问答系统、语音识别、图像处理等。对于机器翻译,encoder输入是源语言文本如中文,decoder输出是目标语言文本如英文;对于文本摘要,encoder输入是文本,decoder输出是一个更短的文本;对于语音识别,encoder输入是语音流,decoder输出是文本;对于图片文本描述,encoder是一张图片,decoder输出是文本。

自动编码器 Autoencoders

无监督学习技术,以学习特征表示。其结构是接受输入向量,经过隐层网络,输出与输入向量维度一致的向量,目标是使得输出和输入尽可能相似(用所谓重构误差度量)。(自动编码器/自编码器)

为避免学习到恒等函数,提出稀疏自动编码器、降噪自动编码器、栈式降噪自动编码器(Stacked Denoising Autoencoder)。

对于最简单的单隐层自动编码器(网络结构为3层:输入层、1个隐藏层、输出层),设输入输出维度为 \(n\) ,隐层维度 \(d\) ,则当隐层维度小于输入输出维度即 \(d<n\) 时自编码器被称为欠完备自编码器,当隐层维度大于输入输出维度即 \(d>n\) 时自动编码器被称为过完备自动编码器。欠完备自动编码器的隐层有类似降维的效果。

降噪自动编码器 Denoising Autoencoders

将样本经加噪处理后作为自动编码器的输入。
加噪处理可以为对样本向量的随机数个坐标的值置0。

变分自动编码器

本质上是一种生成模型。(传统自动编码器是一种无监督学习神经网络模型)

受限玻尔兹曼机 Restricted Boltzmann Machine

玻尔兹曼机 Boltzmann Machine (Hinton & Sejnowski, 1986),由可见单元(对应可见变量,即数据样本)和隐层单元(对应隐变量)构成,变量取值0或1,表示神经元处于抑制或激活状态。

由于Boltzmann Machine训练非常耗时,Sejnowski等人提出 Restricted Boltzman Machine,其去除了同层变量之间的所有连接,即使得同层变量之间变成相互独立的。

深度信念网络 Deep Belief Network

Deep Belief Network (Hinton, 2006),是一种由多层非线性变量连接组成的生成式模型。 其靠近可见层的部分是多个 Bayesian Belief Network,最远离可见层的部分则是Restricted Boltzmann Machine。

Deep Belief Network主要被应用于音乐推荐 (Xinxi Wang, 2014),因其建模一维数据比较有效。

注意力机制 Attention

迁移学习(Transfer Learning)

迁移学习在如下情况有意义:1.迁移源模型和迁移应用模型具有相同类型的输入(都是图像、都是文本、都是音频等);2.源模型数据集比应用模型数据集多很多。

Genrative Adversarial Network (GAN) 生成式对抗网络

Deep Reinforcement Learning

Word Embedding (Word2Vec)

无监督学习,学习词的向量表示。


预训练 pre-training:

微调 fine tuning:

端到端学习 end-to-end learning

end-to-end learning algorithm:
learning algorithm connects the input end(raw input) of the system directly to the output end.

In contrast, a non-end-to-end learning algorithm employs many components of algorithms as a pipeline to achieve the task objective.

区别一个算法是否是端到端学习算法,最重要的是这个算法是否直接根据样本进行整体学习,而不是分组件地各自独立学习或者甚至其中部分组件未涉及学习(人工工程)。


激活函数

sigmoid函数

sigmoid函数:指具有S型曲线的函数。常说的sigmoid一般指特例logistic sigmoid,值域为[0,1],函数式为

\[\sigma(z)=\frac{1}{1+e^{-z}}=\frac{e^z}{1+e^z} \]

logistic函数:一般地,等同于sigmoid函数。

sigmoid在值饱和时存在梯度接近0导致训练学习几乎停止的问题。

cons:
Vanishing gradients.
Not zero centred.

双曲正切函数(Hyperbolic Tangent)

tanh (pronounced tanch)

\[\tanh(z):=\frac{\sinh z}{\cosh z} \\ =\frac{e^z-e^{-z}}{e^z+e^{-z}} \]

tannh在饱和时也存在梯度近0导致学习几乎停止的问题。

cons:
Vanishing gradients.

在神经网络激活函数,用到sigmoid的地方可换用tanh也能达到不错效果,事实上

\[\tanh(z)= 2\sigma(2z)-1 \text{; i.e.} \\ \sigma(z)=\frac12 (\tanh(\frac{z}2)-1) \]

有时用tanh会更好,因比起用sigmoid,前者权重更新可以更灵活。从数学角度看,

\[\nabla_{\bm W^{(l)}} L = \bm\delta^{(l)} [\bm a^{(l-1)}]^T \]

如果激活函数a=sigmoid,而 \(\sigma(\cdot)\in (0, 1)\) ,则 \(a_i>0, \forall i\) ,对于矩阵 \(\nabla_{\bm W}L\) 的一个列向量 \([\nabla_{\bm W}L]_{*,k}=\delta_k \bm a\) ,其向量内所有元素的符号(正负形)相同,即是 \(\delta_k\) 的符号,也即向量内所有元素要么同同时增加(为正数)要么同时减少(为负数),这限制了神经网络权重变化的自由度。
如果激活函数a=tanh,而 \(\tanh(\cdot)\in [-1,+1]\) ,这不会出现sigmoid所导致的问题。

硬双曲正切(hard tanh)

\[g(a)=\max(-1, \min(1,a)) \]

归一化指数函数(softmax)

函数使得向量中的分量值归一化到[0,1]之间,并且分量值之和为1,同时抑制远低于最大值的其他分量,这也是被称为softmax的原因(平滑化的argmax函数)。

\[\bm\sigma(\bm z)_j=\frac{e^{\bm z_j}}{\sum_i e^{\bm z_i}} \]

Rectified Linear Unit (ReLU) 整流线性单元

\[\mathrm{relu}(z)=\max(0, z) \]

ReLU在输入z小于0时其导数(梯度)始终为0,存在梯度消失问题,故出现扩展的整流线性单元。所谓扩展,一般是扩展对负值部分的梯度的定义,所谓线性,即函数是线性的,其梯度是常量(可能是分段函数)。

cons:
Not zero centred.
Zero gradient for negative output.

扩展的整流线性单元

\[h_i=g(\bm z,\bm \alpha)_i=\max(0,z_i)+\alpha_i\min(0,z_i) \]

该扩展在z为负值时将z映射为 \(\alpha z\) ,即梯度为 \(\alpha\)
扩展整流线性单元在0处的梯度是未定义的,但在实践中(程序中)一般将其定义为附近点的梯度,这不影响最终的学习结果。

绝对值整流

\[g(z)=|z| \]

渗漏整流(Leaky ReLU):将 \(\alpha_i\) 固定为类似0.01的小值。

参数化整流(parametric ReLU, PReLU):将 \(\alpha\) 视为需要学习的参数。

maxout单元(maxout unit)

\(\bm z\) 划分为每组具有k个值的组,每个maxout单元输出每组中的最大值:

\[g(\bm z)_i=\max_{j\in\mathbb G^{(i)}} z_j \]

\(\mathbb G^{(i)}\) 是组 \(i\) 的索引集。

maxout单元可以学习具有多达k段的分段线性的凸函数。


Loss Functions 损失函数

均方误 Mean Square Error(MSE):

\[L=\frac1N \sum_{i}^N (\hat y_i - y_i)^2 \]

where,
\(N\) : number of training exmaples.
\(i\) : training exmaple i.
\(\hat y_i\) : predication value of exmaple i (model output).
\(y_i\) : ground true value of exmaple i.

(为了方便导数计算,一般会再除以2,即 \(L=\frac1{2N}\sum (\hat y - y)^2\)

MSE+激活函数sigmoid的模型在损失较大时梯度不会相应较大,(而我希望在初期即损失较大时期快速修正参数也就希望学习速度较快),从数学原理来看,损失对参数的梯度为(为简化问题,假设神经网络仅有1层,仅分析一个参数标量w):

\[\nabla_w L(x)=\frac{\partial \frac12(\hat y - y)^2}{\partial w} \\ (\hat y-y)\frac{\partial \hat y}{\partial w} \\ =(\hat y-y)\sigma'(z)\frac{\partial z}{\partial w} \\ =(\hat y-y)\sigma'(z)x \]

其中sigmoid的导数( \(\sigma'\) )会在0附近区间外变得平缓,因sigmoid函数在0附近区间外快速趋近饱和。

均方误(MSE):

\[\begin{aligned} \text{MSE}&=\frac1m\Sigma_i^m(\bm y^*-\bm y)_i^2 \\ &=\frac1m||\bm y^*-\bm y||^2 \end{aligned} \]

均方误是线回归中最常用的代价函数,均方误能解决大部分的线性回归问题。

均方误正则代价函数:

\[J(\bm w)=\text{MSE}+\lambda\bm w^T\bm w \]

负对数似然代价函数:

\[J(\bm\theta)=-\mathbb E_{\bm x,\bm y\sim \hat p_{data}}\log p_{model}(\bm y|\bm x) \]

交叉熵 Cross-Entropy:

主要用于分类任务。

  • For binary classification problems:

\[L=\frac1N \sum_i^N -[y_i \log p_i + (1-y_i)\log(1-p_i)] \]

where,
\(N\) : number of training exmaples.
\(i\) : training exmaple i.
\(\hat y_i \in \{0, 1\}\) : true label of example i.
\(p_i \in [0,1]\) : predication probability for exmaple i with label \(\hat y_i=1\) .

It's called also log-loss, binary cross entropy.

  • For multi-classification problems:

\[L=\frac1N\sum_i^N L_i \\ =\frac1N\sum_i^N \sum_k^K -[y(i,k)\log p(i,k) + (1-y(i,k))(1-\log p(i,k))] \]

where,
\(N\) : number of training exmaples.
\(K\) : number of classes.
\(p(i,k)\) : predication probability for exmaple i with class k.
\(y(i,k)\) : indicator function of ground truth for exmaple i with class k.

交叉熵损失+sigmoid的分类模型在损失较大时梯度较大,从数学原理看,损失对参数梯度为(为简化问题,假设神经网络仅有1层, \(p=\hat y=\sigma(z)\) ,仅分析一个参数标量w):

\[\begin{aligned} \nabla_w L(x) &= \frac{\partial}{\partial w} \left[-[y\log\sigma(z)+(1-y)\log(1-\sigma(z))]\right] \\ &= \left[-\frac{y}{\sigma(z)}+\frac{(1-y)}{1-\sigma(z)}\right]\frac{\partial \sigma(z)}{\partial w} \\ &= \left[\frac{(1-y)}{1-\sigma(z)}-\frac{y}{\sigma(z)}\right]\sigma(z)[1-\sigma(z)]x \\ &= [\sigma(z)-y]x \\ &= (\hat y-y)x \end{aligned} \]

梯度其在损失较大时值较大也就起到快速更新参数的作用。

当输出层的激活函数是sigmoid时,模型损失函数几乎都会选择交叉熵(相对于均方误)。
当输出层激活函数是线性的时,损失函数考虑MSE。

多分类:可用softmax作为出输出层激活函数。

数据集中的错误标注数据:如果比例非常小、错误是随机性的,可以忽略。深度学习模型对随机错误不敏感(标注错误是很随机的),但对系统错误敏感(某种规律性的标注错误)。若想通过分析错误标注数据以改善模型,可人工核查开发集测试集数据,分析错误,以决定修正错误的价值大小;确保训练和开发集服从相似分布。


Regularization 正则化

(防止过拟合)

Weights Decay

(weights penalty)

为什么权重衰减能防止过拟合?
过拟合网络的权重一般较大:
在过拟合过程中,函数拟合尽可能多的数据点,会对自变量(输入)比较敏感,输出波动剧烈,而自变量是随机的,为了适应任意自变量输入,就需要函数权重参数(绝对值)值是很大的。

参数权重的值是比较大的,加入权重惩罚项以减小权重的值。

L2 penalty, L1 penalty.

L2 Penalty

\[J(\bm \theta)=L(\bm \theta)+\frac{\lambda}2\|\bm\theta \|_2 \]

L2-norm, the second term on the right side of equation is the regularization term.

L2 penalty penalizes the squared weights. Result:

  • Keeps weights small weights unless error derivative is very large.
  • Prevent from fitting sampling error.
  • Smoother model (output changes slower as the input changes)
  • If network has two similar inputs, it prefers to put half the weight on each rather than all the weight on one.

L1 Penalty

\[J(\bm\theta)=L(\bm\theta)+\lambda \|\bm\theta\|_1 \\ =L(\bm\theta)+\lambda \sum_i |\theta_i| \]

L1-norm, the second term on the right side of equation is the regularization term.

L1 penalty penalizes the absolute weights. Result:

  • Allow for a few weights to remain large.

Early Stoppage

Early stoppage(early stopping): stop training when performance on validation set decreases.

Dropout

randomly drop out some inputs (output of previous layer).


Normalization

Network Input Normalization

normalize the inputs of model to [0,1] or [-1, +1], or to the mean and variance.

Batch Normalization

Normalize the hidden layer inputs to mini-batch mean and variance.

Batch Renormalization

Layer Normalization

for RNNs.

Instance Normalization

for Style Transfer.

Group Normalization

for CNNs.


优化方法 Optimizing Algorithms

(for unconstrained optimization)

  • GD(BGD)(每次权重更新是基于整个训练集)
  • SGD(每次权重更新是基于1个样本),
  • mini-batch GD(MBGD)(每次权重更新是基于小批次样本集如几十个上百个样本)
  • Hessian Optimization (二阶导数优化)
  • Momentum
  • Adagrad(Adaptive Gradient Algorithm)
  • Nesterov Accelerated Gradient(NAG)
  • Adadelta
  • RMSprop (root mean squre propagation)
  • Adam(Adaptive Moment Estimation)
  • Adamax
  • Nadam
  • Conjugate gradient 共轭梯度法
  • Newton's Method、阻尼牛顿法
  • 拟牛顿法
  • DFP
  • BFGS (BFGS是4个人名的首字母)
  • L-BFGS (Limited-memory BFGS)

gradient descent optimization algorithms

Gradient Descent

(also called batch gradient descent)

gradient descent学习的公式说明:

denote parameters as vector \(\bm \theta\) , loss function as

\[L=f(\bm \theta) \]

. Calculus tells us,

\[\Delta L \approx \sum_i \frac{\partial L}{\theta_i}\Delta \theta_i \\ =\frac{\partial L}{\partial \bm \theta} \cdot \Delta\bm\theta \]

denote a vector as

\[\nabla_\theta L := \left( \frac{\partial L}{\partial \theta_1}, \frac{\partial L}{\partial \theta_2},... \right)^T \]

(we say it gradient vector)

then \(\Delta L\) can be rewitten as

\[\Delta L \approx \nabla_\theta L \cdot \Delta \bm\theta \]

to minimize the loss function \(L\) , we should make \(\Delta L\) to be negative. In the above equation, what we can change is \(\Delta \bm\theta\) (cos it's the parameters), so let us make \(\Delta \bm \theta\) to be , for example, \(\Delta\bm\theta = -\eta \nabla_\theta L\) , where \(\eta > 0\) , and then we get \(\Delta L= -\eta \nabla L^2 >=0\) , it is always negtative( non-postive).

Stochastic Gradient Descent (SGD) 随机梯度下降

  1. 打乱数据集
  2. 对每个样本,计算损失函数,随机选择一个特征维度利用梯度下降法更新参量(每次迭代中用到1个样本)。
    其中随机之意是指一个随机训练样本,而相比于整体样本的梯度下降算法,相当于在一个相对随机的梯度方向上变化。

不过当前说的SGD一般指 mini-batch SGD。

mini-batch Gradient Descent

小批次梯度下降(mini-batch gradient descent, MBGD)方法的批大小选择问题,需要考虑cpu/gpu内存大小、算力等,一般选取 \(2^4\to 2^9\) ,即16,32,...,512。

如果数据集不大(在数千、数万)可直接用全量梯度下降(batch gradient descent),如果数据集很大,可考虑小批次梯度下降或随机梯度下降。

Large mini-batch sizes
will provide accurate estimates of the gradient, reducing the variance in the parameter update. Furthermore, large mini-batches take advantage of highly optimized matrix operations in vectorized implementations of the cost and gradient. The reduction in variance leads to more stable convergence, but each gradient calculation will be more expensive.
In contrast, small mini-batches are quick to estimate. If we keep the
mini-batch size small, the noise in our gradient estimate will allow us to get out of some bad local optima, which we may otherwise get stuck in. In machine learning, optimization methods are used for training by minimizing an objective function on the training data, but the overall goal is to improve generalization performance. Since the goal in machine learning does not necessarily need a precise estimate of the minimum of the objective function, approximate gradients using mini-batch approaches have been widely used.

GD vs. SGD vs. mini-batch GD:
GD(Batch Gradient Descent):
每次权重更新时计算梯度都需要用到所有样本。
SGD:计算梯度时用到1个随机样本。
Mini-batch Gradient Descent:
每次用到一小批样本(数量被称为mini-batch size)。遍历数据集中的所有小批次,对于每个小批次,计算每个样本梯度,利用梯度下降法将小批次梯度均值作为梯度更新值。

Hessian Optimization

By Taylor's theorem (Taylor series expansion),

\[L(w+\Delta w)=L(w)+\sum_i \frac{\partial L}{\partial w_i}\Delta w_i + \frac12 \sum_{j,k} \Delta w_j \frac{\partial^2 L}{\partial w_j\partial w_k}\Delta w_k... \\ =L(w)+(\nabla L)^T\Delta w + \frac12 \Delta w^T H \Delta w + ... \]

where \(\nabla L\) is the gradient vector, \(H\) is the Hessian matrix whose entry \(H_{jk}=\frac{\partial^2 L}{\partial w_j \partial w_k}\) .

approximate L by discarding higher-order derivative term, obtaining:

\[L(w+\Delta w)\approx L(w)+(\nabla L)^T\Delta w + \frac12 \Delta w^T H \Delta w \]

optimizing objective function by choosing

\[\Delta w= -\eta H^{-1}\nabla L \]

where \(\eta\) is known as learning rate.

Momentum-based gradient descent

introducing parameter velocity.

update rule:

\[v^{(t+1)}=\mu v^{(t)} - \eta \nabla_\theta J \\ \theta ^{(t+1)}=\theta^{(t)} + v^{(t+1)} \]

In other words:

\[\Delta\theta^{(t+1)}=\mu \Delta\theta^{(t)} - \eta\nabla_\theta J(\theta^{(t)}) \\ \theta^{(t+1)}=\theta ^{(t)}+\Delta\theta^{(t+1)} \]

where \(\nabla_\theta J(\theta^{(t)})\) denotes the gradient of loss function \(J\) with respect to \(\theta\) , evaluated at \(\theta^{(t)}\) .

hyper-parameter \(\mu \in [0,1]\) controls the amount of friction in the system. actually \(1-\mu\) is the friction. when \(\mu=0\) , there is a lot of friction, the velocity can't build up, equations in update rule reduce to the usual equations for gradient descent. when \(\mu=1\) , there is no friction, and the velocity is completely driven by the gradient \(\nabla J\) .
( \(\mu\) , so called momentum co-efficient, a badly choosen name, is not at all the same as the notion of momentum from physics)
Notice that \(v\) is also a form of EMA(exponential moving average) founded on \(\nabla J\) .

momentum: 相对于普通梯度下降,在每次迭代中更新参数时,momentum方式利用梯度的指数平滑平均值(EMA)作为更新数据。momentum能够减少梯度下降过程中即参数在往最优值移动过程中,参数在某个/某些不太相关的特征维度方向上的摆动,使其尽量靠近最佳移动方向。

momentum的更新策略代码:

v_dw=0, v_db=0
for t = 1 : max_iterations
    dw=...      % 损失函数关于参数W的梯度
    db=...      % 损失函数关于b的梯度
    
    v_dw = beta * dw  + (1 - beta)* v_dw     % dw的动量(指数平滑平均)
    v_db = beta * db  + (1 - beta)* v_db
    
    w = w - alpha * v_dw
    b = b - alpha * v_db
    
end

AdaGrad

(adaptive gradient) learning rate decay

\[\bm h \leftarrow \bm h + \nabla_\theta J \odot \nabla_\theta J \\ \theta \leftarrow \theta - \frac{\eta}{\sqrt{\bm h}} \nabla_\theta J \]

AdaGrad会记录历史梯度平方和,学习越久,更新变化幅度越小,直至没有变化。

RMSprop

RMSprop(root mean square propagation):能够减少参数在优化过程中的摆动,使参数变化路径尽量靠近最佳优化路径。

算法代码:

s_dw=0, s_db=0
for t = 1 : max_iterations
    dw=...      % 损失函数关于参数W的梯度
    db=...      % 损失函数关于b的梯度
    
    s_dw = beta * (dw.^2)  + (1 - beta)* s_dw     % dw的平方(square)
    s_db = beta * (db.^2)  + (1 - beta)* s_db       
    
    w = w - alpha * dw ./ sqrt(s_dw)            %除以平滑平均(mean)的平方根(root)。为避免除以0造成的数值问题,除数可加个小值
    b = b - alpha * db ./ sqrt(s_db)
    
end

Adam

Adam, short for adaptive momentum.

Adam优化算法:结合momentum和RMSprop。

算法代码:

v_dw=0, v_db=0
s_dw=0, s_db=0
epsilon=1e-9        % 小值常量
for t = 1 : max_iterations
    dw=...      % 损失函数关于参数W的梯度
    db=...      % 损失函数关于b的梯度
    
    v_dw = beta1 * dw  + (1 - beta1)* v_dw     % dw的动量(指数平滑平均)
    v_db = beta1 * db  + (1 - beta1)* v_db
    
    s_dw = beta2 * (dw.^2)  + (1 - beta2)* s_dw     % dw的平方(square)
    s_db = beta2 * (db.^2)  + (1 - beta2)* s_db       
    
    %偏差修正  bias correction
    v_dw_c = v_dw / (1-beta1 ^t);  v_db_c = v_db / (1-beta1 ^t);
    s_dw_c = s_dw / (1-beta2^t);   s_dw_c = s_dw / (1-beta2^t)
    
    w = w - alpha * v_dw_c ./ sqrt(s_dw_c + epsilon)            %除以平滑平均(mean)的平方根(root)。为避免除以0造成的数值问题,除数可加个小值
    b = b - alpha * v_db_c ./ sqrt(s_db_c + epsilon)
    
end

Adamax

Paper Kingma & Ba (2014)

Adamax is a variant of the Adam which uses the infinite norm, while the Adam uses the L2-norm, for optimization.

Infinite norm is influenced less by smaller components in the gradient vector, so it makes the algorithm more robust to noise in the gradients.

Nadam

Nesterov-accelerated Adaptive Momentum Estimation, combines Adam and NAG.

\[ \]

Newton's Method

\[\Delta x = -[\nabla^2 f(x)]^{-1} \nabla f(x) \]

Searching stopping if \(\nabla f(x)^T\nabla^2 f(x)\nabla f(x) < \epsilon\) .


Initialization for parameters 初始化

Xavier initialization for linear-like activations

(paper by Xavier Glorot etc.)

适用于激活函数是线性或近似线性的(如sigmoid, tanh)

Initilization for ReLU

(paper by Kaiming He)


权重W和偏置b一般不合并为一个向量,这样编程实现更简单,这里的b称为 interceptor

在构建数据集输入矩阵时,一般将单个样本以列向量排列,对于n个样本数据集形成一个 \(d\times n\) 的数据矩阵。


数据集

training/devlopment/test dataset:
将数据集划分为三部分:训练集(train set)、开发集(dev set)、测试集(test set)。划分数据集时需注意划分后三部分数据集的数据分布,应该具有相似的分布。开发集(hold-out cross validation dataset)是在训练过程中用作交叉验证的。训练集在三部分中应占最高比例,数据集比较充足时(如百万样本),训练集可以占比到98+%,99+%,开发集可以比测试集稍多一点。

开发集和测试集的样本分布应该保持一致,并且应当能反映模型未来应用的实际数据分布。这样有利于我们分布一个模型在标注数据上的训练结果。(如果我们需要一个模型从训练开发集到测试集的泛化能力,则样本分布应该有差别)

在致力于提升模型性能表现时,开发集、测试集的样本数量应当达到能够分辨提升率的数量。如1000样本对于分辨0.1%提升率来说是不够的。

驱使深度学习发展的因素:

  1. 数据。网络催生大规模数据,算法可使用的数据、训练集更多。
  2. 计算机算力。允许更复杂的计算结构。
  3. 算法。有很多算法出现,并且简单的算法也能启不错的作用。

错误分析

分析开发集错误样例。

bias: avoidalbe error(gap between training error and optimal error.).
variance: gap between training error and test error.

bayes optimal error 贝叶斯最优误差:
optimal error: (Bayes Error) 最优误差,指最理想效果情况下仍存在的不可避免误差(unavoidable bias)。

Reducing high avoidable bias:

  • Increase the model size. (if it increases variance, then use regularization)
  • Modify input features.
  • Reduce or eliminate regularization. (this will reduce avoidable bias but increase variance)
  • Modify model architecture.

One method which is NOT helpful for reducing bias:

  • Add more training data. More training data helps to reduce variance but usually has no significant effect on bias.

Reducing variance:

  • More training data.
  • Regularization.
  • Early stopping.
  • Less input features.
  • Decrease the model size.
  • Modify modle architecture.

学习曲线 Learning curves: 反映错误率在学习过程中变化情况的曲线,以帮助分析bias和variance。

分析错误随训练集样本数量增加而发生的变化曲线。

对于偏斜类别的数据集,可取采样出一个均衡的子数据集来训练,以画出学习曲线来分析错误。

效果差的原因存在于目标函数还是搜索算法?利用已知的最优样本(可认为人工样本为最优样本)与一般样本来评估目标函数输出值是否符合预期的大小序,最优样本输出值应比一般样本输出值更大(对于目标函数是argmax情况)或更小(对于目标函数是argmin情况)。

posted @ 2022-07-06 23:48  二球悬铃木  阅读(156)  评论(0编辑  收藏  举报