From 6e83bd5dd956d22b1d4ce20648177587bb669890 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 26 Sep 2019 19:43:52 +0800 Subject: [PATCH] Polish the Chinese API documentation of AdagradOptimizer (#1194) * polish adagrad optimizer api zh doc * polish details --- .../optimizer_cn/AdagradOptimizer_cn.rst | 161 ++---------------- 1 file changed, 15 insertions(+), 146 deletions(-) diff --git a/doc/fluid/api_cn/optimizer_cn/AdagradOptimizer_cn.rst b/doc/fluid/api_cn/optimizer_cn/AdagradOptimizer_cn.rst index 575e1cfc9..4493a62b7 100644 --- a/doc/fluid/api_cn/optimizer_cn/AdagradOptimizer_cn.rst +++ b/doc/fluid/api_cn/optimizer_cn/AdagradOptimizer_cn.rst @@ -5,38 +5,42 @@ AdagradOptimizer .. py:class:: paddle.fluid.optimizer.AdagradOptimizer(learning_rate, epsilon=1e-06, regularization=None, name=None, initial_accumulator_value=0.0) -**Adaptive Gradient Algorithm(Adagrad)** +Adaptive Gradient 优化器(自适应梯度优化器,简称Adagrad)可以针对不同参数样本数不平均的问题,自适应地为各个参数分配不同的学习率。 -更新如下: +其参数更新的计算过程如下: .. math:: moment\_out &= moment + grad * grad\\param\_out &= param - \frac{learning\_rate * grad}{\sqrt{moment\_out} + \epsilon} -原始论文(http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf)没有epsilon属性。在我们的实现中也作了如下更新: -http://cs231n.github.io/neural-networks-3/#ada 用于维持数值稳定性,避免除数为0的错误发生。 + +相关论文:`Adaptive Subgradient Methods for Online Learning and Stochastic Optimization `_。 + +原始论文的算法中没有引入上述公式中的 ``epsilon`` 属性,此处引入该属性用于维持数值稳定性,避免除0错误发生。 + +引入epsilon参数依据:`Per-parameter adaptive learning rate methods `_。 参数: - - **learning_rate** (float|Variable)-学习率,用于更新参数。作为数据参数,可以是一个浮点类型值或者有一个浮点类型值的变量 - - **epsilon** (float) - 维持数值稳定性的短浮点型值 - - **regularization** - 规则化函数,例如fluid.regularizer.L2DecayRegularizer - - **name** - 名称前缀(可选) - - **initial_accumulator_value** (float) - moment累加器的初始值。 + - **learning_rate** (float|Variable) - 学习率,用于参数更新的计算。可以是一个浮点型值或者一个值为浮点型的Variable + - **epsilon** (float, 可选) - 维持数值稳定性的浮点型值,默认值为1e-06 + - **regularization** (WeightDecayRegularizer, 可选) - 正则化函数,用于减少泛化误差。例如可以是 :ref:`cn_api_fluid_regularizer_L2DecayRegularizer` ,默认值为None + - **name** (str, 可选) - 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` ,默认值为None + - **initial_accumulator_value** (float, 可选) - moment累加器的初始值,默认值为0.0 **代码示例** .. code-block:: python - import paddle.fluid as fluid import numpy as np + import paddle.fluid as fluid np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) inp = fluid.layers.data( name="inp", shape=[2, 2], append_batch_size=False) out = fluid.layers.fc(inp, size=3) out = fluid.layers.reduce_sum(out) - optimizer = fluid.optimizer.Adagrad(learning_rate=0.2) + optimizer = fluid.optimizer.AdagradOptimizer(learning_rate=0.2) optimizer.minimize(out) exe = fluid.Executor(fluid.CPUPlace()) @@ -45,141 +49,6 @@ http://cs231n.github.io/neural-networks-3/#ada 用于维持数值稳定性,避 feed={"inp": np_inp}, fetch_list=[out.name]) -.. py:method:: apply_gradients(params_grads) - -为给定的params_grads对附加优化算子,为minimize过程的第二步 - -参数: - - **params_grads** (list)- 用于优化的(param, grad)对组成的列表 - -返回: 附加在当前Program的算子组成的列表 - -返回类型: list - -**代码示例** - -.. code-block:: python - - import paddle.fluid as fluid - loss = network() - optimizer = fluid.optimizer.SGD(learning_rate=0.1) - params_grads = optimizer.backward(loss) - # you may append operations for params_grads here - # ... - optimizer.apply_gradients(params_grads) - - -.. py:method:: apply_optimize(loss, startup_program, params_grads) - -为给定的params_grads对附加优化算子,为minimize过程的第二步。 - -参数: - - **loss** (Variable) – 用于优化过程的损失值变量 - - **startup_program** (Program) – 用于初始化在parameter_list中参数的startup_program - - **params_grads** (list)- 用于优化的(param, grad)对组成的列表 - -返回: 附加在当前Program的算子组成的列表 - -返回类型: list - -.. py:method:: backward(loss, startup_program=None, parameter_list=None, no_grad_set=None, callbacks=None) - -自动做diff来向当前program附加反向算子,为minimize过程的第一步。 - -参数: - - **loss** (Variable) – 用于优化过程的损失值变量 - - **startup_program** (Program) – 用于初始化在parameter_list中参数的startup_program - - **parameter_list** (list) – 待更新的Variables组成的列表 - - **no_grad_set** (set|None) – 应该被无视的Variables集合 - - **callbacks** (list|None) – 当为某参数附加反向算子时所要运行的callables组成的列表 - -返回: 附加在当前Program的算子组成的列表 - -返回类型: list - -**代码示例** - -详见apply_gradients的示例 - - -.. py:method:: load(stat_dict) - -在dygraph模式下,附带学习率衰减来加载优化器。 - -参数: - - **stat_dict** – load_persistable方法加载的dict - -**代码示例** - -.. code-block:: python - - from __future__ import print_function - import numpy as np - import paddle - import paddle.fluid as fluid - from paddle.fluid.optimizer import SGDOptimizer - from paddle.fluid.dygraph.nn import FC - from paddle.fluid.dygraph.base import to_variable - - class MLP(fluid.Layer): - def __init__(self, name_scope): - super(MLP, self).__init__(name_scope) - - self._fc1 = FC(self.full_name(), 10) - self._fc2 = FC(self.full_name(), 10) - - def forward(self, inputs): - y = self._fc1(inputs) - y = self._fc2(y) - return y - - with fluid.dygraph.guard(): - mlp = MLP('mlp') - optimizer2 = SGDOptimizer( - learning_rate=fluid.layers.natural_exp_decay( - learning_rate=0.1, - decay_steps=10000, - decay_rate=0.5, - staircase=True)) - - train_reader = paddle.batch( - paddle.dataset.mnist.train(), batch_size=128, drop_last=True) - - for batch_id, data in enumerate(train_reader()): - dy_x_data = np.array( - [x[0].reshape(1, 28, 28) for x in data]).astype('float32') - - y_data = np.array([x[1] for x in data]).astype('int64').reshape( - 128, 1) - - img = to_variable(dy_x_data) - label = to_variable(y_data) - label._stop_gradient = True - cost = mlp(img) - avg_loss = fluid.layers.reduce_mean(cost) - avg_loss.backward() - optimizer.minimize(avg_loss) - mlp.clear_gradients() - fluid.dygraph.save_persistables( - mlp.state_dict(), [optimizer, optimizer2], "save_dir_2") - if batch_id == 2: - break - - with fluid.dygraph.guard(): - mlp_load = MLP('mlp') - optimizer_load2 = SGDOptimizer( - learning_rate=fluid.layers.natural_exp_decay( - learning_rate=0.1, - decay_steps=10000, - decay_rate=0.5, - staircase=True)) - parameters, optimizers = fluid.dygraph.load_persistables( - "save_dir_2") - mlp_load.load_dict(parameters) - optimizer_load2.load(optimizers) - self.assertTrue(optimizer2._learning_rate.__dict__ == optimizer_load2._learning_rate.__dict__) - - .. py:method:: minimize(loss, startup_program=None, parameter_list=None, no_grad_set=None, grad_clip=None) 为网络添加反向计算过程,并根据反向计算所得的梯度,更新parameter_list中的Parameters,最小化网络损失值loss。 -- GitLab