cross_entropy#

ivy.cross_entropy(true, pred, /, *, axis=-1, epsilon=1e-07, reduction='sum', out=None)[source]#

Compute cross-entropy between predicted and true discrete distributions.

Parameters:
  • true (Union[Array, NativeArray]) – input array containing true labels.

  • pred (Union[Array, NativeArray]) – input array containing the predicted labels.

  • axis (int) – the axis along which to compute the cross-entropy. If axis is -1, (default: -1) the cross-entropy will be computed along the last dimension. Default: -1.

  • epsilon (float) – a float in [0.0, 1.0] specifying the amount of smoothing when calculating (default: 1e-07) the loss. If epsilon is 0, no smoothing will be applied. Default: 1e-7.

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

Return type:

Array

Returns:

ret – The cross-entropy loss between the given distributions

Examples

>>> x = ivy.array([0, 0, 1, 0])
>>> y = ivy.array([0.25, 0.25, 0.25, 0.25])
>>> print(ivy.cross_entropy(x, y))
ivy.array(1.3862944)
>>> z = ivy.array([0.1, 0.1, 0.7, 0.1])
>>> print(ivy.cross_entropy(x, z))
ivy.array(0.35667497)
Array.cross_entropy(self, pred, /, *, axis=-1, epsilon=1e-07, reduction='sum', out=None)#

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

Parameters:
  • self (Array) – input array containing true labels.

  • pred (Union[Array, NativeArray]) – input array containing the predicted labels.

  • axis (int) – the axis along which to compute the cross-entropy. If axis is -1, (default: -1) the cross-entropy will be computed along the last dimension. Default: -1.

  • epsilon (float) – a float in [0.0, 1.0] specifying the amount of smoothing when calculating (default: 1e-07) the loss. If epsilon is 0, no smoothing will be applied. Default: 1e-7.

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

Return type:

Array

Returns:

ret – The cross-entropy loss between the given distributions.

Examples

>>> x = ivy.array([0, 0, 1, 0])
>>> y = ivy.array([0.25, 0.25, 0.25, 0.25])
>>> z = x.cross_entropy(y)
>>> print(z)
ivy.array(1.3862944)
Container.cross_entropy(self, pred, /, *, axis=-1, epsilon=1e-07, reduction='sum', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#

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

Parameters:
  • self (Container) – input container containing true labels.

  • pred (Union[Container, Array, NativeArray]) – input array or container containing the predicted labels.

  • axis (Union[int, Container]) – the axis along which to compute the cross-entropy. If axis is -1, (default: -1) the cross-entropy will be computed along the last dimension. Default: -1.

  • epsilon (Union[float, Container]) – a float in [0.0, 1.0] specifying the amount of smoothing when calculating (default: 1e-07) the loss. If epsilon is 0, no smoothing will be applied. Default: 1e-7.

  • 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[Container]) – optional output container, for writing the result to. It must have a shape (default: None) that the inputs broadcast to.

Return type:

Container

Returns:

ret – The cross-entropy loss between the given distributions.

Examples

>>> x = ivy.Container(a=ivy.array([1, 0, 0]),b=ivy.array([0, 0, 1]))
>>> y = ivy.Container(a=ivy.array([0.6, 0.2, 0.3]),b=ivy.array([0.8, 0.2, 0.2]))
>>> z = x.cross_entropy(y)
>>> print(z)
{
    a:ivy.array(0.5108256),
    b:ivy.array(1.609438)
}