Fine-Tuning an LLM for Spam Classification
A pre-trained LLM is a general-purpose starting point.
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
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
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.
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
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.
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.
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.
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.
- 1Receive the raw text
The incoming message arrives as plain text. No category has been assigned yet.
- 2Pass 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.
- 3Choose 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.
- 4Emit the category label
The model outputs the chosen label, here spam, which the email system can then act on.
The fine-tuned model outputs the label spam for this message.
Common Misunderstandings About Fine-Tuning
Common mistake Thinking fine-tuning means training a new model from scratch
Why it failsFine-tuning starts from a model that already exists, the pre-trained LLM, and adjusts it for a specific task.
Do this insteadDescribe 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 failsThe LoRA weight matrices are kept separate from the original model weights, so the pretrained weights can remain unchanged.
Do this insteadThink 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 failsA classification task produces a choice among fixed categories rather than newly generated text.
Do this insteadThe 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.
| Aspect | Classification | Open-ended generation |
|---|---|---|
| Output form | A choice among predefined categories | Newly generated free-form text |
| Set of possible outputs | Fixed in advance | Not limited to a fixed list |
| Spam filter example | Spam or not spam | A written reply to the message |
Classification output versus open-ended text generation
Practice: Trace a New Message
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.