Concepts / Class Imbalance in Training Data

Class Imbalance in Training Data

Data coverage is about whether the training data spans the kinds of inputs and behaviors a model will meet, not only how much data exists.

  • Programming

Why Having Enough Data Is Not Enough

When people judge a training dataset, they often start with a simple question: how much data is there? Size matters, but it is not the whole story. Data coverage is about whether the training data spans the kinds of inputs and behaviors a model will meet, not only how much data exists. A dataset can be large and still leave whole areas of the problem untouched.

This article follows a model through a small thought experiment. You will trace what the model has seen, find where its experience runs out, and then see how new data can be generated to extend that experience. Along the way you will also meet class imbalance, the situation where one label has many examples and another has very few.

What do you think happens?

A team has collected 1,000 customer messages to train a model. Almost all messages are of middle length, a few are very short, and none are very long. What has the model learned about?

  • Messages of every length equally well

  • Only middle-length messages, with very short and very long regions left empty

  • Only very long messages, because they carry the most information

Reveal answer
Answer:

Only middle-length messages, with very short and very long regions left empty

The model can only learn from what it sees. Since the very short and very long regions of the input space are empty in the data, the model has no direct experience with them, and its behavior there is hard to predict.

Tracing the Coverage Gaps in Message Length

Start with the team's 1,000 customer messages. When they sort the messages by length, the pattern is clear: most fall in the middle range, a few are very short, and none are very long. Each time the team asks what the model has learned, the honest answer is that it has learned about middle-length messages. The very short and very long regions are empty.

Training dataset1,000 customer messagesMiddle-lengthmessageswell representedModel's learnedexperiencemiddle-length onlyVery short messagesonly a few examplesVery long messagesnone in the datacoverssparseabsentlearned from
Which regions of the full range of possible message lengths does the training data cover, and which regions are empty?

Notice what this walkthrough reveals. The coverage gap is defined by a characteristic of the inputs, here their length, rather than by the total count. A dataset of a thousand examples can still have a hole in it. Improving coverage therefore means finding the specific characteristics that are missing and adding data that carries them.

Worked example

Finding the gap in a support-chat classifier

A support-chat classifier is trained mostly on messages of ordinary length. Where are its gaps, and what would it struggle with?

  1. 1
    Name the characteristic

    The relevant characteristic here is message length, because the training data was collected and sorted by length.

  2. 2
    Check the regions

    Ask whether very short and very long messages appear in the data. If a region is empty or nearly empty, the model has little direct experience there.

  3. 3
    Predict the weakness

    The model may do well on typical messages and still struggle when a customer sends a single word or a long, winding complaint.

  4. 4
    Locate the problem correctly

    The problem is not that the data is wrong. The data simply does not cover the ground the model will need to walk.

Result

The gaps lie at the extremes of message length. The fix is to add data with those characteristics, not simply to add more ordinary messages.

Filling Gaps with Targeted Generation

The question this article asks is practical: when the data you already have does not cover an area well enough, can you create new data that does? The source answers yes. Data synthesis generates data with targeted characteristics, either to improve performance or to get a model to express specific behaviors. The method is to choose the characteristic first and then generate data that carries it.

Gap specificationfor example, very longmessagesTarget characteristiclength, toxicity,consistencyGeneratorproduces data toward thedescriptionSynthetic examplescarry the characteristicTraining datasetgap now coveredname the missing kinddescribe the targetgenerateadd
How do targeted characteristics flow from a gap specification through a generator into the training dataset?

The source gives two concrete illustrations of this targeting. In the first, you generate very short texts or very long texts, which fills the length gap from the previous section. In the second, you create conversations that contain toxic phrases for a toxic detection model, so that the detector sees the kind of content it must recognize.

Targeting also works in the opposite direction. The source notes that if real-world data is toxic, you can synthesize safe data instead. The goal is then not more examples of a problem but examples of the desired behavior that the real data lacks. Whether you want more of a characteristic or less of it, the underlying move is the same: decide what the data should look like and generate toward that description.

Expressing a Behavior Versus Challenging a Model

Synthetic data can be used for two different purposes that are easy to confuse. In one, the data is built to show a model a behavior you want it to express. In the other, the data is built to challenge the model, and such data is called an adversarial example. The source defines an adversarial example as synthetic data designed to challenge a model's performance, and notes that AI is commonly used to produce them.

AspectSynthetic data expressing a behaviorAdversarial examples
PurposeGet a model to express a specific behaviorTest and expose a model's weaknesses
Targeted characteristicThe desired behavior or the characteristic to add, such as safe content or a missing lengthDifficulty for the model, built to be hard in a way that exposes weaknesses
What the data looks likeExamples that carry the wanted behaviorExamples deliberately built to be hard for the model
What it tells you about the modelWhether the model can produce or recognize the behaviorHow robust the model is under pressure
Source exampleSafe data synthesized to replace toxic real data, or model-written datasets that test 154 AI behaviorsFactually inconsistent summaries generated with large language models to train detectors of factual inconsistency

