paddle.fluid.layers.nn. clip_by_norm ( x, max_norm, name=None ) [source]

ClipByNorm Operator.

This operator limits the L2 norm of the input $X$ within $max_norm$. If the L2 norm of $X$ is less than or equal to $max_norm$, $Out$ will be the same as $X$. If the L2 norm of $X$ is greater than $max_norm$, $X$ will be linearly scaled to make the L2 norm of $Out$ equal to $max_norm$, as shown in the following formula:

$$ Out = \frac{max\_norm * X}{norm(X)}, $$

where $norm(X)$ represents the L2 norm of $X$.

  • x (Variable) – (Tensor) The input of clip_by_norm op and data type is float32.The number of dimensions must be between [1, 9]

  • max_norm (FLOAT) – (float) The maximum norm value

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.


out(Variable): (Tensor) The output of clip_by_norm op with shape as input(X)The data type is float32

Return type



import paddle
import paddle.fluid as fluid

input = paddle.to_tensor([[2.0, 2.0], [2.0, 2.0]], dtype='float32')
reward = fluid.layers.clip_by_norm(x=input, max_norm=1.0)
# [[0.5, 0.5], [0.5, 0.5]]