mindspore.ops.FusedSparseProximalAdagrad

class mindspore.ops.FusedSparseProximalAdagrad(use_locking=False)[源代码]

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

\[\begin{split}\begin{array}{ll} \\ 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) \end{array}\end{split}\]

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

  • accum (Parameter) - Variable tensor to be updated, has the same shape and data type as var.

  • lr (Tensor) - The learning rate value. The data type must be float32. The shape is \((1, )\).

  • l1 (Tensor) - l1 regularization strength. The data type must be float32. The shape is \((1, )\).

  • l2 (Tensor) - l2 regularization strength. The data type must be float32. The shape is \((1, )\).

  • grad (Tensor) - A tensor of the same data type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

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

  • var (Tensor) - A Tensor with shape \((N, *)\).

  • 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.

  • 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.sparse_apply_proximal_adagrad = ops.FusedSparseProximalAdagrad()
...         self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="accum")
...         self.lr = Tensor(0.01, mindspore.float32)
...         self.l1 = Tensor(0.0, mindspore.float32)
...         self.l2 = Tensor(0.0, mindspore.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.array([[[0.1, 0.1]], [[0.1, 0.1]]]).astype(np.float32))
>>> indices = Tensor(np.array([0, 1]).astype(np.int32))
>>> output = net(grad, indices)
>>> print(net.var.asnumpy())
[[[0.99900496 0.99900496]]
 [[0.99900496 0.99900496]]
 [[1.         1.        ]]]