5
$\begingroup$

I want to create a deep learning model (CNN) for binary classification, can I used the softmax function instead of the sigmoid function in binary classification?

Adding the classification layer to the model, will be like this

model.add(Dense(2, activation='softmax'))
$\endgroup$

2 Answers 2

5
$\begingroup$

For binary classification, it should give the same results, because softmax is a generalization of sigmoid for a larger number of classes.

$\endgroup$
5
$\begingroup$

The answer is not always a yes. You can always formulate the binary classification problem in such a way that both sigmoid and softmax will work. However you should be careful to use the right formulation.

Sigmoid can be used when your last dense layer has a single neuron and outputs a single number which is a score. Sigmoid then maps that score to the range [0,1]. You can then assume that this is a probability distribution and say that the prediction is class 1 if the probability is larger than 0.5 and class 0 other wise.

If you want to use softmax, you need to adjust your last dense layer such that it has two neurons. It must output two numbers which corresponds to the scores of each class, namely 0 and 1. Now, you can use softmax to convert those scores into a probability distribution. Finally, to get the predicted label, you still need to find the argmax in the probability distribution.

You can not use softmax when you have a single neuron in the last layer. This will lead to some strange behaviour and performance will drop. Obviously, you can also not use sigmoid when you formulate the problem with two dimensional last layer.

So it is either

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

or

model.add(Dense(1, activation='sigmoid'))
$\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.