3
$\begingroup$

We can predict the class for new data instances using the Sequential classification model in Keras using the predict_classes() function. What is the way to predict the class for models that developed using the functional API?

For example, I have a model (functional API based) with sigmoid activation on the last layer to get probabilities in a multi-label classification. When I apply model.predict(), I got a series of probabilities even though the loss is binary_crossentropy.

I understand that I can do this classification manually e.g. following approach.

test_predict_proba = model.predict(x_test, batch_size=batch_size)
class_predict = (test_predicted_proba > 0.5).astype(int)

I am wondering is there any standard procedure to do the same?

$\endgroup$

2 Answers 2

0
$\begingroup$

Is this a multi-label problem or a pure classification problem? If it is just classification change the activation function in the final layer to softmax. When you do predictions select the output with the highest probability as the class. Alternatively use model.evaluate(.....). versus model.predict Documentation is here,

$\endgroup$
1
  • $\begingroup$ It's a multi-label classification problem. $\endgroup$ Commented Apr 29, 2020 at 5:33
0
$\begingroup$

The functional API does not change how you can call the predict function. The output shape does not depend on the loss function. You should look at the output layer and check its shape. Multi-label classification should indeed give you a series of probabilities, one for each of the classes you are predicting. So if there is 5 classes, you would get 5 probabilities.

The threshold is up to the modeler. You can choose 0.5 as a rule of thump, but that is not necessarily the best, as there is a trade off between precision and recall. StatQuest has an excellent video about this > https://youtu.be/4jRBRDbJemM

If you are interested in more in-depth details about choosing the threshold, you can check out the Bayesian Decision Theory, nicely explained in Kevin Murphy's "Machine Learning: a probabilistic perspective" section 5.7.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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