mindspore.nn.Rprop

查看源文件
class mindspore.nn.Rprop(params, learning_rate=0.1, etas=(0.5, 1.2), step_sizes=(1e-06, 50.0), weight_decay=0.0)[源代码]

弹性反向传播(Rprop)算法的实现。

请参阅论文 A Direct Adaptive Method for Faster Backpropagation Learning: The RPROP Algorithm.

更新公式如下:

\[\begin{split}\begin{gather*} &\hspace{-10mm} \textbf{if} \: g_{t-1} g_t > 0 \\ &\hspace{25mm} \Delta_t \leftarrow \mathrm{min}(\Delta_{t-1} \eta_{+}, \Delta_{max}) \\ &\hspace{0mm} \textbf{else if} \: g_{t-1} g_t < 0 \\ &\hspace{25mm} \Delta_t \leftarrow \mathrm{max}(\Delta_{t-1} \eta_{-}, \Delta_{min}) \\ &\hspace{-25mm} \textbf{else} \: \\ &\hspace{-5mm} \Delta_t \leftarrow \Delta_{t-1} \\ &\hspace{15mm} w_{t} \leftarrow w_{t-1}- \Delta_{t} \mathrm{sign}(g_t) \\ \end{gather*}\end{split}\]

\(\Delta_{min/max}\) 表示最小或者最大步长, \(\eta_{+/-}\) 表示加速和减速因子, \(g\) 表示 gradients\(w\) 表示 params

说明

  • 在参数未分组时,优化器配置的 weight_decay 应用于名称不含”beta”或”gamma”的网络参数。

  • 用户可以分组调整权重衰减策略。分组时,每组网络参数均可配置 weight_decay 。若未配置,则该组网络参数使用优化器中配置的 weight_decay

参数:
  • params (Union[list[Parameter], list[dict]]) - 必须是 Parameter 组成的列表或字典组成的列表。当列表元素是字典时,字典的键可以是”params”、”lr”、”weight_decay”、”grad_centralization”和”order_params”:

    • params - 必填。当前组别的权重,该值必须是 Parameter 列表。

    • lr - 可选。如果键中存在”lr”,则使用对应的值作为学习率。如果没有,则使用优化器中的参数 learning_rate 作为学习率。支持固定和动态学习率。

    • weight_decay - 可选。如果键中存在”weight_decay”,则使用对应的值作为权重衰减值。如果没有,则使用优化器中配置的 weight_decay 作为权重衰减值。 值得注意的是, weight_decay 可以是常量,也可以是Cell类型。Cell类型的weight decay用于实现动态weight decay算法。动态权重衰减和动态学习率相似, 用户需要自定义一个输入为global step的weight_decay_schedule。在训练的过程中,优化器会调用WeightDecaySchedule的实例来获取当前step的weight decay值。

    • grad_centralization - 可选。如果键中存在”grad_centralization”,则使用对应的值,该值必须为布尔类型。如果没有,则认为 grad_centralization 为False。该参数仅适用于卷积层。

    • order_params - 可选。值的顺序是参数更新的顺序。当使用参数分组功能时,通常使用该配置项保持 parameters 的顺序以提升性能。如果键中存在”order_params”,则会忽略该组配置中的其他键。”order_params”中的参数必须在某一组 params 参数中。

  • learning_rate (Union[float, int, Tensor, Iterable, LearningRateSchedule]) - 学习率。默认值: 0.1

    • float - 固定的学习率。必须大于等于零。

    • int - 固定的学习率。必须大于等于零。整数类型会被转换为浮点数。

    • Tensor - 可以是标量或一维向量。标量是固定的学习率。一维向量是动态的学习率,第i步将取向量中第i个值作为学习率。

    • Iterable - 动态的学习率。第i步将取迭代器第i个值作为学习率。

    • LearningRateSchedule - 动态的学习率。在训练过程中,优化器将使用步数(step)作为输入,调用 LearningRateSchedule 实例来计算当前学习率。

  • etas (tuple[float, float]) - 乘法的增加或减少的因子(etaminus, etaplus)。默认值: (0.5, 1.2)

  • step_sizes (tuple[float, float]) - 允许的最小和最大步长(min_step_sizes, max_step_size)。默认值: (1e-6, 50.)

  • weight_decay (Union[float, int, Cell]) - 权重衰减(L2 penalty)。默认值: 0.0

    • float: 固定值,必须大于或者等于0。

    • int: 固定值,必须大于或者等于0,会被转换成float。

    • Cell: 动态weight decay。在训练过程中,优化器会使用步数(step)作为输入,调用该Cell实例来计算当前weight decay值。

输入:
  • gradients (tuple[Tensor]) - params 的梯度,shape与 params 相同。

输出:

Tensor[bool],值为True。

异常:
  • TypeError - 如果 learning_rate 不是int、float、Tensor、Iterable或LearningRateSchedule。

  • TypeError - 如果 parameters 的元素不是Parameter或字典。

  • TypeError - 如果 step_sizesetas 不是tuple。

  • ValueError - 如果最大步长小于最小步长。

  • ValueError - 如果 step_sizesetas 的长度不等于2。

  • TypeError - 如果 etasstep_sizes 中的元素不是float。

  • ValueError - 如果 etaminus 不在(0,1)的范围内,或者 etaplus 不大于1。

  • TypeError - 如果 weight_decay 既不是float也不是int。

  • ValueError - 如果 weight_decay 小于0。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore as ms
>>> from mindspore import nn
>>>
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/master/docs/mindspore/code/lenet.py
>>> net = LeNet5()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.Rprop(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params,'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.Rprop(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 default weight decay of 0.0 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = ms.train.Model(net, loss_fn=loss, optimizer=optim)