mindspore.ops.FusedSparseAdam
- class mindspore.ops.FusedSparseAdam(*args, **kwargs)[source]
Merges the duplicate value of the gradient and then updates parameters by the Adaptive Moment Estimation (Adam) algorithm. This operator is used when the gradient is sparse.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.
All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.
- Parameters
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.
- Inputs:
var (Parameter) - Parameters to be updated with float32 data type.
m (Parameter) - The 1st moment vector in the updating formula, has the same type as var with float32 data type.
v (Parameter) - The 2nd moment vector in the updating formula. Mean square gradients, has the same type as var with float32 data type.
beta1_power (Tensor) - \(beta_1^t\) in the updating formula with float32 data type.
beta2_power (Tensor) - \(beta_2^t\) in the updating formula with float32 data type.
lr (Tensor) - \(l\) in the updating formula. With float32 data type.
beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type.
beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type.
epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type.
gradient (Tensor) - Gradient value with float32 data type.
indices (Tensor) - Gradient indices with int32 data type.
- Outputs:
Tuple of 3 Tensors, this operator will update the input parameters directly, the outputs are useless.
var (Tensor) - A Tensor with shape (1,).
m (Tensor) - A Tensor with shape (1,).
v (Tensor) - A Tensor with shape (1,).
- Raises
- Supported Platforms:
Ascend
CPU
Examples
>>> import numpy as np >>> import mindspore.nn as nn >>> from mindspore import Tensor, Parameter >>> from mindspore.ops import operations as ops >>> import mindspore.common.dtype as mstype >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.sparse_apply_adam = ops.FusedSparseAdam() ... self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="v") ... def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, indices): ... out = self.sparse_apply_adam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2, ... epsilon, grad, indices) ... return out ... >>> net = Net() >>> beta1_power = Tensor(0.9, mstype.float32) >>> beta2_power = Tensor(0.999, mstype.float32) >>> lr = Tensor(0.001, mstype.float32) >>> beta1 = Tensor(0.9, mstype.float32) >>> beta2 = Tensor(0.999, mstype.float32) >>> epsilon = Tensor(1e-8, mstype.float32) >>> gradient = Tensor(np.random.rand(2, 1, 2), mstype.float32) >>> indices = Tensor([0, 1], mstype.int32) >>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices) >>> print(net.var.asnumpy()) [[[0.9996963 0.9996977 ]] [[0.99970144 0.9996992 ]] [[0.99971527 0.99971527]]]