# tf.keras.backend.categorical_crossentropy

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/backend/categorical_crossentropy) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/keras/backend.py#L4443-L4504) |

Categorical crossentropy between an output tensor and a target tensor.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.keras.backend.categorical_crossentropy`](https://www.tensorflow.org/api_docs/python/tf/keras/backend/categorical_crossentropy)

<br />

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

<br />

<br />

<br />

| ## 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', and`axis=1`corresponds to data format`channels_first\`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Output tensor. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | if `axis` is neither -1 nor one of the axes of `output`. |

<br />

#### 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)