Is it ok to use categorical_crossentropy for binary classification or is it better to use binary_crossentropy


Binary cross-entropy is a special case of categorical cross-entropy with just 2 classes. So theoretically it does not make a difference.

If $y_k$ is the true label and $\hat{y}_k$ is the predicted label of class $k$ (both one-hot encoded, i.e. $\sum_k y_k =1 \land \sum_k \hat{y_k} =1 \land y_k,\hat{y_k} \in \{0,1\}$) the multicategorical cross-entropy for $K$ categories is $$-\sum_{k=1}^{K}y_k \log(\hat{y}_k)$$

which is equal to the binary cross-entropy for $K=2$ : $$-\sum_{k=1}^{K}y_k \log(\hat{y}_k) = -y \log(\hat{y})- (1-y) \log(1-\hat{y})$$

Just in case there are any implementation differences, e.g. speed-wise, I would still just use binary cross-entropy for a binary classification problem instead of multicategory with $K=2$.


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.