Both, categorical cross entropy and sparse categorical cross entropy have the same loss function which you have mentioned above. The only difference is the format in which you mention (i,e true labels).

If your 's are one-hot encoded, use categorical_crossentropy. Examples (for a 3-class classification): [1,0,0] , [0,1,0], [0,0,1]

But if your 's are integers, use sparse_categorical_crossentropy. Examples for above 3-class classification problem: [1] , [2], [3]

The usage entirely depends on how you load your dataset. One advantage of using sparse categorical cross entropy is it saves time in memory as well as computation because it simply uses a single integer for a class, rather than a whole vector.

Answer from skadaver on Stack Exchange
Top answer
1 of 5
112

Both, categorical cross entropy and sparse categorical cross entropy have the same loss function which you have mentioned above. The only difference is the format in which you mention (i,e true labels).

If your 's are one-hot encoded, use categorical_crossentropy. Examples (for a 3-class classification): [1,0,0] , [0,1,0], [0,0,1]

But if your 's are integers, use sparse_categorical_crossentropy. Examples for above 3-class classification problem: [1] , [2], [3]

The usage entirely depends on how you load your dataset. One advantage of using sparse categorical cross entropy is it saves time in memory as well as computation because it simply uses a single integer for a class, rather than a whole vector.

2 of 5
14

The formula which you posted in your question refers to binary_crossentropy, not categorical_crossentropy. The former is used when you have only one class. The latter refers to a situation when you have multiple classes and its formula looks like below:

This loss works as skadaver mentioned on one-hot encoded values e.g [1,0,0], [0,1,0], [0,0,1]

The sparse_categorical_crossentropy is a little bit different, it works on integers that's true, but these integers must be the class indices, not actual values. This loss computes logarithm only for output index which ground truth indicates to. So when model output is for example [0.1, 0.3, 0.7] and ground truth is 3 (if indexed from 1) then loss compute only logarithm of 0.7. This doesn't change the final value, because in the regular version of categorical crossentropy other values are immediately multiplied by zero (because of one-hot encoding characteristic). Thanks to that it computes logarithm once per instance and omits the summation which leads to better performance. The formula might look like this:

🌐
Medium
medium.com › @shivamvbomble › understanding-sparse-categorical-cross-entropy-and-binary-cross-entropy-d37aa33aed8e
Understanding Sparse Categorical Cross-Entropy and Binary Cross-Entropy | by Shivam Bomble | Medium
February 3, 2025 - Binary Cross-Entropy: For binary classification problems. Categorical Cross-Entropy: For multi-class classification problems. Sparse Categorical Cross-Entropy: A variant of categorical cross-entropy for integer-labeled classes.
Discussions

tensorflow - What is the difference of BinaryCrossentropy and SparseCategoricalCrossentropy? - Stack Overflow
I´m training a CNN for binary image classification and I´m at the point where I have to choose the loss function and searching for answers. At this point I´m getting confused because one half is sa... More on stackoverflow.com
🌐 stackoverflow.com
neural network - Sparse_categorical_crossentropy vs categorical_crossentropy (keras, accuracy) - Data Science Stack Exchange
Which is better for accuracy or are they the same? Of course, if you use categorical_crossentropy you use one hot encoding, and if you use sparse_categorical_crossentropy you encode as normal integ... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
December 1, 2018
python - What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? - Stack Overflow
One good example of the sparse-categorical-cross-entropy is the fasion-mnist dataset. More on stackoverflow.com
🌐 stackoverflow.com
tensorflow - Meaning of sparse in "sparse cross entropy loss"? - Stack Overflow
However, note that this sparse cross-entropy is only suitable for "sparse labels", where exactly one value is 1 and all others are 0 (if the labels were represented as a vector and not just an index). On the other hand, the general CategoricalCrossentropy also works with targets that are not ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › deep learning › sparse-categorical-crossentropy-vs-categorical-crossentropy
Sparse Categorical Crossentropy vs. Categorical Crossentropy - GeeksforGeeks
July 26, 2025 - Sparse Categorical Crossentropy is functionally similar to Categorical Crossentropy but is designed for cases where the target labels are not one-hot encoded. Instead, the labels are represented as integers corresponding to the class indices.
🌐
Medium
medium.com › @shireenchand › choosing-between-cross-entropy-and-sparse-cross-entropy-the-only-guide-you-need-abea92c84662
Choosing between Cross Entropy and Sparse Cross Entropy — The Only Guide you Need! | by Shireen Chand | Medium
July 20, 2023 - In contrast to categorical cross-entropy loss, where the true labels are represented as one-hot encoded vectors, sparse categorical cross-entropy loss expects the target labels to be integers indicating the class indices directly.
🌐
Stack Overflow
stackoverflow.com › questions › 74171755 › what-is-the-difference-of-binarycrossentropy-and-sparsecategoricalcrossentropy
tensorflow - What is the difference of BinaryCrossentropy and SparseCategoricalCrossentropy? - Stack Overflow
At this point I´m getting confused because one half is saying that you should use BinaryCrossentropy and use a Dense-layer at the end with the dimension (None, 1) and others say to use SparseCategoricalCrossentropy and a Dense-layer with dim (None, 2). Also I know that SparseCategoricalCrossentropy is meant to be used for a classification task with more than 2 categories.
🌐
PyTorch & Keras
androidkt.com › home › how to choose cross-entropy loss function in keras?
How to choose cross-entropy loss function in Keras? - PyTorch & Keras
December 8, 2023 - This can mean that the target element ... memory. Sparse cross-entropy addresses this by performing the same cross-entropy calculation of error, without requiring that the target variable be one-hot encoded prior to training...
Find elsewhere
🌐
Keras
keras.io › api › losses › probabilistic_losses
Keras documentation: Probabilistic losses
focal_factor = (1 - output) ** gamma for class 1 focal_factor = output ** gamma for class 0 where gamma is a focusing parameter. When gamma=0, this function is equivalent to the binary crossentropy loss.
🌐
V7 Labs
v7labs.com › home › blog › cross entropy loss: intro, applications, code
Cross Entropy Loss: Intro, Applications, Code
Binary cross entropy is calculated on top of sigmoid outputs, whereas Categorical cross-entropy is calculated over softmax activation outputs.
🌐
GitHub
github.com › christianversloot › machine-learning-articles › blob › main › how-to-use-sparse-categorical-crossentropy-in-keras.md
machine-learning-articles/how-to-use-sparse-categorical-crossentropy-in-keras.md at main · christianversloot/machine-learning-articles
In Keras, this can be done with to_categorical, which essentially applies one-hot encoding to your training set's targets. When applied, you can start using categorical crossentropy. But did you know that there exists another type of loss - sparse categorical crossentropy - with which you can leave the integers as they are, yet benefit from crossentropy loss?
Author   christianversloot
🌐
Apache
cwiki.apache.org › confluence › display › MXNET › Multi-hot+Sparse+Categorical+Cross-entropy
Multi-hot Sparse Categorical Cross-entropy - MXNet - Apache Software Foundation
November 17, 2018 - The only difference between sparse categorical cross entropy and categorical cross entropy is the format of true labels. When we have a single-label, multi-class classification problem, the labels are mutually exclusive for each data, meaning each data entry can only belong to one class.
Top answer
1 of 3
97

