mindspore.ops.FusedSparseProximalAdagrad

class mindspore.ops.FusedSparseProximalAdagrad(*args, **kwargs)[source]

Merges the duplicate value of the gradient and then updates relevant entries according to the proximal adagrad algorithm.

\[accum += grad * grad\]
\[\text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}}\]
\[var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0)\]

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) – If true, the variable and accumulation tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - Variable tensor to be updated. The data type must be float32.

  • accum (Parameter) - Variable tensor to be updated, has the same dtype as var.

  • lr (Tensor) - The learning rate value. The data type must be float32.

  • l1 (Tensor) - l1 regularization strength. The data type must be float32.

  • l2 (Tensor) - l2 regularization strength. The data type must be float32.

  • grad (Tensor) - A tensor of the same type as var, for the gradient. The data type must be float32.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The data type must be int32.

Outputs:

Tuple of 2 Tensors, this operator will update the input parameters directly, the outputs are useless.

  • var (Tensor) - A Tensor with shape (1,).

  • accum (Tensor) - A Tensor with shape (1,).

Raises
  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, accum, lr, l1, l2 or grad is not float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.nn as nn
>>> import mindspore.common.dtype as mstype
>>> from mindspore import Tensor, Parameter
>>> from mindspore.ops import operations as ops
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_proximal_adagrad = ops.FusedSparseProximalAdagrad()
...         self.var = Parameter(Tensor(np.random.rand(3, 1, 2).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.random.rand(3, 1, 2).astype(np.float32)), name="accum")
...         self.lr = Tensor(0.01, mstype.float32)
...         self.l1 = Tensor(0.0, mstype.float32)
...         self.l2 = Tensor(0.0, mstype.float32)
...     def construct(self, grad, indices):
...         out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1,
...                                                  self.l2, grad, indices)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.random.rand(2, 1, 2).astype(np.float32))
>>> indices = Tensor(np.array([0, 1]).astype(np.int32))
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[1], dtype=Float32, value= [0.00000000e+00]),
 Tensor(shape=[1], dtype=Float32, value= [0.00000000e+00]))