4

As an input a have a float 1.0 or 0.0. When I try to predict with my model and the sparse_categorical_crossentropy loss I get something like: [[0.4846592 0.5153408]].

How do I know what category it predicts?

nbro
  • 15,395
  • 32
  • 113
  • 196
user9468014
  • 479
  • 2
  • 6
  • 20

1 Answers1

8

These numbers you see are the probability of each class for the given input sample. For example, [[0.4846592 0.5153408]] means that the given sample belongs to class 0 with probability of around 0.48 and it belongs to class 1 with probability of around 0.51. So you want to take the class with the highest probability and therefore you can use np.argmax to find which index (i.e. 0 or 1) is the maximum one:

import numpy as np

pred_class = np.argmax(probs, axis=-1) 

Further, this has nothing to do with the loss function of the model. These probabilities are given by the last layer in your model which is very likely that it uses softmax as the activation function to normalize the output as a probability distribution.

today
  • 32,602
  • 8
  • 95
  • 115
  • I know this is an old question, but how do you know which category name is 'class 0' referred to? Is there something like OHE.categories_ from the One Hot Encoder from sklearn? – alan.elkin Mar 09 '20 at 22:02
  • 1
    @alan.elkin In case of using `sparse_categorical_crossentropy`, the category you have assigned to number 0 is actually class 0; the category you have assigned to number 1 is actually class 1; and so on. For example, if you use [LabelEncoder](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html) from sklearn, you can find out this mapping via `.classes_` attribute (see the documentation and examples). – today Mar 10 '20 at 09:19