mindspore.nn.SGD

查看源文件
class mindspore.nn.SGD(params, learning_rate=0.1, momentum=0.0, dampening=0.0, weight_decay=0.0, nesterov=False, loss_scale=1.0)[源代码]

随机梯度下降的实现。动量可选。

SGD相关介绍参见 SGD

Nesterov动量公式参见论文 On the importance of initialization and momentum in deep learning

\[v_{t+1} = u \ast v_{t} + gradient \ast (1-dampening)\]

如果nesterov为 True

\[p_{t+1} = p_{t} - lr \ast (gradient + u \ast v_{t+1})\]

如果nesterov为 False

\[p_{t+1} = p_{t} - lr \ast v_{t+1}\]

需要注意的是,对于训练的第一步 \(v_{t+1} = gradient\)。其中,p、v和u分别表示 parametersaccummomentum

说明

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

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

参数:
  • params (Union[list[Parameter], list[dict]]) - 当 params 为会更新的 Parameter 列表时, params 中的元素必须为类 Parameter。当 paramsdict 列表时,”params”、”lr”、”weight_decay”、”grad_centralization”和”order_params”为可以解析的键。

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

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

    • weight_decay - 可选。如果键中存在”weight_decay”,则使用对应的值作为权重衰减值。如果没有,则使用优化器中配置的 weight_decay 作为权重衰减值。当前 weight_decay 仅支持float类型,不支持动态变化。

    • 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 - �����DZ�����һά�����������ǹ̶���ѧϰ�ʡ�һά�����Ƕ�̬��ѧϰ�ʣ���i����ȡ�����е�i��ֵ��Ϊѧϰ�ʡ�

    • Iterable - ��̬��ѧϰ�ʡ���i����ȡ��������i��ֵ��Ϊѧϰ�ʡ�

    • LearningRateSchedule - ��̬��ѧϰ�ʡ���ѵ�������У��Ż�����ʹ�ò�����mindspore.cn/docs/zh-CN/masterateSchedule <https://www.mindspore.cn/docs/zh-CN/r2.3/api_python/mindspore.nn.html#learningrateschedule%E7%B1%BB>`_ ʵ�������㵱ǰѧϰ�ʡ�

  • momentum (float) - 浮点动量,必须大于等于0.0。默认值: 0.0

  • dampening (float) - 浮点动量阻尼值,必须大于等于0.0。默认值: 0.0

  • weight_decay (float) - 权重衰减(L2 penalty),必须大于等于0。默认值: 0.0

  • nesterov (bool) - 启用Nesterov动量。如果使用Nesterov,动量必须为正,阻尼必须等于0.0。默认值: False

  • loss_scale (float) - 梯度缩放系数,必须大于0.0。如果 loss_scale 是整数,它将被转换为浮点数。通常使用默认值,仅当训练时使用了 FixedLossScaleManager,且 FixedLossScaleManagerdrop_overflow_update 属性配置为 False 时,此值需要与 FixedLossScaleManager 中的 loss_scale 相同。有关更多详细信息,请参阅 mindspore.amp.FixedLossScaleManager。默认值: 1.0

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

输出:

Tensor[bool],值为 True

异常:
  • ValueError - 动量、阻尼或重量衰减值小于0.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.SGD(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.SGD(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and 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)