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 behavior is not equivalent to the original Adam algorithm, as only the current indices parameters will be updated.

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, the lower priority data type will be converted to the relatively highest priority data type.

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. The shape is $$(N, *)$$ where $$*$$ means, any number of additional dimensions.

• m (Parameter) - The 1st moment vector in the updating formula, has the same shape and data type as var.

• v (Parameter) - The 2nd moment vector in the updating formula, has the same shape and data type as var. 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. The shape is $$(1, )$$.

• beta2_power (Tensor) - $$beta_2^t$$ in the updating formula with float32 data type. The shape is $$(1, )$$.

• lr (Tensor) - $$l$$ in the updating formula with float32 data type. The shape is $$(1, )$$.

• beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type. The shape is $$(1, )$$.

• beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type. The shape is $$(1, )$$.

• epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type. The shape is $$(1, )$$.

• gradient (Tensor) - Gradient value with float32 data type and gradient.shape[1:] = var.shape[1:] if var.shape > 1.

• indices (Tensor) - Gradient indices with int32 data type and indices.shape[0] = gradient.shape[0].

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
• TypeError – If neither use_locking nor use_nestrov is a bool.

• TypeError – If dtype of var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon or gradient is not float32.

• TypeError – If dtype of indices is not int32.

• RuntimeError – If the data type of all inputs except indices conversion of Parameter is not supported.

Supported Platforms:

Ascend CPU

Examples

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         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_lazyadam(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, mindspore.float32)
>>> beta2_power = Tensor(0.999, mindspore.float32)
>>> lr = Tensor(0.001, mindspore.float32)
>>> beta1 = Tensor(0.9, mindspore.float32)
>>> beta2 = Tensor(0.999, mindspore.float32)
>>> epsilon = Tensor(1e-8, mindspore.float32)
>>> gradient = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]), mindspore.float32)
>>> indices = Tensor([0, 1], mindspore.int32)
>>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices)
>>> print(net.var.asnumpy())
[[[0.9997121  0.9997121 ]]
[[0.9997121  0.9997121 ]]
[[1.         1.        ]]]