megengine.functional.normalize

normalize(inp, ord=None, axis=None, eps=1e-12)[source]

Performs \(L_p\) normalization of input tensor along given axis.

For a tensor of shape \((n_0, ..., n_{dim}, ..., n_k)\), each \(n_{dim}\) -element vector \(v\) along dimension axis is transformed as:

\[v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}.\]
Parameters
  • inp (Tensor) – input tensor.

  • ord (Optional[float]) – power of value applied to input tensor.

  • axis (Optional[int]) – dimension to reduce.If None, input must be a vector.

  • eps (float) – a small value to avoid division by zero.

Return type

Tensor

Returns

normalized output tensor.

See also

numpy.linalg.norm / norm

Examples

>>> x = Tensor([[1, 2, 3], [4, 5, 6]])
>>> F.normalize(x, ord=2, axis=0)
Tensor([[0.2425 0.3714 0.4472]
 [0.9701 0.9285 0.8944]], device=xpux:0)
>>> F.normalize(x, ord=2, axis=1)
Tensor([[0.2673 0.5345 0.8018]
 [0.4558 0.5698 0.6838]], device=xpux:0)