mindspore.ops.assign_sub

View Source On Gitee
mindspore.ops.assign_sub(variable, value)[source]

Updates a Parameter or Tensor by subtracting a value from it.

Args of variable and value 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. 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.

Note

Since variable is a data type Parameter or Tensor, 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.

Parameters
  • variable (Union[Parameter, Tensor]) – The Parameter or Tensor. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • value (Tensor) – The value to be subtracted from the variable. It must have the same shape as variable. it is recommended to use the same data type when using this operator.

Returns

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

Raises
  • TypeError – If value is neither Number nor Tensor.

  • RuntimeError – If the type conversion between variable and value is not supported.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> from mindspore.common.initializer import initializer
>>> variable = mindspore.Parameter(initializer(1, [1], mindspore.int32), name="global_step")
>>> value = Tensor(np.ones([1]).astype(np.int32) * 100)
>>> ops.assign_sub(variable, value)
>>> print(variable.asnumpy())
[-99]