Simply:

  • categorical_crossentropy (cce) produces a one-hot array containing the probable match for each category,
  • sparse_categorical_crossentropy (scce) produces a category index of the most likely matching category.

Consider a classification problem with 5 categories (or classes).

  • In the case of cce, the one-hot target may be [0, 1, 0, 0, 0] and the model may predict [.2, .5, .1, .1, .1] (probably right)

  • In the case of scce, the target index may be [1] and the model may predict: [.5].

Consider now a classification problem with 3 classes.

  • In the case of cce, the one-hot target might be [0, 0, 1] and the model may predict [.5, .1, .4] (probably inaccurate, given that it gives more probability to the first class)
  • In the case of scce, the target index might be [0], and the model may predict [.5]

Many categorical models produce scce output because you save space, but lose A LOT of information (for example, in the 2nd example, index 2 was also very close.) I generally prefer cce output for model reliability.

There are a number of situations to use scce, including:

  • when your classes are mutually exclusive, i.e. you don't care at all about other close-enough predictions,
  • the number of categories is large to the prediction output becomes overwhelming.

220405: response to "one-hot encoding" comments:

one-hot encoding is used for a category feature INPUT to select a specific category (e.g. male versus female). This encoding allows the model to train more efficiently: training weight is a product of category, which is 0 for all categories except for the given one.

cce and scce are a model OUTPUT. cce is a probability array of each category, totally 1.0. scce shows the MOST LIKELY category, totally 1.0.

scce is technically a one-hot array, just like a hammer used as a door stop is still a hammer, but its purpose is different. cce is NOT one-hot.

2 of 3
77

I was also confused with this one. Fortunately, the excellent keras documentation came to the rescue. Both have the same loss function and are ultimately doing the same thing, only difference is in the representation of the true labels.

  • Categorical Cross Entropy [Doc]:

Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided in a one_hot representation.

>>> y_true = [[0, 1, 0], [0, 0, 1]]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.  
>>> cce = tf.keras.losses.CategoricalCrossentropy()
>>> cce(y_true, y_pred).numpy()
1.177
  • Sparse Categorical Cross Entropy [Doc]:

Use this crossentropy loss function when there are two or more label classes. We expect labels to be provided as integers.

>>> y_true = [1, 2]
>>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>> # Using 'auto'/'sum_over_batch_size' reduction type.  
>>> scce = tf.keras.losses.SparseCategoricalCrossentropy()
>>> scce(y_true, y_pred).numpy()
1.177

One good example of the sparse-categorical-cross-entropy is the fasion-mnist dataset.

import tensorflow as tf
from tensorflow import keras

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

print(y_train_full.shape) # (60000,)
print(y_train_full.dtype) # uint8

y_train_full[:10]
# array([9, 0, 0, 3, 0, 2, 7, 2, 5, 5], dtype=uint8)