Keras softmax activation, category_crossentropy loss. But output is not 0, 1

user6101147 picture user6101147 · Aug 24, 2017 · Viewed 13.1k times · Source

I trained CNN model for just one epoch with very little data. I use Keras 2.05.

Here is the CNN model's (partial) last 2 layers, number_outputs = 201. Training data output is one hot encoded 201 output.

model.add(Dense(200, activation='relu', name='full_2'))
model.add(Dense(40, activation='relu',  name='full_3'))
model.add(Dense(number_outputs, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])

The model is saved to a h5 file. Then, saved mode is loaded with same model as above. batch_image is an image file.

prediction = loaded_model.predict(batch_image, batch_size=1)

I get prediction like this:

ndarray: [[ 0.00498065  0.00497852  0.00498095  0.00496987  0.00497506  0.00496112
   0.00497585  0.00496474  0.00496769  0.0049708   0.00497027  0.00496049
   0.00496767  0.00498348  0.00497927  0.00497842  0.00497095  0.00496493
   0.00498282  0.00497441  0.00497477  0.00498019  0.00497417  0.00497654
   0.00498381  0.00497481  0.00497533  0.00497961  0.00498793  0.00496556
   0.0049665   0.00498809  0.00498689  0.00497886  0.00498933  0.00498056

Questions:

  1. Prediction array should be 1, 0? Why do I get output like output activate as sigmoid, and loss is binary_crossentropy. What is wrong? I want to emphasize again, the model is not really trained well with data. It's almost just initialized with random weights.

  2. If I don't train the network well (not converge yet), such as just initializing weights with random number, should the prediction still be 1, 0?

  3. If I want to get the probability of prediction, and then, I decide how to interpret it, how to get the probability prediction output after the CNN is trained?

Answer

Sargam Modak picture Sargam Modak · Aug 24, 2017

Your number of output is 201 that is why your output comes as (1,201) and not as (1,0). You can easily get which class has the highest value just by using np.argmax and that class is the output for your given input by your model.

And for the fact even when you have trained for 1 epoch only, your model has learned something that may be very lame, but still, it learns something and based on that, it has predicted the output.

You have used softmax as your activation in the last layer. It normalizes your output in a non-linear fashion so that the sum of output for all classes is equals to 1. So the value you get for each class can be interpreted as the probability of that class as output for the given input by the model. (For more clarity, you can look into how softmax function works)

And lastly, each class has values like 0.0049 or similar because the model is not sure which class your input belongs to. So it calculates values for each class and then softmax normalizes it. That is why your output values are in the range 0 to 1.

For example, say I have four class so one of the probable output can be like [0.223 0.344 0.122 0.311] which in the end we look as a confidence score for each class. And by looking at confidence score for each class we can say the predicted class is 2 as it has the highest confidence score of 0.344.