mindspore.ops.ApplyAdagradV2
- class mindspore.ops.ApplyAdagradV2(*args, **kwargs)[source]
Updates relevant entries according to the adagradv2 scheme.
\[accum += grad * grad\]\[var -= lr * grad * \frac{1}{\sqrt{accum} + \epsilon}\]Inputs of var, accum and grad 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
- Inputs:
var (Parameter) - Variable to be updated. With float16 or float32 data type.
accum (Parameter) - Accumulation to be updated. The shape and dtype must be the same as var. With float16 or float32 data type.
lr (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float16 or float32 data type.
grad (Tensor) - A tensor for gradient. The shape and dtype must be the same as var. With float16 or float32 data type.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as m.
- Raises
- Supported Platforms:
Ascend
Examples
>>> import numpy as np >>> import mindspore.nn as nn >>> from mindspore import Tensor >>> from mindspore import 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.apply_adagrad_v2 = ops.ApplyAdagradV2(epsilon=1e-6) ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... def construct(self, lr, grad): ... out = self.apply_adagrad_v2(self.var, self.accum, lr, grad) ... return out ... >>> net = Net() >>> lr = Tensor(0.001, mstype.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(lr, grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.99638879e-01, 3.99296492e-01], [ 9.97817814e-02, 4.99281585e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 6.90000057e-01, 9.90000010e-01], [ 2.10000008e-01, 1.24000001e+00]]))