.. _sec_rmsprop: RMSProp算法 =========== :numref:`sec_adagrad`\ 中的关键问题之一,是学习率按预定时间表\ :math:`\mathcal{O}(t^{-\frac{1}{2}})`\ 显著降低。 虽然这通常适用于凸问题,但对于深度学习中遇到的非凸问题,可能并不理想。 但是,作为一个预处理器,Adagrad算法按坐标顺序的适应性是非常可取的。 :cite:`Tieleman.Hinton.2012`\ 建议以RMSProp算法作为将速率调度与坐标自适应学习率分离的简单修复方法。 问题在于,Adagrad算法将梯度\ :math:`\mathbf{g}_t`\ 的平方累加成状态矢量\ :math:`\mathbf{s}_t = \mathbf{s}_{t-1} + \mathbf{g}_t^2`\ 。 因此,由于缺乏规范化,没有约束力,\ :math:`\mathbf{s}_t`\ 持续增长,几乎上是在算法收敛时呈线性递增。 解决此问题的一种方法是使用\ :math:`\mathbf{s}_t / t`\ 。 对\ :math:`\mathbf{g}_t`\ 的合理分布来说,它将收敛。 遗憾的是,限制行为生效可能需要很长时间,因为该流程记住了值的完整轨迹。 另一种方法是按动量法中的方式使用泄漏平均值,即\ :math:`\mathbf{s}_t \leftarrow \gamma \mathbf{s}_{t-1} + (1-\gamma) \mathbf{g}_t^2`\ ,其中参数\ :math:`\gamma > 0`\ 。 保持所有其它部分不变就产生了RMSProp算法。 算法 ---- 让我们详细写出这些方程式。 .. math:: \begin{aligned} \mathbf{s}_t & \leftarrow \gamma \mathbf{s}_{t-1} + (1 - \gamma) \mathbf{g}_t^2, \\ \mathbf{x}_t & \leftarrow \mathbf{x}_{t-1} - \frac{\eta}{\sqrt{\mathbf{s}_t + \epsilon}} \odot \mathbf{g}_t. \end{aligned} 常数\ :math:`\epsilon > 0`\ 通常设置为\ :math:`10^{-6}`\ ,以确保我们不会因除以零或步长过大而受到影响。 鉴于这种扩展,我们现在可以自由控制学习率\ :math:`\eta`\ ,而不考虑基于每个坐标应用的缩放。 就泄漏平均值而言,我们可以采用与之前在动量法中适用的相同推理。 扩展\ :math:`\mathbf{s}_t`\ 定义可获得 .. math:: \begin{aligned} \mathbf{s}_t & = (1 - \gamma) \mathbf{g}_t^2 + \gamma \mathbf{s}_{t-1} \\ & = (1 - \gamma) \left(\mathbf{g}_t^2 + \gamma \mathbf{g}_{t-1}^2 + \gamma^2 \mathbf{g}_{t-2} + \ldots, \right). \end{aligned} 同之前在 :numref:`sec_momentum`\ 小节一样,我们使用\ :math:`1 + \gamma + \gamma^2 + \ldots, = \frac{1}{1-\gamma}`\ 。 因此,权重总和标准化为\ :math:`1`\ 且观测值的半衰期为\ :math:`\gamma^{-1}`\ 。 让我们图像化各种数值的\ :math:`\gamma`\ 在过去40个时间步长的权重。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline import math from mxnet import np, npx from d2l import mxnet as d2l npx.set_np() d2l.set_figsize() gammas = [0.95, 0.9, 0.8, 0.7] for gamma in gammas: x = np.arange(40).asnumpy() d2l.plt.plot(x, (1-gamma) * gamma ** x, label=f'gamma = {gamma:.2f}') d2l.plt.xlabel('time'); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [07:00:58] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU .. figure:: output_rmsprop_251805_3_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import math import torch from d2l import torch as d2l d2l.set_figsize() gammas = [0.95, 0.9, 0.8, 0.7] for gamma in gammas: x = torch.arange(40).detach().numpy() d2l.plt.plot(x, (1-gamma) * gamma ** x, label=f'gamma = {gamma:.2f}') d2l.plt.xlabel('time'); .. figure:: output_rmsprop_251805_6_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import math import tensorflow as tf from d2l import tensorflow as d2l d2l.set_figsize() gammas = [0.95, 0.9, 0.8, 0.7] for gamma in gammas: x = tf.range(40).numpy() d2l.plt.plot(x, (1-gamma) * gamma ** x, label=f'gamma = {gamma:.2f}') d2l.plt.xlabel('time'); .. figure:: output_rmsprop_251805_9_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import warnings from d2l import paddle as d2l warnings.filterwarnings("ignore") import math import paddle d2l.set_figsize() gammas = [0.95, 0.9, 0.8, 0.7] for gamma in gammas: x = paddle.arange(40).detach().numpy() d2l.plt.plot(x, (1-gamma) * gamma ** x, label=f'gamma = {gamma:.2f}') d2l.plt.xlabel('time'); .. figure:: output_rmsprop_251805_12_0.svg .. raw:: html
.. raw:: html
从零开始实现 ------------ 和之前一样,我们使用二次函数\ :math:`f(\mathbf{x})=0.1x_1^2+2x_2^2`\ 来观察RMSProp算法的轨迹。 回想在 :numref:`sec_adagrad`\ 一节中,当我们使用学习率为0.4的Adagrad算法时,变量在算法的后期阶段移动非常缓慢,因为学习率衰减太快。 RMSProp算法中不会发生这种情况,因为\ :math:`\eta`\ 是单独控制的。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def rmsprop_2d(x1, x2, s1, s2): g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6 s1 = gamma * s1 + (1 - gamma) * g1 ** 2 s2 = gamma * s2 + (1 - gamma) * g2 ** 2 x1 -= eta / math.sqrt(s1 + eps) * g1 x2 -= eta / math.sqrt(s2 + eps) * g2 return x1, x2, s1, s2 def f_2d(x1, x2): return 0.1 * x1 ** 2 + 2 * x2 ** 2 eta, gamma = 0.4, 0.9 d2l.show_trace_2d(f_2d, d2l.train_2d(rmsprop_2d)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output epoch 20, x1: -0.010599, x2: 0.000000 .. figure:: output_rmsprop_251805_18_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def rmsprop_2d(x1, x2, s1, s2): g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6 s1 = gamma * s1 + (1 - gamma) * g1 ** 2 s2 = gamma * s2 + (1 - gamma) * g2 ** 2 x1 -= eta / math.sqrt(s1 + eps) * g1 x2 -= eta / math.sqrt(s2 + eps) * g2 return x1, x2, s1, s2 def f_2d(x1, x2): return 0.1 * x1 ** 2 + 2 * x2 ** 2 eta, gamma = 0.4, 0.9 d2l.show_trace_2d(f_2d, d2l.train_2d(rmsprop_2d)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output epoch 20, x1: -0.010599, x2: 0.000000 .. figure:: output_rmsprop_251805_21_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def rmsprop_2d(x1, x2, s1, s2): g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6 s1 = gamma * s1 + (1 - gamma) * g1 ** 2 s2 = gamma * s2 + (1 - gamma) * g2 ** 2 x1 -= eta / math.sqrt(s1 + eps) * g1 x2 -= eta / math.sqrt(s2 + eps) * g2 return x1, x2, s1, s2 def f_2d(x1, x2): return 0.1 * x1 ** 2 + 2 * x2 ** 2 eta, gamma = 0.4, 0.9 d2l.show_trace_2d(f_2d, d2l.train_2d(rmsprop_2d)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output epoch 20, x1: -0.010599, x2: 0.000000 .. figure:: output_rmsprop_251805_24_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def rmsprop_2d(x1, x2, s1, s2): g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6 s1 = gamma * s1 + (1 - gamma) * g1 ** 2 s2 = gamma * s2 + (1 - gamma) * g2 ** 2 x1 -= eta / math.sqrt(s1 + eps) * g1 x2 -= eta / math.sqrt(s2 + eps) * g2 return x1, x2, s1, s2 def f_2d(x1, x2): return 0.1 * x1 ** 2 + 2 * x2 ** 2 eta, gamma = 0.4, 0.9 d2l.show_trace_2d(f_2d, d2l.train_2d(rmsprop_2d)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output epoch 20, x1: -0.010599, x2: 0.000000 .. figure:: output_rmsprop_251805_27_1.svg .. raw:: html
.. raw:: html
接下来,我们在深度网络中实现RMSProp算法。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def init_rmsprop_states(feature_dim): s_w = np.zeros((feature_dim, 1)) s_b = np.zeros(1) return (s_w, s_b) def rmsprop(params, states, hyperparams): gamma, eps = hyperparams['gamma'], 1e-6 for p, s in zip(params, states): s[:] = gamma * s + (1 - gamma) * np.square(p.grad) p[:] -= hyperparams['lr'] * p.grad / np.sqrt(s + eps) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def init_rmsprop_states(feature_dim): s_w = torch.zeros((feature_dim, 1)) s_b = torch.zeros(1) return (s_w, s_b) def rmsprop(params, states, hyperparams): gamma, eps = hyperparams['gamma'], 1e-6 for p, s in zip(params, states): with torch.no_grad(): s[:] = gamma * s + (1 - gamma) * torch.square(p.grad) p[:] -= hyperparams['lr'] * p.grad / torch.sqrt(s + eps) p.grad.data.zero_() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def init_rmsprop_states(feature_dim): s_w = tf.Variable(tf.zeros((feature_dim, 1))) s_b = tf.Variable(tf.zeros(1)) return (s_w, s_b) def rmsprop(params, grads, states, hyperparams): gamma, eps = hyperparams['gamma'], 1e-6 for p, s, g in zip(params, states, grads): s[:].assign(gamma * s + (1 - gamma) * tf.math.square(g)) p[:].assign(p - hyperparams['lr'] * g / tf.math.sqrt(s + eps)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def init_rmsprop_states(feature_dim): s_w = paddle.zeros((feature_dim, 1)) s_b = paddle.zeros([1]) return (s_w, s_b) def rmsprop(params, states, hyperparams): a = [] gamma, eps = hyperparams['gamma'], 1e-6 for p, s in zip(params, states): with paddle.no_grad(): s[:] = gamma * s + (1 - gamma) * paddle.square(p.grad) p[:] -= hyperparams['lr'] * p.grad / paddle.sqrt(s + eps) p.grad.zero_() a.append(p) return a .. raw:: html
.. raw:: html
我们将初始学习率设置为0.01,加权项\ :math:`\gamma`\ 设置为0.9。 也就是说,\ :math:`\mathbf{s}`\ 累加了过去的\ :math:`1/(1-\gamma) = 10`\ 次平方梯度观测值的平均值。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python data_iter, feature_dim = d2l.get_data_ch11(batch_size=10) d2l.train_ch11(rmsprop, init_rmsprop_states(feature_dim), {'lr': 0.01, 'gamma': 0.9}, data_iter, feature_dim); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.245, 0.080 sec/epoch .. figure:: output_rmsprop_251805_48_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python data_iter, feature_dim = d2l.get_data_ch11(batch_size=10) d2l.train_ch11(rmsprop, init_rmsprop_states(feature_dim), {'lr': 0.01, 'gamma': 0.9}, data_iter, feature_dim); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.247, 0.014 sec/epoch .. figure:: output_rmsprop_251805_51_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python data_iter, feature_dim = d2l.get_data_ch11(batch_size=10) d2l.train_ch11(rmsprop, init_rmsprop_states(feature_dim), {'lr': 0.01, 'gamma': 0.9}, data_iter, feature_dim); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.246, 0.112 sec/epoch .. figure:: output_rmsprop_251805_54_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python data_iter, feature_dim = d2l.get_data_ch11(batch_size=10) d2l.train_ch11(rmsprop, init_rmsprop_states(feature_dim), {'lr': 0.01, 'gamma': 0.9}, data_iter, feature_dim); .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.246, 0.049 sec/epoch .. figure:: output_rmsprop_251805_57_1.svg .. raw:: html
.. raw:: html
简洁实现 -------- 我们可直接使用深度学习框架中提供的RMSProp算法来训练模型。 .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python d2l.train_concise_ch11('rmsprop', {'learning_rate': 0.01, 'gamma1': 0.9}, data_iter) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.247, 0.045 sec/epoch .. figure:: output_rmsprop_251805_63_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = torch.optim.RMSprop d2l.train_concise_ch11(trainer, {'lr': 0.01, 'alpha': 0.9}, data_iter) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.244, 0.017 sec/epoch .. figure:: output_rmsprop_251805_66_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = tf.keras.optimizers.RMSprop d2l.train_concise_ch11(trainer, {'learning_rate': 0.01, 'rho': 0.9}, data_iter) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.244, 0.139 sec/epoch .. figure:: output_rmsprop_251805_69_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python trainer = paddle.optimizer.RMSProp d2l.train_concise_ch11(trainer, {'learning_rate': 0.01, 'rho': 0.9}, data_iter) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss: 0.244, 0.033 sec/epoch .. figure:: output_rmsprop_251805_72_1.svg .. raw:: html
.. raw:: html
小结 ---- - RMSProp算法与Adagrad算法非常相似,因为两者都使用梯度的平方来缩放系数。 - RMSProp算法与动量法都使用泄漏平均值。但是,RMSProp算法使用该技术来调整按系数顺序的预处理器。 - 在实验中,学习率需要由实验者调度。 - 系数\ :math:`\gamma`\ 决定了在调整每坐标比例时历史记录的时长。 练习 ---- 1. 如果我们设置\ :math:`\gamma = 1`\ ,实验会发生什么?为什么? 2. 旋转优化问题以最小化\ :math:`f(\mathbf{x}) = 0.1 (x_1 + x_2)^2 + 2 (x_1 - x_2)^2`\ 。收敛会发生什么? 3. 试试在真正的机器学习问题上应用RMSProp算法会发生什么,例如在Fashion-MNIST上的训练。试验不同的取值来调整学习率。 4. 随着优化的进展,需要调整\ :math:`\gamma`\ 吗?RMSProp算法对此有多敏感? .. raw:: html
mxnetpytorchtensorflowpaddle
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html