W3cubDocs

/TensorFlow 2.3

tf.keras.backend.categorical_crossentropy

View source on GitHub

Categorical crossentropy between an output tensor and a target tensor.

Arguments
target A tensor of the same shape as output.
output A tensor resulting from a softmax (unless from_logits is True, in which case output is expected to be the logits).
from_logits Boolean, whether output is the result of a softmax, or is a tensor of logits.
axis Int specifying the channels axis. axis=-1 corresponds to data format channels_last', andaxis=1corresponds to data formatchannels_first`.
Returns
Output tensor.
Raises
ValueError if axis is neither -1 nor one of the axes of output.

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(np.around(loss, 5))
[0.10536 0.80467 0.06188]
loss = tf.keras.backend.categorical_crossentropy(a, a)
print(np.around(loss, 5))
[0. 0. 0.]

© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/keras/backend/categorical_crossentropy