mindspore.ops.AssignAdd

class mindspore.ops.AssignAdd[source]

Updates a Parameter by adding a value to it.

Inputs of variable and value 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. If value is a number, the number is automatically converted to Tensor, and the data type is consistent with the Tensor data type involved in the operation. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Note

Since variable is a data type Parameter, the data type cannot be changed, so only the type of value is allowed to be promoted to the type of variable. And the conversion type supported by different devices will be different, it is recommended to use the same data type when using this operator.

Inputs:
  • variable (Parameter) - The Parameter. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • value (Union[numbers.Number, Tensor]) - The value to be added to the variable. It must have the same shape as variable if it is a Tensor. it is recommended to use the same data type when using this operator.

Outputs:

Tensor, has the same data type and shape as original variable.

Raises

TypeError – If value is neither Number nor Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.AssignAdd = ops.AssignAdd()
...         self.variable = mindspore.Parameter(initializer(1, [1], mindspore.int64), name="global_step")
...
...     def construct(self, x):
...         self.AssignAdd(self.variable, x)
...         return self.variable
...
>>> net = Net()
>>> value = Tensor(np.ones([1]).astype(np.int64)*100)
>>> output = net(value)
>>> print(output)
[101]