mindspore.ops.clip_by_norm

View Source On Gitee
mindspore.ops.clip_by_norm(x, max_norm, norm_type=2.0, error_if_nonfinite=False)[source]

The input Tensor is cropped based on norm. The computation is done by concatenating the norms of all the input elementsinto a vector and then computing the norm of that vector. The Tensor gradient value corresponding to the identifier.

Note

The interface is suitable for gradient clipping scenarios, and only supports input of type float.

Parameters
  • x (Union[Tensor, list[Tensor], tuple[Tensor]]) – Input that wishes to be clipped.

  • max_norm (Union[float, int]) – The upper limit of the norm for this group of network parameters.

  • norm_type (Union[float, int], optional) – Norm type. Default: 2.0.

  • error_if_nonfinite (bool, optional) – If it is True, an exception is thrown if the total norm from the input is nan, inf or -inf. If it is False, no exception will be thrown.Default: False .

Returns

Tensors, a list or tuple of Tensors, representing clipped Tensors.

Raises

RuntimeError – If the total norm from the x is nan, inf or -inf.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor, ops
>>> x = Tensor([[0.8748, 0.1425, 0.0076], [0.7721, 0.4084, 0.0552], [4.6376, 0.2914, 2.1120]])
>>> out = ops.clip_by_norm(x, max_norm=1)
>>> print(out)
[[0.16650201 0.02712224 0.00144652]
 [0.14695495 0.07773139 0.0105063 ]
 [0.8826814  0.0554626  0.40198016]]