Mini-batch
-
[i] {j} (k)上标分别表示 第i层,第j小块,第k个示例
-
优点:
- 能够进行向量化。
- 不用等整个数据集都计算后再梯度下降,单次迭代耗时短。
指数加权平均
- V t = β V t − 1 + ( 1 − β ) θ t V_t=\beta V_{t-1}+(1-\beta)\theta_t Vt=βVt−1+(1−β)θt (平均过去 1 1 − β \frac{1}{1-\beta} 1−β1天的数据)
- β \beta β越大,曲线越平稳,也会稍微右移。
- 优点:占用极少内存,只用一行代码,不断更新 V V V值。
- 偏差修正:
V
t
c
o
r
r
e
c
t
e
d
=
V
t
1
−
β
t
V_t^{corrected}=\frac{V_t}{1-\beta^t}
Vtcorrected=1−βtVt
早期需要好的估测来修正,随着 t t t的增大, β t \beta^t βt逐渐趋于 0 0 0,修正也不再起作用。
Momentum
(Gradient descent with momentum)动量梯度下降法
- 基本思路:计算梯度的指数加权平均值,更新权重,加速梯度下降。
上下摆动正负抵消,摆动变小,横轴平均值还很大,因此运动更快。 -
V
d
θ
=
β
V
d
θ
=
+
(
1
−
β
)
d
θ
V_{d\theta}=\beta V_{d\theta}=+(1-\beta)d\theta
Vdθ=βVdθ=+(1−β)dθ
θ = θ − α V d θ \theta=\theta-\alpha V_{d\theta} θ=θ−αVdθ
β = 0.9 \beta=0.9 β=0.9 (一般固定,平均前十次迭代)
β \beta β越大,上下摆动越小。 - 不用偏差修正,10次迭代后已经过了初始阶段,基本无偏差。
- 想象成小球从坡上往下滚。小球的动量越来越大,过最低点后仍然会往前冲。
RMSprop
(root mean square propagation) 均方根反向传播
-
S d θ = β S d θ = + ( 1 − β ) d 2 θ S_{d\theta}=\beta S_{d\theta}=+(1-\beta)d^2\theta Sdθ=βSdθ=+(1−β)d2θ
θ = θ − α d θ S d θ + ϵ \theta=\theta-\alpha\frac{d\theta}{\sqrt{S_{d\theta}}+\epsilon} θ=θ−αSdθ+ϵdθ
ϵ = 1 0 − 8 \epsilon=10^{-8} ϵ=10−8 (使稳定,不为0)
-
纵轴斜率大,因此除以一个大的数,会减缓纵轴摆动;
横轴斜率小,因此除以一个小的数,会加速梯度下降。
Adam 优化算法
Adaptive Moment Estimation 自适应矩估计
-
结合Momentum and RMSprop
-
V d θ = β 1 V d θ = + ( 1 − β 1 ) d θ V_{d\theta}=\beta_1 V_{d\theta}=+(1-\beta_1)d\theta Vdθ=β1Vdθ=+(1−β1)dθ
S d θ = β 2 S d θ = + ( 1 − β 2 ) d 2 θ S_{d\theta}=\beta_2 S_{d\theta}=+(1-\beta_2)d^2\theta Sdθ=β2Sdθ=+(1−β2)d2θV d θ c o r r e c t e d = V d θ 1 − β 1 t V_{d\theta}^{corrected}=\frac{V_{d\theta}}{1-\beta_1^t} Vdθcorrected=1−β1tVdθ
S d θ c o r r e c t e d = S d θ 1 − β 2 t S_{d\theta}^{corrected}=\frac{S_{d\theta}}{1-\beta_2^t} Sdθcorrected=1−β2tSdθ
θ = θ − α V d θ c o r r e c t e d S d θ c o r r e c t e d + ϵ \theta=\theta-\alpha\frac{V_{d\theta}^{corrected}}{\sqrt{S_{d\theta}^{corrected}}+\epsilon} θ=θ−αSdθcorrected+ϵVdθcorrected
β 1 = 0.9 \beta_1=0.9 β1=0.9, β 2 = 0.999 \beta_2=0.999 β2=0.999, ϵ = 1 0 − 8 \epsilon=10^{-8} ϵ=10−8
学习率衰减
1 epoch
:遍历一次整个数据集
1 iteration
:遍历batchsize的数据,进行一次梯度下降
学习开始可以承受较大的步伐,但开始收敛时,要减小步伐,因此需要学习率衰减。
α = 1 1 + d e c a y − r a t e ∗ e p o c h − n u m α 0 \alpha=\frac{1}{1+decay-rate*epoch-num}\alpha_0 α=1+decay−rate∗epoch−num1α0
α = 0.9 5 e p o c h − n u m α 0 \alpha=0.95^{epoch-num}\alpha_0 α=0.95epoch−numα0