What are the actual class labels while using SparseCategoricalCrossEntropy loss for multiclass classification in keras?

Viewed 253

I am trying to use: [SparseCategoricalCrossEntropy][https://www.tensorflow.org/api_docs/python/tf/keras/losses/SparseCategoricalCrossentropy] for multiclass classification

enter image description here

This will give me the last dimension as the number of classes (N_CLASSES). But I want to retrive the actual class labels from the predictions.

Basically if I have 5 classes (N_CLASSES=5), then I have 5 columns, each containing the probability of the class. But I don't know which column belongs to which actual label. How do I retrieve the actual class labels ?

For example if I have my actual class labels as [1.03, 2.07, -2.09, -974, 366], then from the output of shape (None, 5) how do I know which column represents which class?

Note: I cannot use CategoricalCrossEntropy and pass in the one-hot encoded actual target representation due to memory issues.

Any help will be really appreciated

1 Answers

It is actually pretty simple. Let's assume your model outputs the predictions = [1.03, 2.07, -2.09, -974, 366]. These 5 numbers represent your model's confidence that your input data corresponds to each of the 5 different classes. If you then apply np.argmax to your predictions, which returns the index of the max value in predictions:

np.argmax(predictions)

you will get the index 4. Assuming that each label in your dataset is an integer between 0 and 4, and since you are using SparseCategoricalCrossEntropy, you can say that your model is most confident that your input data belongs to class 4 (whatever class 4 may be). I hope you get the idea.

Related