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], default: None) – Axis along which to normalize. If None, the whole array is normalized.

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

Return type:

Array

Returns:

ret – The normalized array.

Examples

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> y = ivy.l2_normalize(x, axis=1)
>>> print(y)
ivy.array([[0.44721359, 0.89442718],
       [0.60000002, 0.80000001]])
Array.l2_normalize(self, axis=None, out=None)[source]#

Normalize the array to have unit L2 norm.

Parameters:
  • self (Array) – Input array.

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

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

Return type:

Array

Returns:

ret – The normalized array.

Examples

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> y = x.l2_normalize(axis=1)
>>> print(y)
ivy.array([[0.44721359, 0.89442718],
       [0.60000002, 0.80000001]])
Container.l2_normalize(self, axis=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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 (Container) – The input container with leaves to be normalized.

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

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

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

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

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

  • out (Optional[Container], default: None) – 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.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]])
}