IM-Context: In-Context Learning for Imbalanced Regression Tasks

Ismail Nejjar, Faez Ahmed, Olga Fink

Abstract

Regression models often fail to generalize effectively in regions characterized by highly imbalanced label distributions. Previous methods for deep imbalanced regression rely on gradient-based weight updates, which tend to overfit in underrepresented regions. This paper proposes a paradigm shift towards in-context learning as an effective alternative to conventional in-weight learning methods, particularly for addressing imbalanced regression. In-context learning refers to the ability of a model to condition itself, given a prompt sequence composed of in-context samples (input-label pairs) alongside a new query input to generate predictions, without requiring any parameter updates. In this paper, we study the impact of the prompt sequence on the model performance from both theoretical and empirical perspectives. We emphasize the importance of localized context in reducing bias within regions of high imbalance. Empirical evaluations across a variety of real-world datasets demonstrate that in-context learning substantially outperforms existing in-weight learning methods in scenarios with high levels of imbalance.

Introduction

In many real-world regression tasks—ranging from age estimation in computer vision to rare engineering failure prediction—data is inherently imbalanced. Standard deep learning models typically rely on "in-weight" learning, where internal parameters are updated via gradient descent to minimize global error. This process naturally biases the model toward the majority classes, often causing it to overfit or entirely ignore the "tail" regions where data is scarce. While techniques like sample re-weighting or synthetic data generation exist, they frequently struggle to handle the continuous nature of regression labels.

IM-Context introduces a paradigm shift by replacing traditional weight updates with In-Context Learning (ICL). By leveraging pre-trained Transformers, the model generates predictions by conditioning on a small, carefully curated set of example neighbors (the prompt) rather than through parameter optimization. This approach effectively bypasses the overfitting issues that plague standard models in data-sparse regions.

The IM-Context Framework

The core idea of IM-Context is to utilize the inherent pattern-matching capabilities of Transformers. Instead of training a model on a specific imbalanced dataset, we provide a pre-trained model with a prompt consisting of input-label pairs $(x, y)$ from the training set, followed by the target query $x_{query}$.

Localized Context and Augmented Retrieval

Two key technical innovations ensure the effectiveness of this approach in imbalanced scenarios:

  1. Localized Context: Theoretical analysis and empirical evidence suggest that in imbalanced regression, "more context is not always better." In minority (few-shot) regions, adding random context samples can inadvertently bias the model toward the majority mean. IM-Context restricts the prompt to the $k$ nearest neighbors of the query, ensuring the model focuses on relevant local relationships.
  2. Augmented Retrieval (Inverse Density Sampling): Standard nearest-neighbor retrieval can still be biased because majority samples are statistically more likely to be nearby in the feature space. To counteract this, the researchers employ an "inverse density" sampling strategy. They create a secondary training set where minority regions are overrepresented. The final prompt is a balanced mixture of neighbors from the original dataset and this inverse set, providing the model with a balanced "local" view of the data distribution.

Theoretical Analysis: The U-Shaped Error Curve

A critical insight of this work is the discovery of the U-shaped error curve in few-shot regions. As the number of context samples in the prompt increases, the error initially decreases as the model gains more information. However, beyond an optimal point, the error begins to rise. This occurs because, in imbalanced datasets, additional context samples are increasingly likely to come from majority regions, re-introducing the very bias the method aims to avoid.

Theoretical error bounds and target value distributions across many-shot, medium-shot, and few-shot regions.

As shown in the theoretical bounds, the "Few shot region" (orange line) exhibits a sharp increase in the error bound as context size grows, contrasting with the "Many shot region" (green line) where error remains low and stable. This justifies the necessity of localized, restricted context sizes for imbalanced tasks.

Empirical Evaluation

Datasets and Benchmarks

The researchers evaluated IM-Context across three distinct modalities to prove its generalizability:

  • Computer Vision: AgeDB-DIR and IMDB-WIKI-DIR for age estimation from facial images.
  • Natural Language Processing: STS-B-DIR for semantic text similarity.
  • Tabular Data: Six diverse datasets including housing prices (Boston), material strength (Concrete), and engineering design (Airfoil).

Performance Analysis

IM-Context consistently outperformed state-of-the-art in-weight learning methods, particularly in the tail regions. On the AgeDB-DIR benchmark, the method achieved a Mean Absolute Error (MAE) of 7.83 in the few-shot region, a significant leap over the previous best of 9.21. Similar gains were observed in NLP, where MSE improved from 0.670 to 0.566 in rare regions.

Empirical error comparison between in-context learning and simple averaging across different context sizes for Boston and AgeDB datasets.

The empirical results on real-world datasets like Boston and AgeDB confirm the theoretical U-shape. In-context learning (solid lines) significantly outperforms simple averaging (dashed lines), especially in the few-shot regions (orange), demonstrating that the Transformer acts as a sophisticated interpolator that understands complex local feature-label relationships.

Discussion and Future Work

While IM-Context offers a powerful alternative for imbalanced regression, it introduces new trade-offs. Inference Latency is higher than standard multi-layer perceptrons because the model must process the context samples for every new query. Furthermore, the method's success is tied to the quality of the feature embeddings; if the initial representation (e.g., from CLIP or BERT) is poor, the retrieved context will be less effective.

Future research will focus on extending this framework to multi-dimensional outputs and optimizing retrieval mechanisms to further reduce the impact of global imbalance on local context selection. By treating imbalanced regression as an in-context learning problem, IM-Context provides a robust path forward for deep learning in data-constrained environments.

Summary updated

BibTeX
@article{nejjar2024im,
  author = {Ismail Nejjar and Faez Ahmed and Olga Fink},
  title = {{IM-Context: In-Context Learning for Imbalanced Regression Tasks}},
  journal = {Accepted by TMLR},
  year = {2024},
  url = {https://openreview.net/forum?id=p4Y844vJWG},
}
Figure