l2_normalize#

ivy.l2_normalize(x, /, *, axis=None, out=None)[source]#

Normalize the input array along the given axis to have L2 norm equal to 1.

Parameters:
  • x (Union[Array, NativeArray]) – Input array.

  • axis (Optional[int]) – Axis along which to normalize. If None, the whole array is normalized. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type:

Array

Returns:

ret – The normalized array.

Examples

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> ivy.l2_normalize(x, axis=1)
ivy.array([[0.4472, 0.8944],
           [0.6, 0.8]])
Array.l2_normalize(self, axis=None, out=None)#

Normalize the array to have unit L2 norm.

Parameters:
  • self (Array) – Input array.

  • axis (Optional[int]) – Axis along which to normalize. If None, the whole array (default: None) is normalized.

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a (default: None) shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – The normalized array.

Examples

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> x.l2_normalize(axis=1)
ivy.array([[0.4472, 0.8944],
           [0.6, 0.8]])
Container.l2_normalize(self, axis=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#

ivy.Container instance method variant of ivy.l2_normalize. This method simply wraps the function, and so the docstring for ivy.l2_normalize also applies to this method with minimal changes.

Parameters:
  • self – The input container with leaves to be normalized.

  • axis (Optional[int]) – The axis along which to normalize. (default: None)

  • key_chains (Optional[Union[List[str], Dict[str, str]]]) – The key-chains to apply or not apply the method to. Default is None. (default: None)

  • to_apply (bool) – If True, the method will be applied to key_chains, otherwise key_chains (default: True) will be skipped. Default is True.

  • prune_unapplied (bool) – Whether to prune key_chains for which the function was not applied. (default: False) Default is False.

  • map_sequences (bool) – Whether to also map method to sequences (lists, tuples). (default: False) Default is False.

  • out – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the normalized leaves.

Examples

>>> x = ivy.Container(a=ivy.array([[0.5, 1.5, 2.5], [3.5, 4.5, 5.5]])))
...                    b=ivy.array([[-1., -1.], [-1., -0.5]]]))
>>> y = x.static_l2_normalize(axis=1)
>>> print(y)
{
    a: ivy.array([[0.16903085, 0.50709254, 0.84515423],
                  [0.44183609, 0.56807494, 0.69431382]]),
    b: ivy.array([[-0.70710677, -0.70710677],
                  [-0.89442718, -0.44721359]])
}