Fine-tuning means training an existing pre-trained LLM a little more on your own examples, so it learns a specific style, format or task. It changes the model’s weights, unlike prompt engineering or RAG.
What You Will Learn
- When to fine-tune and when not to
- Full fine-tuning vs LoRA and QLoRA
- How to prepare a training dataset
- The fine-tuning workflow with Hugging Face
- How to evaluate the fine-tuned model
Prerequisites
Hugging Face, LLM Fundamentals and Python.
Should You Fine-Tune?
| Goal | Best approach |
|---|---|
| Answer from company documents | RAG |
| Better answers with clearer instructions | Prompt engineering |
| Always reply in a fixed format or brand tone | Fine-tuning |
| Specialised task (e.g. classify support tickets) | Fine-tuning |
| Smaller, cheaper model that matches a big one on one task | Fine-tuning |
Types of Fine-Tuning
| Method | What is trained | Hardware |
|---|---|---|
| Full fine-tuning | All weights | Several large GPUs |
| LoRA | Small “adapter” matrices added to layers; base weights frozen | One GPU |
| QLoRA | LoRA on a 4-bit quantized model | Even a free Colab GPU for small models |
| Instruction tuning | Model learns to follow instructions from (instruction, response) pairs | Depends on size |
| RLHF / DPO | Model learns from human preference pairs | Advanced |
Step 1: Prepare the Data
{"messages": [
{"role": "system", "content": "You are the support bot of machinelearning.org.in."},
{"role": "user", "content": "Do you give certificates?"},
{"role": "assistant", "content": "Yes. After you finish a track and pass the assessment, you get a shareable certificate."}
]}
Explanation: Each line of the JSONL file is one conversation showing the exact behaviour you want. Hundreds of high-quality, varied examples usually beat thousands of messy ones.
Step 2: Fine-Tune with LoRA (Hugging Face)
# pip install transformers datasets peft trl accelerate
from datasets import load_dataset
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
dataset = load_dataset("json", data_files="train.jsonl", split="train")
lora = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
target_modules="all-linear", task_type="CAUSAL_LM")
trainer = SFTTrainer(
model="Qwen/Qwen2.5-0.5B-Instruct", # a small open model to practise with
train_dataset=dataset,
peft_config=lora,
args=SFTConfig(output_dir="my-support-bot", num_train_epochs=3,
per_device_train_batch_size=4, learning_rate=2e-4),
)
trainer.train()
trainer.save_model("my-support-bot")
| Setting | Meaning |
|---|---|
r |
Rank of the LoRA matrices – bigger = more capacity, more memory |
lora_alpha |
Scaling factor for the adapter |
target_modules |
Which layers get adapters |
num_train_epochs |
Passes over the data; too many causes overfitting |
learning_rate |
LoRA usually uses about 1e-4 to 2e-4 |
Explanation: Only the small LoRA adapters are trained, so this fits on a single GPU. The saved folder contains the adapter, which is loaded on top of the base model.
trl) change often. If an argument is rejected, check the current Hugging Face TRL documentation.Step 3: Evaluate
- Keep 10–20% of examples as a test set the model never saw.
- Compare base model vs fine-tuned model on the same prompts.
- Check for overfitting: great on training examples, poor on new ones.
- Check that general abilities and safety have not got worse.
Hosted Fine-Tuning
OpenAI, Google (Vertex AI) and others let you upload a JSONL file and fine-tune some of their models without managing GPUs. You pay for training and for using the custom model.
Common Errors
| Problem | Cause and fix |
|---|---|
| CUDA out of memory | Use a smaller model, QLoRA (4-bit), a smaller batch size or gradient accumulation. |
| Model repeats training answers word for word | Overfitting: fewer epochs, more varied data. |
| No improvement | Data quality is the usual cause; fix inconsistent or wrong examples. |
Series
Generative AI series: Introduction · ChatGPT · Google Gemini · Claude AI · Prompt Engineering · AI Agents · RAG · Fine-Tuning LLMs · Multimodal AI
Interview Questions
Q: What is LoRA?
Low-Rank Adaptation: freezing the base model and training small low-rank matrices added to its layers.
Q: What is QLoRA?
LoRA applied to a model quantized to 4 bits, which greatly reduces GPU memory.
Q: When is RAG better than fine-tuning?
When the knowledge changes often or must be cited from documents.
Practice Exercises
- Write 20 training examples for a college FAQ bot in the JSONL format above.
- List three tasks in your job where fine-tuning would beat prompting.
- Fine-tune the small model on Google Colab and compare 5 answers before and after.
Summary
Fine-tuning adapts an LLM’s behaviour with your examples. LoRA and QLoRA make it affordable by training small adapters. Use it for style and specialised tasks; use RAG for changing knowledge.
Next: Multimodal AI · Hugging Face
Machine Learning Tutorials, Courses and Certifications