tf.keras.backend.categorical_crossentropy

View source on GitHub

Categorical crossentropy between an output tensor and a target tensor.

tf.keras.backend.categorical_crossentropy(
    target, output, from_logits=False, axis=-1
)

Arguments:

Returns:

Output tensor.

Raises:

Example:

>>> a = tf.constant([1., 0., 0., 0., 1., 0., 0., 0., 1.], shape=[3,3])
>>> print(a)
tf.Tensor(
  [[1. 0. 0.]
   [0. 1. 0.]
   [0. 0. 1.]], shape=(3, 3), dtype=float32)
>>> b = tf.constant([.9, .05, .05, .5, .89, .6, .05, .01, .94], shape=[3,3])
>>> print(b)
tf.Tensor(
  [[0.9  0.05 0.05]
   [0.5  0.89 0.6 ]
   [0.05 0.01 0.94]], shape=(3, 3), dtype=float32)
>>> loss = tf.keras.backend.categorical_crossentropy(a, b)
>>> print(loss)
tf.Tensor([0.10536055 0.8046684  0.06187541], shape=(3,), dtype=float32)
>>> loss = tf.keras.backend.categorical_crossentropy(a, a)
>>> print(loss)
tf.Tensor([1.1920929e-07 1.1920929e-07 1.19...e-07], shape=(3,),
dtype=float32)