Keras image binary classification which class is assigned probability 0 and 1 Using Functional API

0 votes

When implementing binary classification, the sigmoid function is used as activation for the last layer by majority of the people. As I understand the sigmoid function gives a probability between 0 and 1 and we set a threshold value (mostly 0.5) to determine the class label.

However I'm confused as to which class does is that probability for ? Say I have two classes A and B and I get prediction result p or p% . Is that the probability of being class A or the probability of not being class A (i.e B).

I apologize if this has been asked here before but I couldn't find it. Since I'm using Keras Functional API i cannot use the predict_classes() function. Also I mostly use generators for loading my dataset mostly flow_from_dataframe() from ImageDatagenerator in which you can just provide the class labels or class list. In my case it's two strings in my dataframe "REAL" or "FAKE" .

Also is there a way to set which class I want the probability for ?

Mar 4, 2022 in Machine Learning by Nandini
• 5,480 points
1,480 views

1 answer to this question.

0 votes
Not exactly.  If your sigmoid output was 0.8, that doesn't mean your network produced probabilities for each class as sigmoid outputs don't add up to 1. In other words, an output of 0.8 does not imply that it is likely to belong to other classes with a probability of 0.2.

Also, with sigmoid network outputs p(y=1) in Binary Classification. Then p(y=0) = 1 - p(y=1) by definition of probability. Only for basic binary categorization, they add up to one.

Softmax activation should be used to see the probability of each class because its output will total to 1. Softmax outputs can be interpreted as probabilities.

These models, on the other hand, are deterministic rather than probabilistic. As a result, it is customary to interpret softmax results as probabilities, but there is no mathematical connection between them.

Is there a way to specify which class the probability applies to?

You can choose from a variety of thresholds, the most common of which is 0.5, but this is dependent on your data and situation. You can adjust the threshold to observe how it affects the AUC-ROC, and then interpret the results to find the ideal threshold for you.

If you wish to decide the classes, you can use the following formula, keeping in mind that the threshold is 0.5 and that you can adjust it:

predicted_classes = [1 * (x[0]>=0.5) for x in preds_sigmoid]

If the output is greater than 0.5, it is classified as second class.
answered Mar 7, 2022 by Dev
• 6,000 points

Related Questions In Machine Learning

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

Getting one word as caption with zero probability using pretrained checkpoints for image captioning-im2txt

Under caption_generator.py change the following line as follow ...READ MORE

answered Feb 27, 2022 in Machine Learning by CoolCoder
• 4,400 points
291 views
0 votes
1 answer

What is correlation and its types?

Correlation is a statistical measure that shows ...READ MORE

answered May 10, 2019 in Machine Learning by Zulaikha
2,102 views
0 votes
1 answer

Where is the Keras configuration file stored?

Hi@akhtar, The Keras configuration file is a JSON ...READ MORE

answered Jul 15, 2020 in Machine Learning by MD
• 95,440 points
729 views
0 votes
1 answer
+1 vote
1 answer

Index of predicted wrong data in Keras, how to find it?

Simply, use: model.predict() pred = model.predict(x_test) indices = [i for ...READ MORE

answered Sep 28, 2018 in Python by Priyaj
• 58,090 points
5,273 views
0 votes
2 answers
0 votes
1 answer

What is the difference between classification and prediction?

Classification is about classifying categorical variables in ...READ MORE

answered Feb 25, 2022 in Machine Learning by Dev
• 6,000 points
992 views
0 votes
1 answer

Getting one word as caption with zero probability using pretrained checkpoints for image captioning-im2txt

captiongenerator.py is a Python script that generates ...READ MORE

answered Mar 17, 2022 in Machine Learning by Dev
• 6,000 points
274 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP