# Keras: How do I train a binary classification net where one class is much more frequent then the other?

I want to train a binary classification net (for NLP) where one class is much more frequent then the other (using Keras).

I have learned that in this case (one class is much more frequent then the other) it is not useful to talk about accuracy. It is more useful to talk about F1 score or to look at ROC curves or talk about the ROC AUC value.

The problem is that you can not directly optimize for those values (loss function can not be given). But what do I have to do in this case? What is the workflow to get a good F1 Score or high ROC AUC value?

Should I use class_weight argument of the fit function in Keras in this case?

• You can't directly optimize accuracy either, since the function is not continuous. So, how would you optimize the neural network weights when you are interested in maximizing accuracy? Most people would just use cross-entropy, whether measuring performance with accuracy, ROC AUC, or another non-differentiable metric.
– Sycorax
Sep 23, 2018 at 22:23

1. You can use the class_weight parameter. If you input the correct weights this will cause each example from the minority class to impact the loss more than one from the majority class. This in turn will cause the model to be trained well on both classes. To figure out the appropriate weights you can use scikit-learn's compute_class_weight function.