Mastering Focal Loss: Tackling Imbalanced Text Classification Like a Pro!

A Tale of Binary Cross-Entropy and the Problem That Wouldn’t Quit

Vinit_chavan
4 min readDec 4, 2024

Once upon a time in the mystical land of Machine Learning, a young data scientist (that’s me!) was tasked with solving a problem. It wasn’t just any problem — it was a text classification problem with 19 labels. And here’s the kicker: some labels were party animals (tons of samples), while others were loners (barely any samples). The data imbalance was real, folks, and it made every metric I cared about — precision, recall, and my self-esteem — suffer.

Like any good data scientist, I turned to Binary Cross-Entropy (BCE) for help. BCE is a trusty companion for multi-label classification, but even it struggled under the weight of this imbalance.

What Is Binary Cross-Entropy Anyway?

Let’s break it down in a way everyone can understand: imagine BCE as a strict schoolteacher. For every label, it checks how wrong your prediction was and gives you a penalty. The closer you are to being right, the smaller the penalty. The formula looks something like this:

Where:

  • yi​ is the true label (1 or 0),
  • y^​i​ is the predicted probability (between 0 and 1),
  • N is the number of samples.

Simple, right? But there’s a catch: BCE treats every misprediction the same way, whether it’s for the most common label or the rarest one. It’s fair but not very street-smart. And this is where the imbalance issue hits us like a ton of bricks.

Enter Focal Loss: The Hero We Deserved

Now imagine you’re organizing a party (a classification task), and some guests (labels) always RSVP while others rarely show up. BCE doesn’t care about this, but Focal Loss does! It’s like that one friend who checks in on introverts to make sure they feel included.

Focal Loss extends BCE by focusing more on hard-to-classify samples (the rare ones). Its secret weapon is a modulating factor that downplays the easy cases and amplifies the hard ones. Here’s the formula:

It’s like giving extra homework to the students who need it most.

Our Imbalanced Text Classification Problem

So, back to our problem: we had 19 labels with wildly different frequencies. Imagine a zoo where lions (common labels) outnumber pandas (rare labels) 100 to 1. BCE was failing because it couldn’t give enough love to the pandas.

Here’s a snippet of the dataset imbalance:

Here’s a snippet of the dataset imbalance:

Using BCE, our model would happily classify everything as lions and still achieve decent accuracy. But we knew better.

The Focal Loss Solution

We implemented Focal Loss with γ = 2 and α = 0.25. The results? Pandas, penguins, and koalas finally got the attention they deserved. Here’s how the magic happened:

import tensorflow as tf

def focal_loss(alpha=0.25, gamma=2.0):
def loss_fn(y_true, y_pred):
# Clip predictions to avoid log(0)
y_pred = tf.clip_by_value(y_pred, 1e-7, 1 - 1e-7)
focal_loss = -alpha * (1 - y_pred) ** gamma * y_true * tf.math.log(y_pred) \
- (1 - alpha) * y_pred ** gamma * (1 - y_true) * tf.math.log(1 - y_pred)
return tf.reduce_mean(focal_loss)
return loss_fn

# Compile the model
model.compile(optimizer='adam', loss=focal_loss(alpha=0.25, gamma=2.0), metrics=['accuracy'])

The Results

After implementing Focal Loss, we saw a significant improvement in metrics like recall for minority classes. The pandas, penguins, and koalas rejoiced!

  • Baseline (BCE): Precision: 0.70, Recall: 0.50
  • With Focal Loss: Precision: 0.75, Recall: 0.68

Lessons Learned

  1. Imbalance is a bully, but focal loss fights back. It helps the underrepresented classes without overwhelming the model.
  2. Experimentation is key. Tuning α\alphaα and γ\gammaγ made all the difference.
  3. Metrics matter. Focal Loss showed its real power when we measured class-wise recall.

Takeaways for You

If you’re wrestling with imbalanced data in multi-label text classification, Focal Loss might be your new best friend. It’s easy to implement, powerful, and makes your model fairer and more effective.

A Fun Recap

Think of BCE as a referee who treats everyone equally, and Focal Loss as a coach who spends extra time with the players who need it most. Together, they can take your models from decent to dazzling.

Have you faced similar challenges in your ML journey? Share your story in the comments! 🚀

Applications of Focal Loss

Focal loss has proven to be a game-changer in many scenarios involving imbalanced datasets. Some of its key applications include:

  1. Object Detection.
  2. Medical Image Classification.
  3. Fraud Detection.
  4. Text Classification.

Other Loss Functions for Imbalanced Datasets

While focal loss is exceptional, there are other loss functions you can experiment with for handling imbalanced data:

  1. Weighted Cross-Entropy Loss:
  • Assigns higher weights to underrepresented classes, ensuring the model focuses more on them.
  • Example: BinaryCrossentropy(weighted=True) in TensorFlow/Keras.

2. Hinge Loss:

  • Useful for imbalanced data in SVMs. It focuses on maximizing the margin between classes, reducing the effect of imbalance.

3. Dice Loss:

  • Commonly used in segmentation tasks, it measures overlap between predicted and true classes, which can be helpful for imbalanced datasets.

5. Tversky Loss:

  • A generalization of Dice Loss lets you adjust weights for false positives and false negatives, making it effective for imbalanced datasets.

4. Log-Cosh Loss:

  • Combines the strengths of MSE and MAE, reducing sensitivity to outliers, which is useful for datasets with a skewed distribution.

Let’s solve imbalance together — one model at a time! 🚀

--

--

Vinit_chavan
Vinit_chavan

Written by Vinit_chavan

I am a Data Scientist, with the skills to look at problems in a solution way. Having a master's degree in mathematics, Quite optimistic about decisions for BI.

Responses (1)