mindspore.nn.Optimizer
- class mindspore.nn.Optimizer(learning_rate, parameters, weight_decay=0.0, loss_scale=1.0)[source]
Base class for all optimizers.
Note
This class defines the API to add Ops to train a model. Never use this class directly, but instead instantiate one of its subclasses.
Different parameter groups can set different learning_rate, weight_decay and grad_centralization.
When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight_decay is positive. For most optimizer, when not separating parameters, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.
When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.
To improve parameter groups performance, the customized order of parameters can be supported.
- Parameters
learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float.
parameters (Union[list[Parameter], list[dict]]) –
When the parameters is a list of Parameter which will be updated, the element in parameters must be class Parameter. When the parameters is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.
params: Required. The value must be a list of Parameter.
lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.
weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.
order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.
grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.
weight_decay (Union[float, int]) – An int or a floating point value for the weight decay. It must be equal to or greater than 0. If the type of weight_decay input is int, it will be converted to float. Default: 0.0.
loss_scale (float) – A floating point value for the loss scale. It must be greater than 0. If the type of loss_scale input is int, it will be converted to float. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class
mindspore.FixedLossScaleManager
for more details. Default: 1.0.
- Raises
TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.
TypeError – If element of parameters is neither Parameter nor dict.
TypeError – If loss_scale is not a float.
TypeError – If weight_decay is neither float nor int.
ValueError – If loss_scale is less than or equal to 0.
ValueError – If weight_decay is less than 0.
ValueError – If learning_rate is a Tensor, but the dimension of tensor is greater than 1.
- Supported Platforms:
Ascend
GPU
- broadcast_params(optim_result)[source]
Apply Broadcast operations in the sequential order of parameter groups.
- Returns
bool, the status flag.
- decay_weight(gradients)[source]
Weight decay.
An approach to reduce the overfitting of a deep learning neural network model.
- get_lr()[source]
Get the learning rate of current step.
- Returns
float, the learning rate of current step.
- gradients_centralization(gradients)[source]
Gradients centralization.
A method for optimizing convolutional layer parameters to impore the training speed of a deep learning neural network model.
- scale_grad(gradients)[source]
Loss scale for mixed precision.
An approach of mixed precision training to improve the speed and energy efficiency of training deep neural network.
- property target
The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.
- property unique
The method is to see whether to make unique. The input type is bool. The method is read-only.