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]

Clip norm of a set of input Tensors. This norm is the result of calculating the norm of all elements in the input separately, connecting them into a vector, and then calculating the norm.

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)) – Norm type. Default: 2.0.

  • error_if_nonfinite (bool) – 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]]