keras: Will Keras output non-binary (continuous) predictions for a classification problem?

My current model is compiled as

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

and I get predictions like

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

Instead I’d like my class predictions to show a percentage

 [[ 0.8  0.1  0.1  0.4]]

Elsewhere I was advised to use loss='mse', but this was discouraged by another user. This issue does not seem addressed in the docs (I’d be glad to add to docs any insight you can give).

Any advice on what is best practice using Keras?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 8
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@mptorr I have the same problem. model.predict() returns predictions (zeros and ones) and not the probability (although every document on Keras states the opposite). Even model.predict_proba() is returning predictions.

If you’re using predict() to generate your predictions, you should already get probabilities (provided your last layer is a softmax activation), nothing to do with the loss function. Are you sure you’re using predict(), not predict_classes()?

Just change the data types to float32 and normalize it the following way then the results will give the probabilities Data = X_train.astype(‘float32’) Data /=255 … model.predict_proba(Data, verbose=1)

try normalization on your test set and you may get the probabilities.

In my case, model.predict() and model.predict_proba() both give probabilities. model.predict_classes() gives binary responses

@mbollmann I check the predict_classes. default I don’t use the function of predict_classes and use the predict function Then i don’t know what does the predict_classes return. Does it be the format,like this.

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

I also don’t know when use the function of predict_classes? Thanks.