Task 03- 模型泛化-数值稳定性-RNN进阶
1 模型泛化
-
数据集划分:训练集(/验证集)/测试集
-
泛化性能(狭义):通过训练集训练得到的模型,在测试集上的性能
- 训练误差:模型在训练集上表现出的误差
- 泛化误差:模型在任意⼀个测试数据样本上表现出的误差期望,常通过测试集上的误差来近似
-
模型选择:从训练集中预留验证集进行参数优化与模型选择
- K K K-折交叉验证( K K K- f o l d fold fold)
-
留一法交叉验证( L e a v e Leave Leave- O n e One One- O u t Out Out):样本容量为 N N N,每次留出 1 1 1个样本作为验证集,剩余 ( N − 1 ) (N-1) (N−1)个样本作为训练集,则共开发 N N N个模型,最终泛化误差为各个样本泛化误差均值
-
模型复杂度&过拟合-欠拟合
-
欠拟合改善方法:特征工程、增加模型复杂度、增强拟合能力
-
过拟合改善方法:
-
增大数据集:收集数据、数据增广
-
权重衰减( w e i g h t weight weight d e c a y decay decay): L 2 L2 L2正则化,惩罚绝对值较⼤的模型参数
l = l ( w 1 , w 2 , b ) + λ 2 n ∥ w ∥ 2 l = l(w_1,w_2,b)+\frac{\lambda}{2n}{\left\| w \right\|}^2 l=l(w1,w2,b)+2nλ∥w∥2 -
D r o p o u t Dropout Dropout:在一个训练轮次中,随机将一个神经网络层中的若干个神经元无效化(不更新参数),从而使得模型不依赖于特定的神经元
-
-
-
计算图
2 数值稳定性
2.1 梯度稳定性
根据反向传播机制,梯度根据链式法则由后向前递乘,容易出现梯度过小(gradient vanishing)与梯度过大的现象(gradient exploding)
2.2 参数初始化
- Xavier随机初始化:全连接层输入维度为 a a a,输出维度为 b b b,则每个权重元素的初始值随机采样于均匀分布 U ( − 6 a + b , 6 a + b ) . U(-\sqrt{\frac{6}{a+b}},\sqrt{\frac{6}{a+b}}). U(−a+b6,a+b6).
- He初始化:(适用于ReLU激活函数),每个权重元素采样于正态分布 N ( 0 , 2 a ) N(0,\sqrt\frac{2}{a}) N(0,a2)
2.3 偏移
-
协变量偏移( c o v a r i a t e covariate covariate s h i f t shift shift):训练/测试集的输入特征分布差异较大
-
标签偏移( l a b e l label label s h i f t shift shift):训练/测试集的标签分布差异较大
-
概念偏移:标签的定义发生变化?
3 RNN进阶
3.1 通过时间反向传播( B P T T , BPTT, BPTT, B a c k Back Back P r o p a g a t i o n Propagation Propagation T h r o u g h Through Through T i m e Time Time)
-
计算图(无偏置项+线性激活)
-
运算
- 模型定义(定义前向传播)
h t = W x h x t + W h h h t − 1 o t = W q h h t L = 1 T ∑ t = 1 T l ( o t , y t ) \bold{h}_{t} = \bold W_{xh}\bold{x}_{t} + \bold W_{hh}\bold{h}_{t-1} \\ \bold{o}_t=\bold{W}_{qh}\bold{h}_t \\ L=\frac{1}{T}\sum_{t=1}^{T}l(\bold{o}_t,\bold{y}_t) ht=Wxhxt+Whhht−1ot=WqhhtL=T1t=1∑Tl(ot,yt)
-
反向传播
∂ L ∂ W q h = 1 T ∑ t = 1 T [ ∂ l ( o t , y t ) ∂ o t ∂ o t ∂ W q h ] = 1 T ∑ t = 1 T ∂ l ( o t , y t ) ∂ o t h t ⊤ ∂ L ∂ W x h = ∂ L ∂ o t ∂ o t ∂ h t ∂ h t ∂ W x h = 1 T ∑ t = 1 T ∂ l ( o t , y t ) ∂ o t W q h x t ⊤ ∂ L ∂ W h h = ∂ L ∂ o t ∂ o t ∂ h t ∂ h t ∂ W h h = 1 T ∑ t = 1 T ∂ l ( o t , y t ) ∂ o t W h h h t − 1 ⊤ \frac{\partial L}{\partial W_{qh}}=\frac{1}{T}\sum_{t=1}^{T} [\frac{\partial l(\bold {o}_t,\bold{y}_t)}{\partial \bold {o}_t} \frac{\partial\bold{o}_t}{\partial W_{qh}}]=\frac{1}{T}\sum_{t=1}^{T}\frac{\partial l(\bold {o}_t,\bold{y}_t)}{\partial \bold {o}_t}\bold{h}_t^{\top} \\ \frac{\partial L}{\partial W_{xh}} = \frac{\partial L}{\partial \bold{o}_t} \frac{\partial \bold{o}_t}{\partial \bold{h}_t} \frac{\partial \bold{h}_t}{\partial W_{xh}} = \frac{1}{T}\sum_{t=1}^{T} \frac{\partial l(\bold {o}_t,\bold{y}_t)}{\partial \bold {o}_t} W_{qh}x_t^{\top} \\ \frac{\partial L}{\partial W_{hh}} = \frac{\partial L}{\partial \bold{o}_t} \frac{\partial \bold{o}_t}{\partial \bold{h}_t} \frac{\partial \bold{h}_t}{\partial W_{hh}} = \frac{1}{T}\sum_{t=1}^{T} \frac{\partial l(\bold {o}_t,\bold{y}_t)}{\partial \bold {o}_t} W_{hh}h_{t-1}^{\top} ∂Wqh∂L=T1t=1∑T[∂ot∂l(ot,yt)∂Wqh∂ot]=T1t=1∑T∂ot∂l(ot,yt)ht⊤∂Wxh∂L=∂ot∂L∂ht∂ot∂Wxh∂ht=T1t=1∑T∂ot∂l(ot,yt)Wqhxt⊤∂Whh∂L=∂ot∂L∂ht∂ot∂Whh∂ht=T1t=1∑T∂ot∂l(ot,yt)Whhht−1⊤ -
机理
与MLP的计算图不同,MLP的反向传播可以高度并行化,如均方误差损失+线性激活的Perceptron,其 ∂ L ∂ w a , b = ∣ o b − y b ∣ x a \frac{\partial L}{\partial w_{a,b}}=|o_b-y_b|x_a ∂wa,b∂L=∣ob−yb∣xa只依赖于输入 x a x_a xa而不依赖于其他状态,而RNN的计算图由于存在序列上的依赖关系,所以反向传播时,需要依赖于若干时间步长之前的状态来对梯度进行计算(见 ∂ L ∂ W h h \frac{\partial L}{\partial W_{hh}} ∂Whh∂L)
3.2 GRU
-
门控循环单元,Gated Recurrent Unit
-
门控:重置门(reset gate)和更新门(update gate)
-
计算图
3.3 LSTM
- 长短期记忆单元,Long Short Term Memory
- 门控:输入门(input gate),遗忘门(forget gate),输出门(output gate)
- 计算图
3.4 deep & bi-directional
- 深层RNN
- 双向RNN:隐藏状态的传递方向包含了前向后及后向前