binary_cross_entropy#

ivy.binary_cross_entropy(true, pred, /, *, from_logits=False, epsilon=0.0, reduction='mean', pos_weight=None, axis=None, out=None)[source]#

Compute the binary cross entropy loss.

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

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

  • from_logits (bool, default: False) – Whether pred is expected to be a logits tensor. By default, we assume that pred encodes a probability distribution.

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

  • reduction (str, default: 'mean') – 'none': No reduction will be applied to the output. 'mean': The output will be averaged. 'sum': The output will be summed. Default: 'none'.

  • pos_weight (Optional[Union[Array, NativeArray]], default: None) – a weight for positive examples. Must be an array with length equal to the number of classes.

  • axis (Optional[int], default: None) – Axis along which to compute crossentropy.

  • 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 binary cross entropy between the given distributions.

Examples

With ivy.Array input:

>>> x = ivy.array([0, 1, 0, 0])
>>> y = ivy.array([0.2, 0.8, 0.3, 0.8])
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
ivy.array(0.60309976)
>>> x = ivy.array([[0, 1, 1, 0]])
>>> y = ivy.array([[2.6, 6.2, 3.7, 5.3]])
>>> z = ivy.binary_cross_entropy(x, y, reduction='mean')
>>> print(z)
ivy.array(7.6666193)
>>> x = ivy.array([[0, 1, 1, 0]])
>>> y = ivy.array([[2.6, 6.2, 3.7, 5.3]])
>>> pos_weight = ivy.array([1, 2, 3, 4])
>>> z = ivy.binary_cross_entropy(x, y, pos_weight=pos_weight, from_logits=True)
ivy.array(2.01348412)
>>> x = ivy.array([[0, 1, 1, 0]])
>>> y = ivy.array([[2.6, 6.2, 3.7, 5.3]])
>>> pos_weight = ivy.array([1, 2, 3, 4])
>>> z = ivy.binary_cross_entropy(x, y, pos_weight=pos_weight, from_logits=True, reduction='sum', axis=1)
>>> print(z)
ivy.array([8.05393649])
>>> x = ivy.array([[0, 1, 1, 0]])
>>> y = ivy.array([[2.6, 6.2, 3.7, 5.3]])
>>> z = ivy.binary_cross_entropy(x, y, reduction='none', epsilon=0.5)
>>> print(z)
ivy.array([[11.49992943,  3.83330965,  3.83330965, 11.49992943]])
>>> x = ivy.array([[0, 1, 0, 0]])
>>> y = ivy.array([[0.6, 0.2, 0.7, 0.3]])
>>> z = ivy.binary_cross_entropy(x, y, epsilon=1e-3)
>>> print(z)
ivy.array(1.02136981)

With ivy.NativeArray input:

>>> x = ivy.native_array([0, 1, 0, 1])
>>> y = ivy.native_array([0.2, 0.7, 0.2, 0.6])
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
ivy.array(0.32844672)

With a mix of ivy.Array and ivy.NativeArray inputs:

>>> x = ivy.array([0, 0, 1, 1])
>>> y = ivy.native_array([0.1, 0.2, 0.8, 0.6])
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
ivy.array(0.26561815)

With ivy.Container input:

>>> 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 = ivy.binary_cross_entropy(x, y)
>>> print(z)
{
    a: ivy.array(0.36354783),
    b: ivy.array(1.14733934)
}

With a mix of ivy.Array and ivy.Container inputs:

>>> x = ivy.array([1 , 1, 0])
>>> y = ivy.Container(a=ivy.array([0.7, 0.8, 0.2]))
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
{
   a: ivy.array(0.26765382)
}

Instance Method Examples

Using ivy.Array instance method:

>>> x = ivy.array([1, 0, 0, 0])
>>> y = ivy.array([0.8, 0.2, 0.2, 0.2])
>>> z = ivy.binary_cross_entropy(x, y)
>>> print(z)
ivy.array(0.22314337)
Array.binary_cross_entropy(self, pred, /, *, from_logits=False, epsilon=0.0, reduction='mean', pos_weight=None, axis=None, out=None)[source]#

ivy.Array instance method variant of ivy.binary_cross_entropy. This method simply wraps the function, and so the docstring for ivy.binary_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 Predicted labels.

  • from_logits (bool, default: False) – Whether pred is expected to be a logits tensor. By default, we assume that pred encodes a probability distribution.

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

  • reduction (str, default: 'mean') – 'none': No reduction will be applied to the output. 'mean': The output will be averaged. 'sum': The output will be summed. Default: 'none'.

  • pos_weight (Optional[Union[Array, NativeArray]], default: None) – a weight for positive examples. Must be an array with length equal to the number of classes.

  • axis (Optional[int], default: None) – Axis along which to compute crossentropy.

  • 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 binary cross entropy between the given distributions.

Examples

>>> x = ivy.array([1 , 1, 0])
>>> y = ivy.array([0.7, 0.8, 0.2])
>>> z = x.binary_cross_entropy(y)
>>> print(z)
ivy.array(0.26765382)
Container.binary_cross_entropy(self, pred, /, *, from_logits=False, epsilon=0.0, reduction='mean', pos_weight=None, axis=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.binary_cross_entropy. This method simply wraps the function, and so the docstring for ivy.binary_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 Predicted labels. from_logits

    Whether pred is expected to be a logits tensor. By default, we assume that pred encodes a probability distribution.

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

  • reduction (Union[str, Container], default: 'mean') – 'none': No reduction will be applied to the output. 'mean': The output will be averaged. 'sum': The output will be summed. Default: 'none'.

  • pos_weight (Optional[Union[Array, NativeArray, Container]], default: None) – a weight for positive examples. Must be an array with length equal to the number of classes.

  • axis (Optional[Union[int, Container]], default: None) – Axis along which to compute crossentropy.

  • 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 – The binary cross entropy 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.binary_cross_entropy(y)
>>> print(z)
{
    a: ivy.array(0.36354783),
    b: ivy.array(1.14733934)
}