Concepts / Preparing Text Data for Language Models

Preparing Text Data for Language Models

Instruction fine-tuning aims at flexibility and interaction quality across many complex user instructions, and it demands larger datasets and more computational resources.

  • Programming

Deciding What the Adapted Model Must Do

When a team decides to adapt a large language model, the first question is rarely which training recipe to use. The more useful question is what the adapted model must do. Some projects need one model that can take many different kinds of requests and respond sensibly to each. Other projects need a model that sorts incoming text into a fixed set of labels and nothing else. The two fine-tuning approaches covered here are built around those two goals, and the choice between them shapes the data you collect, the hardware you need, and the evaluation you run.

What do you think happens?

A company needs a model that can sort customer emails into ten predefined categories and will never need to handle a new category. Which approach fits, and why?

Reveal answer
Answer:

Classification fine-tuning fits this project.

Classification fine-tuning suits precise categorization into predefined classes, and it needs less data and compute than the alternative. Its limitation is that it is confined to the classes it was trained on. Because this project never needs a new category, that limitation costs nothing here.

Two Goals and the Trade-Off Between Them

Instruction fine-tuning aims at flexibility and interaction quality across many complex user instructions. It is the more versatile of the two approaches, but it demands larger datasets and greater computational resources in order to produce a model that is proficient across many tasks.

Classification fine-tuning asks less of both data and compute. It suits precise categorization into predefined classes, such as sentiment analysis or spam detection. The cost of that economy is scope: a classification model's use is confined to the specific classes it was trained on. A new category it has never seen is not something it can handle by design.

Key point

The two approaches sit on opposite sides of a trade-off: versatility on one side, data and compute economy on the other. Neither is better in general. The right choice depends on whether the project needs one model for many kinds of requests or a model for a fixed label set.

The Three Stages of Classification Fine-Tuning

Once classification fine-tuning has been chosen, the work proceeds in three stages. Stage 1 is dataset preparation. Stage 2 is model setup. Stage 3 covers fine-tuning the model and evaluating it. Each stage hands its results to the next, so the order matters.

Stage 2 turns the prepared data toward an actual model. Its steps are to initialize the model, load pretrained weights, modify the model so it suits fine-tuning, and implement evaluation utilities. Stage 3 then runs the fine-tuning itself, evaluates the fine-tuned model, and finally uses the model on new data.

Tracing a Text Message Through Dataset Preparation

The worked example used throughout this topic is a classification fine-tuning project on the GPT model implemented and pretrained earlier. The task is to modify that model and fine-tune it for classification, using a text message dataset that contains both spam and non-spam messages. The text message setting makes the goal intuitive: each message needs one of two labels.

Dataset preparation begins with downloading the dataset. After that comes inspecting and preprocessing it, and only then creating data loaders. Follow one message through that sequence. It starts as raw text with a label attached: a short message marked as spam, for example, or a short message marked as non-spam. Inspection tells you what the rows look like, and preprocessing converts each message into the form the model can read. The data loaders then feed batches of those rows to the model during training.

Messages rarely have the same length, and a model consumes batches of rows that line up position by position. A common way to handle this is to convert each message into a sequence of token IDs and then pad shorter sequences with a filler ID so every row in the batch has the same length. The illustration below uses made-up token IDs to show the idea. It is a generated example of the technique, not a value taken from the source.

Worked example

Following one message from raw text to a batch row

A labeled message is "Win a free prize now" with the label spam. Trace what happens to it across the first steps of dataset preparation.

  1. 1
    Download

    The dataset is obtained first, so the message exists as one labeled row in the collection.

  2. 2
    Inspect

    You look at the rows to understand what the messages and labels look like before changing anything.

  3. 3
    Preprocess

    The message text is converted into token IDs, and the label is kept alongside it. This is an illustrative description of the conversion, not a specific source procedure.

  4. 4
    Pad and batch

    If this message is shorter than the longest message in its batch, filler IDs are added so it matches the others in position and length.

  5. 5
    Create data loaders

    The row is handed to a data loader, which supplies it to the model in batches during fine-tuning.

Result

The message has moved from raw labeled text to a padded, batch-ready row that the model can consume in Stage 3.

Common Mistakes When Choosing and Preparing

  • Common mistake

    Choosing classification fine-tuning for a project that will need new categories later

    Why it fails

    A classification model's use is confined to the classes it was trained on, and a new category it has never seen is not something it can handle by design.

    Do this instead

    Check whether the label set is fixed before choosing. If new kinds of requests are expected, consider instruction fine-tuning.

  • Common mistake

    Assuming instruction fine-tuning is the cheaper option because it is more versatile

    Why it fails

    Instruction fine-tuning demands larger datasets and greater computational resources. Classification fine-tuning asks less of both.

    Do this instead

    Compare the data and hardware budget against the required scope before committing to an approach.

  • Common mistake

    Treating the three stages as independent steps

    Why it fails

    The stages are sequential, and a problem found in evaluation often traces back to earlier choices.

    Do this instead

    When evaluation disappoints, revisit dataset preparation and model setup rather than patching the final output.

  • Common mistake

    Creating data loaders before inspecting the dataset

Trace the Pipeline for a New Project

EASY

A team wants a model that routes support tickets into four fixed departments and will not add departments later. Using the stage structure from this topic, name the approach you would choose and list, in order, the first two steps of dataset preparation for a labeled ticket dataset.

Hints
  • Ask whether the label set is fixed, since that is the deciding factor for classification fine-tuning.
  • Dataset preparation begins with downloading the dataset, followed by inspecting and preprocessing it.

Summary of the Pipeline

  1. 1
    Instruction fine-tuning aims at flexibility and interaction quality across many complex user instructions, at the cost of larger datasets and more computational resources.
  2. 2
    Classification fine-tuning suits precise categorization into predefined classes and needs less data and compute, but is limited to the classes it was trained on.
  3. 3
    Classification fine-tuning proceeds in three stages: dataset preparation, model setup, and fine-tuning with evaluation, and the stages are sequential.
  4. 4
    Dataset preparation begins with downloading the dataset, then inspecting and preprocessing it before creating data loaders.

At a glance

  • Instruction fine-tuning aims at flexibility across many complex instructions but demands larger datasets and more compute.
  • Classification fine-tuning needs less data and compute and suits fixed label sets, but it is limited to the classes it was trained on.
  • Classification fine-tuning runs in three sequential stages: dataset preparation, model setup, and fine-tuning with evaluation.
  • Dataset preparation starts with downloading the dataset, then inspecting and preprocessing it before creating data loaders.

Related concepts

More concepts starting with P