normalize¶
- paddle.nn.functional. normalize ( x, p=2, axis=1, epsilon=1e-12, name=None ) [source]
-
This op normalizes
x
along dimensionaxis
using \(L_p\) norm. This layer computes\[y = \frac{x}{ \max\left( \lvert \lvert x \rvert \rvert_p, epsilon\right) }\]\[\lvert \lvert x \rvert \rvert_p = \left( \sum_i {\lvert x_i \rvert^p} \right)^{1/p}\]where, \(\sum_i{\lvert x_i \rvert^p}\) is calculated along the
axis
dimension.- Parameters
-
x (Tensor) – The input tensor could be N-D tensor, and the input data type could be float32 or float64.
p (float|int, optional) – The exponent value in the norm formulation. Default: 2
axis (int, optional) – The axis on which to apply normalization. If axis < 0, the dimension to normalization is x.ndim + axis. -1 is the last dimension.
epsilon (float, optional) – Small float added to denominator to avoid dividing by zero. Default is 1e-12.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
- Returns
-
Tensor, the output has the same shape and data type with
x
.
Examples
import numpy as np import paddle import paddle.nn.functional as F paddle.disable_static() x = np.arange(6, dtype=np.float32).reshape(2,3) x = paddle.to_tensor(x) y = F.normalize(x) print(y.numpy()) # [[0. 0.4472136 0.8944272 ] # [0.42426404 0.5656854 0.7071067 ]] y = F.normalize(x, p=1.5) print(y.numpy()) # [[0. 0.40862012 0.81724024] # [0.35684016 0.4757869 0.5947336 ]] y = F.normalize(x, axis=0) print(y.numpy()) # [[0. 0.24253564 0.37139067] # [1. 0.97014254 0.9284767 ]]