paddle.fluid.layers.nn. l2_normalize ( x, axis, epsilon=1e-12, name=None ) [source]

This op normalizes x along dimension axis using an L2 norm. For a 1-D tensor (dim is fixed to 0), this layer computes

\[\begin{split}y = \\frac{x}{ \sqrt{\sum {x^2} + epsion }}\end{split}\]

For x with more dimensions, this layer independently normalizes each 1-D slice along dimension axis.

  • x (Variable|list) – The input tensor could be N-D tensor, and the input data type could be float16, float32 or float64.

  • axis (int) – The axis on which to apply normalization. If axis < 0, the dimension to normalization is rank(X) + axis. -1 is the last dimension.

  • epsilon (float) – The epsilon value is used to avoid division by zero, the default value is 1e-12.

name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name


The output has the same shape and data type with x.

Return type



import paddle

X = paddle.randn(shape=[3, 5], dtype='float64')
out = paddle.fluid.layers.l2_normalize(X, axis=-1)

# [[ 0.21558504  0.56360189  0.47466096  0.46269539 -0.44326736]
#  [-0.70602414 -0.52745777  0.37771788 -0.2804768  -0.04449922]
#  [-0.33972208 -0.43014923  0.31772556  0.76617881 -0.10761525]]