Class-aware contrastive optimization for imbalanced text classification

Grigorii Khvatskii, Nuno Moniz, Khoa D. Doan, Nitesh V. Chawla

Abstract

The unique characteristics of text data make classification tasks a complex problem. Advances in unsupervised and semi-supervised learning and autoencoder architectures addressed several challenges. However, they still struggle with imbalanced text classification tasks, a common scenario in real-world applications, demonstrating a tendency to produce embeddings with unfavorable properties, such as class overlap. In this paper, we show that leveraging class-aware contrastive optimization combined with denoising autoencoders can successfully tackle imbalanced text classification tasks, achieving better performance than the other strong text classification models. Concretely, our proposal combines reconstruction loss with contrastive class separation in the embedding space, allowing a better balance between the truthfulness of the generated embeddings and the model’s ability to separate different classes. Compared with an extensive set of traditional and deep learning based competing methods, our proposal demonstrates a notable increase in performance across a wide variety of text datasets.

Introduction

In real-world applications—ranging from spam filtering to fraud detection—data is rarely balanced. These scenarios often present thousands of "normal" examples but only a handful of "target" examples, creating a significant class imbalance problem. Traditional Natural Language Processing (NLP) models typically generate text embeddings using unsupervised methods like autoencoders. While effective at capturing semantic meaning, these models often ignore class labels during embedding generation. In imbalanced contexts, this leads to "class overlap," where minority class examples are subsumed within the majority class cluster, making it difficult for classifiers to establish clear boundaries.

To address this, we introduce CAROL (Class-awARe cOntrastive Loss), a novel optimization strategy designed to force better class separation in the embedding space. By balancing traditional reconstruction loss with a contrastive term, CAROL achieves state-of-the-art performance on imbalanced datasets.

The CAROL Framework

The proposed framework integrates the TSDAE (Transformer-based Sequential Denoising Auto-Encoder) architecture with the CAROL loss function. The core innovation lies in the Total Loss calculation, which is a weighted sum of two distinct components:

  1. Reconstruction Loss ($L_{Recon}$): This ensures the model captures semantic content by reconstructing a sentence from a noisy version of itself.
  2. CAROL Loss ($L_{CAROL}$): A supervised contrastive loss that optimizes for class separation. It calculates the average distance between pairs of the same class (intraclass) and subtracts the average distance between pairs of different classes (interclass).
The CAROL training pipeline combines reconstruction loss from a denoising autoencoder with a supervised contrastive loss for class separation.

The model utilizes a hyperparameter, $C$, to balance these objectives. When $C=0$, the model functions as a standard TSDAE focusing solely on reconstruction. When $C=1$, the model prioritizes class separation. A value of $C=0.5$ provides an equal weight, ensuring embeddings are both semantically truthful and discriminative. Notably, CAROL is distance-agnostic and has been validated using Euclidean, Cosine, and Chebyshev metrics.

Experimental Evaluation

The effectiveness of CAROL was evaluated across six diverse English-language datasets with imbalance ratios ranging from approximately 4:1 to 9:1, including 20News, BBC news articles, and SMS Spam messages.

Key Results

CAROL demonstrated a notable performance increase across all datasets, particularly in the F1 Score, which is critical for imbalanced tasks.

  • Comparison with Large Language Models: In few-shot settings, CAROL (built on a BERT-base model) outperformed GPT-3.5 and GPT-4. For instance, on the BBC dataset, CAROL achieved an F1 score of 0.981, significantly higher than GPT-4's 0.600.
  • Average Performance: Across all datasets, CAROL showed an average improvement of 4.1% over the strongest baseline methods.
  • Precision and Recall: The model maintained high precision even under adverse conditions, reducing the likelihood of "hallucinating" minority class labels.

Discussion

Visualizations using t-SNE reveal that while standard methods often produce overlapping "blobs" of classes, CAROL effectively "shatters" these into distinct islands. The optimal value for the $C$ parameter was found to be domain-dependent; some datasets benefited more from reconstruction (e.g., InsightCrime), while others required heavy class separation (e.g., India Police). This suggests that the ideal embedding space geometry is specific to the nature of the text data.

To maintain computational efficiency, the authors implemented a sampling strategy. Calculating contrastive loss for every possible pair is an $O(n^2)$ operation, but sampling allows for an approximation that keeps computation time constant regardless of dataset size.

Limitations and Future Work

While CAROL is highly effective, several areas for future research remain. The current study is limited to English-language datasets and binary classification; extending it to multi-class problems increases complexity significantly. Additionally, users must currently perform cross-validation to find the optimal $C$ value for their specific domain, and the contrastive component still requires a sufficient amount of labeled data to be effective.

Summary updated

BibTeX
@inproceedings{khvatskii2025class,
  author = {Grigorii Khvatskii and Nuno Moniz and Khoa D. Doan and Nitesh V. Chawla},
  title = {{Class-aware contrastive optimization for imbalanced text classification}},
  booktitle = {Discover},
  year = {2025},
  doi = {10.1007/s44248-025-00064-0},
}
Figure