def BinaryCrossEntropy():
  r"""Returns a layer that computes cross-entropy for binary classification.

  The layer takes two inputs:

    - A batch of activation values; each batch item :math:`x` is a float in
      :math:`(-\infty, \infty)`.

    - A batch of binary targets; each target :math:`t` is an integer in
      :math:`\{0, 1\}`.

  The layer maps each activation value into the range :math:`(0, 1)`,
  interpreted as the model-predicted probability that item's category is 1:

  .. math::
      q = \frac 1 {1 + e^{-x}} \ \ \text{[model-predicted probability]}

  and computes cross-entropy (per batch item) by treating the target category
  as having probability 1:

  .. math::
      \text{cross_entropy} = \left\{ \begin{array}{cl}
          - \log q       & \text{if}\ t = 1, \\
          - \log (1 - q) & \text{if}\ t = 0.
      \end{array} \right.

  The layer returns the average of these cross-entropy values over all items in
  the batch.
  """
  def f(model_output, targets):  # pylint: disable=invalid-name
    probabilities = fastmath.expit(model_output)
    binary_entropies = - (targets * jnp.log(probabilities) +
                          (1 - targets) * (jnp.log(1 - probabilities)))
    return jnp.average(binary_entropies)

  return base.Fn('BinaryCrossEntropy', f)
Returns a layer that computes cross-entropy for binary classification.

  The layer takes two inputs:

    - A batch of activation values; each batch item :math:`x` is a float in
      :math:`(-\infty, \infty)`.

    - A batch of binary targets; each target :math:`t` is an integer in
      :math:`\{0, 1\}`.

  The layer maps each activation value into the range :math:`(0, 1)`,
  interpreted as the model-predicted probability that item's category is 1:

  .. math::
      q = \frac 1 {1 + e^{-x}} \ \ \text{[model-predicted probability]}

  and computes cross-entropy (per batch item) by treating the target category
  as having probability 1:

  .. math::
      \text{cross_entropy} = \left\{ \begin{array}{cl}
          - \log q       & \text{if}\ t = 1, \\
          - \log (1 - q) & \text{if}\ t = 0.
      \end{array} \right.

  The layer returns the average of these cross-entropy values over all items in
  the batch.
  
trax/layers/metrics.py