Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access class probabilities in keras?

Tags:

python

keras

I am training a model for which I need to report class probabilities instead of a single classification. I have three classes and each training instance has either of the three classes assigned to it.

I am trying to use Keras to create an MLP. But I can't figure how to extract the final class probabilities for each class. I am using this as my base example: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

Thanks !

like image 686
silent_dev Avatar asked Feb 10 '17 12:02

silent_dev


People also ask

What is Predict_classes?

predict_class will return the index of the class having maximum value. For example, if cat is 0.6 and dog is 0.4, it will return 0 if the class cat is at index 0)


2 Answers

In order to perform multi-class classification (nb_classes > 1) you have to prepare your model in a specific manner.

  1. Make sure your labels are well-designed for multi-class classification. Have a look at the numpy_utils
  2. You have to use the categorical_crossentropy as objective function for multi-class classification (see Keras objectives)
  3. Your last layer must have the softmax activation function (guarantees the output to be between 0 and 1) and nb_classes neurons.
  4. Train your model as usual
  5. Use the predict function. You will receive a vector of size (nb_classes,1) containing the probabilities of each class.
like image 154
D.Laupheimer Avatar answered Oct 12 '22 03:10

D.Laupheimer


You could use the predict method of your trained model

predict

predict(self, x, batch_size=32, verbose=0)

Generates output predictions for the input samples, processing the samples in a batched way.

Arguments

x: the input data, as a Numpy array (or list of Numpy arrays if the model has multiple outputs). batch_size: integer. verbose: verbosity mode, 0 or 1.

Returns a Numpy array of predictions.

model.predict(input_to_your_network)
like image 24
maz Avatar answered Oct 12 '22 03:10

maz