Two purposes for synthetic data, as the source describes them

A concrete case from the source makes the adversarial idea clearer. Gekhman et al. (2022), in their work on TrueTeacher, used large language models to generate factually inconsistent summaries. Those summaries were then used to train models to detect factual inconsistency. The synthetic data is deliberately wrong in a specific way, and that deliberate wrongness is exactly what the detector needs to learn to notice.

The source also describes a broader use. Perez et al. (2022), in Discovering Language Model Behaviors with Model-Written Evaluations, discussed data synthesis techniques for generating datasets that test 154 different AI behaviors. These included personality traits, political views, ethical stances, and social biases. The authors reported that in head-to-head comparisons, language-model-generated datasets approached the quality of human-written ones, and sometimes exceeded them.

Rare Classes and Class Imbalance

Coverage problems are not only about the shape of the inputs. They can also be about how labels are distributed. Class imbalance describes the situation where classes in the data are unequally distributed: one class has many examples and another has very few. A model trained on such data sees the common class constantly and the rare class seldom, so the rare class is the one it is most likely to be underprepared for.

Common classmany examplesCommon classmany examplesRare classfew examplesRare classmore examplesGenerated rare-classexamplesadded to the rare classadds to
How does the class distribution in the training set change when synthetic examples of the rare class are added?

The source states that it is possible to generate data for the rare class to address the challenges of class imbalance. The intended effect is that the rare class gains examples so that the classes are represented more evenly. The source does not prescribe a particular target ratio, so the right amount of generated data depends on the task.

python
Output (expected)
{'common': 900, 'rare': 30}
{'common': 900, 'rare': 150}

Mistakes Learners Make with Coverage

  • Common mistake

    Assuming that a large dataset has good coverage

    What it looks like

    A team with 1,000 messages concludes the model is ready because the count looks impressive.

    Why it fails

    Coverage is about whether the data spans the kinds of inputs the model will meet. The thousand messages in the example were all of middle length.

    Do this instead

    Sort or inspect the data by the relevant characteristics and check which regions are empty.

  • Common mistake

    Adding more ordinary examples to fix a gap in an extreme region

    What it looks like

    More middle-length messages are collected to help the model with very long complaints.

    Why it fails

    Adding more data with the characteristics the model already has does not supply the missing ones.

    Do this instead

    Name the missing characteristic first, then generate or collect data that carries it.

  • Common mistake

    Treating adversarial examples as training data for the wanted behavior

    What it looks like

    A team generates deliberately hard inputs and assumes they demonstrate the behavior the product should show.

    Why it fails

    An adversarial example is designed to challenge a model and expose weaknesses. Synthetic data that expresses a behavior is designed to show it.

    Do this instead

    Decide whether the goal is to express a behavior or to test robustness, and generate data for that purpose.

  • Common mistake

    Assuming there is a universal target ratio for rare classes

    Why it fails

    The source does not prescribe a particular ratio, and the right amount depends on the task.

    Do this instead

    Set the target for the specific task and check whether the rare class is now represented well enough for it.

Checking Your Understanding of Gaps

EASY

A support-chat classifier has been trained only on messages of ordinary length. Name the coverage gap, state the characteristic you would target, and describe the kind of synthetic data you would generate to fill it.

Hints
  • Look at which message lengths the training data does and does not include.
  • The source gives the example of generating very short texts or very long texts.
  • Describe the data in terms of its characteristic, not just its count.

Summary of Coverage and Synthetic Data

  1. 1
    Data coverage is about whether the training data spans the kinds of inputs and behaviors a model will meet, not only how much data exists.
  2. 2
    Gaps are defined by characteristics such as length, so a dataset of a thousand examples can still have a hole in it.
  3. 3
    Data synthesis generates data with targeted characteristics, either to improve performance or to get a model to express specific behaviors.
  4. 4
    Adversarial examples are synthetic data designed to challenge a model and expose its weaknesses, which is different from data that expresses a wanted behavior.
  5. 5
    Generating data for a rare class can address class imbalance, though the source does not prescribe a target ratio.

At a glance

  • Data coverage is about whether the training data spans the kinds of inputs and behaviors a model will meet, not only how much data exists.
  • Gaps are defined by characteristics such as length, so a dataset of a thousand examples can still have a hole in it.
  • Data synthesis generates data with targeted characteristics, either to improve performance or to get a model to express specific behaviors.
  • Adversarial examples are synthetic data designed to challenge a model and expose its weaknesses, which is different from data that expresses a wanted behavior.
  • Generating data for a rare class can address class imbalance, though the source does not prescribe a target ratio.

Related concepts

More concepts starting with C