tensorflow version = '1.12.0'

keras version = '2.1.6-tf'

I'm using keras with tensorflow backend.

I want to get the probabilities values of the prediction. I want the probabilities to sum up to 1. I tried using 'softmax' and 'categorical_crossentropy' but nothing works.

This is my model:

X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))

number_of_gestures = 5 
y = to_categorical(y, num_classes=number_of_gestures) #to_categorical is a function from keras - np_utils.

model = Sequential()
model.add(Conv2D(16, (2,2), input_shape=(IMG_SIZE, IMG_SIZE, 1), activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

model.add(Conv2D(32, (5,5), activation='relu'))

model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))

model.add(Conv2D(64, (5,5), activation='relu'))


model.add(Dense(128, activation='relu'))


model.add(Dense(number_of_gestures, activation='softmax'))

sgd = optimizers.SGD(lr=1e-2)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit(X, y, batch_size=500, epochs=40, validation_split=0.1)

The probabilities looks like that:

[1. 0. 0. 0. 0.]

And I want it to look like this:

[0.897. 0.023. 0.158. 0.780. 0.1021]

I know it does not sum up to 1 but this is just an example.

  • 1
    $\begingroup$ A couple of questions: When you say that "the probabilities look like this", how do you get them right now? What is the size of your datasets? $\endgroup$ – Mark.F Feb 12 '19 at 7:25
  • $\begingroup$ Can you please provide the code for your prediction cycles? The output that you're posting is quite telling. It's possible that you're already getting the probabilities you want but your prediction code is using some sort of argmax() function that is creating the [1,0,0,0] output. $\endgroup$ – I_Play_With_Data Feb 12 '19 at 22:52

I faced such a problem using CNN in Keras. Even, I thought that the output is being processed in the such a way like the argmax().

  • But, after investigating the model, I found that the model are learned and generalised so well that the labels got binarized ( 0s and 1s ). Remember, that we give the label of an image as a one hot vector ( like [ 1 0 0 0 ] ). The NN learned it so well that the output, usually being a probability vector , was now a one hot vector on which it was trained.

  • Another way to prove this is that you are using a softmax activation function at the output layer. A softmax function produces probabilities which sum up to 1. The sum of all class probabilities is 1. In the vector [ 1 0 0 0 0 ]. The sum of all numbers is 1. Hence the output is valid.

This is just a different phase in training your model. You can try to increase the size of your dataset or lower the learning rate to 0.0001 or even smaller.


My guess is that you are calling predict when you need to call predict_proba.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.