Concepts / Fine-Tuning an LLM for Spam Classification

Fine-Tuning an LLM for Spam Classification

A pre-trained LLM is a general-purpose starting point.

  • Programming

A Provider With Many Customers

Imagine a provider that offers one LLM to many different customers, and each customer wants the model adjusted for its own application. Some customers want help with customer support, others want help with document review, and others want to filter spam from their inboxes. The model needs to behave a little differently for each of them.

What do you think happens?

If the provider uses the obvious approach and creates a separate, complete copy of the model for each customer, how does the number of stored copies change as the number of customers grows?

Reveal answer
Answer:

The number of stored copies grows with the number of customers, because each customer gets its own complete copy of the model.

That approach works, but every copy has to be stored, so storage keeps growing as customers are added. The rest of this article explains the alternative arrangement that LoRA offers.

Fine-Tuning Turns a General Model Into a Specialist

Definition

Fine-tuning is the process of adjusting a pre-trained LLM to perform a specific task.

A pre-trained LLM is a general-purpose starting point. It has learned from a broad body of language, but it has not been set up for any one job. Fine-tuning is the step that connects that general model to a specific task. The word that matters in the definition is adjusting. Fine-tuning starts from a model that already exists and changes it so that it performs a particular task. The starting point is general and the ending point is specialized.

Key point

In this article, the task is spam classification. The general pre-trained model is the starting state, and the fine-tuned model that decides whether a message is spam is the specialized end state. The source applies this to spam classification and notes that the overall workflow is similar to the fine-tuning example from an earlier chapter, with LoRA used to adapt the model to the classification task.

Text Classification and Predefined Categories

Definition

Text classification is the task of categorizing text into predefined categories. The categories are fixed in advance, and the model's output is a choice among them.

Identifying spam messages is one example of text classification. For a spam filter, the predefined categories are spam and not spam. The model does not write anything new when it handles a message. It selects one of the categories that were fixed before the message arrived.

Keeping LoRA Weights Separate From the Base Model

LoRA has a practical advantage beyond reducing the number of weights that must be updated during training: the LoRA weight matrices can be kept separate from the original model weights. In practice, this means the pretrained model weights can remain unchanged. The LoRA matrices are then applied dynamically after training, at the moment the model is being used.

Pretrained LLMweightsleft unchangedModel in usebase plus LoRA appliedLoRA weightmatricessmaller, kept separateused together withapplied dynamically
How the pretrained weights and the LoRA matrices relate: the pretrained weights stay unchanged, and the LoRA matrices are applied on top of them when the model is used.

This separation is what makes the other advantages possible. Because the pretrained weights are never overwritten, the same base model can serve as the foundation for many different sets of LoRA matrices, each tailored to a particular purpose. Only the LoRA matrices change during customization, so the base model stays as it was.

Why Saving Only the LoRA Matrices Saves Space

Keeping the LoRA weights separate enables model customization without storing multiple complete versions of an LLM. Only the smaller LoRA matrices need to be adjusted and saved when the model is customized for each specific customer or application. Scalability here means that serving more customers or applications does not require a full model copy for each new one. Each new customization adds only its own smaller set of LoRA matrices to storage, while the shared pretrained model is kept once.

Customer A fullcopycomplete modelShared pretrainedmodelkept onceLoRA set AsmallerCustomer B fullcopycomplete modelLoRA set BsmallerCustomer C fullcopycomplete modelLoRA set Csmallerfoundation forfoundation forfoundation for
Left side: a complete model copy for each customer. Right side: one shared pretrained model, with only a small LoRA set added for each customer.

Tracing a Spam Message Through the Fine-Tuned Model

The source gives the example of fine-tuning an LLM to identify spam messages. The following scenario is a generated example that follows that goal for a single incoming message. The fine-tuned model consists of the unchanged pretrained weights with the LoRA matrices applied when the model is used.

Worked example

Classifying one incoming message

An email system receives the message: Congratulations, you have won a free gift card. Click the link now to claim it. The filter must assign this message to one of the predefined categories, spam or not spam.

  1. 1
    Receive the raw text

    The incoming message arrives as plain text. No category has been assigned yet.

  2. 2
    Pass the text to the fine-tuned model

    The message is handled by the pretrained LLM with its LoRA matrices applied at the time of use. The base weights are the same ones every customer's model would use.

  3. 3
    Choose among fixed categories

    The model's task is classification, so its output is a choice between the two predefined categories, spam and not spam. It does not compose a new reply.

  4. 4
    Emit the category label

    The model outputs the chosen label, here spam, which the email system can then act on.

Result

The fine-tuned model outputs the label spam for this message.

Incoming messageraw textSpamcategoryFine-tuned modelpretrained weights plusLoRA matricesNot spamcategoryChoice among fixedcategoriesspam or not spaminputscores the messageselectedor selected
The path a single message takes through the fine-tuned model, ending in one of the two predefined categories.

Common Misunderstandings About Fine-Tuning

  • Common mistake

    Thinking fine-tuning means training a new model from scratch

    Why it fails

    Fine-tuning starts from a model that already exists, the pre-trained LLM, and adjusts it for a specific task.

    Do this instead

    Describe fine-tuning as adjusting a starting model that is general into one that is specialized.

  • Common mistake

    Believing that LoRA changes the original model weights permanently

    Why it fails

    The LoRA weight matrices are kept separate from the original model weights, so the pretrained weights can remain unchanged.

    Do this instead

    Think of the LoRA matrices as a separate set applied when the model is used.

  • Common mistake

    Assuming a spam classifier writes a reply to each message

    Why it fails

    A classification task produces a choice among fixed categories rather than newly generated text.

    Do this instead

    The output for a spam filter is one label, spam or not spam.

  • Common mistake

    Assuming that each new customer needs a complete model copy to be served

Classification Output Versus Generated Text

Both classification and open-ended text generation use an LLM, but the shape of the output differs. A classification task selects one label from a fixed set of predefined categories. Open-ended generation produces free-form text that was not chosen from a list in advance.

Predefined labelchosen from fixedcategoriesSpam or not spamexample outputNewly generatedtextfree-form outputA written replyexample output
Each column shows the kind of output a model produces for its task: a single predefined label in classification, or newly written text in open-ended generation.
AspectClassificationOpen-ended generation
Output formA choice among predefined categoriesNewly generated free-form text
Set of possible outputsFixed in advanceNot limited to a fixed list
Spam filter exampleSpam or not spamA written reply to the message

Classification output versus open-ended text generation

Practice: Trace a New Message

EASY

A fine-tuned spam model receives the message: Reminder, your team meeting moves to 3 PM tomorrow. Trace the message from raw text to the output. Name the predefined categories the model can choose from, state what the model's output looks like, and explain why the model does not write a reply.

Hints
  • Start with the raw message text and identify where it enters the model.
  • Recall that the categories are fixed in advance for a spam filter.
  • Remember that a classification task produces a choice among fixed categories rather than newly generated text.

Key Takeaways

  • Fine-tuning adjusts a pre-trained LLM so that it performs a specific task, moving from a general starting point to a specialized end point.
  • Text classification categorizes text into predefined categories, and spam identification is an example in which the categories are spam and not spam.
  • LoRA weight matrices are kept separate from the original model weights, so the pretrained weights can remain unchanged and the LoRA matrices are applied when the model is used.
  • Saving only the smaller LoRA matrices avoids storing a complete model copy per customer or application, which reduces storage requirements and improves scalability.

Related concepts

More concepts starting with F