sparse_categorical_crossentropy is a classification loss for models whose targets are integer class labels. During training, it supplies the feedback signal used to adjust weight tensors and reduce prediction error through gradient descent. section 2.5 section 8.1
MNIST classification
A 10-output Dense layer with softmax predicts handwritten digits numbered 0 through 9. Because the MNIST labels are integers, compile the model with loss="sparse_categorical_crossentropy". Keras then uses that loss during mini-batch training with the Adam optimizer. section 8.1 section 2.5
model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], )
The compile call shows the loss in its practical setting: paired with Adam and accuracy before fitting the model. section 2.5
Use this rule of thumb:
- Choose the sparse form when each target is represented by an integer class index rather than a one-hot vector. section 8.1
- Pair it with a softmax output for multi-class classification, such as the ten-way MNIST classifier. section 8.1
