top of page

Python TensorFlow Tips -When to use Sparse Categorical Crossentropy vs Categorical Crossentropy loss

Updated: Aug 21, 2023

TensorFlow is a powerful framework for building neural networks. With TensorFlow we can build deep-learning models that predict either a continuous variable or a categorical value.


We have the choice of either using Sparse Categorical Crossentropy or Categorical Crossentropy.


Inside our model, it wouldn't make a difference for training only how we put the data into our model.



SparseCategoricalCrossentropy is used when we are concerned about the size if we were to one hot encoding the target before putting it into the model. So sparse is where the classes are in one column when we feed it into our model.


CategoricalCrossentropy is used when the labels are one-hot encoded and the model's output is also a probability distribution over the classes. This is the case when you have a small number of classes and the memory cost of one-hot encoding is not prohibitive.


In conclusion, you would typically use SparseCategoricalCrossentropy loss when your labels are represented as integers and CategoricalCrossentropy when your labels are one-hot encoded.








Follow Data Science Teacher Brandyn





dataGroups:












224 views0 comments
bottom of page