Fine-tuning
In short
Fine-tuning is the process of taking a pretrained machine learning model and training it further on a smaller, specific dataset to adapt it to one task.
What is fine-tuning?
Fine-tuning means continuing the training of a model that has already been trained, called a pretrained or base model, using your own, much smaller dataset. The model keeps the general knowledge it learned during pretraining and adjusts its weights to get better at a specific task, style, or domain. For example, a general language model can be fine-tuned on support conversations so it answers in a company's tone and format.
A fine-tuning dataset for an LLM is usually a set of example inputs paired with ideal outputs, often a few hundred to a few thousand high-quality examples. Because updating every weight in a large model is expensive, many teams use parameter-efficient methods such as LoRA (low-rank adaptation), which train a small set of extra weights and leave the original model frozen. The result is a new version of the model that behaves differently without needing long, detailed prompts.
An analogy is hiring an experienced doctor and giving them a few weeks of training in one hospital's procedures: they don't relearn medicine, they adapt what they already know. Fine-tuning is used for consistent output formats, specialized classification, domain-specific language, and making smaller models perform well on narrow tasks.
Fine-tuning is often confused with RAG. RAG supplies fresh facts in the prompt at request time without changing the model, while fine-tuning changes the model's behavior by changing its weights. Fine-tuning is a poor way to teach facts that change often, because every update requires another training run, so teams usually try better prompts and RAG first.
Key takeaways
- Fine-tuning continues training a pretrained model on a smaller, task-specific dataset.
- It changes the model's weights, so the new behavior persists without extra prompting.
- Parameter-efficient methods like LoRA train only a small set of extra weights.
- Fine-tuning teaches style, format, and skills; RAG is better for changing facts.
- The quality of the examples matters more than their quantity.
Example
import json
# Fine-tuning data: example inputs paired with the ideal outputs
examples = [
{"input": "Order #123 arrived damaged.",
"output": "Sorry about that! A replacement for order #123 is on its way."},
{"input": "Can I change my delivery address?",
"output": "Yes. Open Orders, choose the order, and select Edit address."},
]
# Many training tools accept one JSON object per line (JSONL)
with open("train.jsonl", "w") as f:
for example in examples:
f.write(json.dumps(example) + "\n")Readers ask
What is the difference between fine-tuning and RAG?
Fine-tuning changes the model itself by training it on your examples, while RAG leaves the model unchanged and adds relevant documents to the prompt at request time. Use fine-tuning for consistent behavior, style, or format, and RAG for knowledge that changes often or must be cited.
How much data do you need to fine-tune a model?
It depends on the task, but many LLM fine-tuning jobs start with a few hundred to a few thousand carefully written examples. A small set of clean, consistent examples usually beats a large, noisy one.
Is fine-tuning the same as training a model from scratch?
No. Training from scratch builds a model from random weights and needs enormous datasets and computing power, while fine-tuning starts from a pretrained model and needs only a small fraction of the data and cost.
Often compared
See also
- Machine LearningAI & Machine Learning, p. 27Machine learning is a branch of artificial intelligence in which computers learn patterns from data to make predictions instead of following hand-written rules.
- LLMAI & Machine Learning, p. 25An LLM is a machine learning model trained on huge amounts of text that generates language by repeatedly predicting the next most likely piece of text.
- RAGAI & Machine Learning, p. 38RAG is a technique that makes an LLM answer using relevant documents retrieved at question time, so its responses are grounded in current, specific data.
- PromptAI & Machine Learning, p. 35A prompt is the input text or instructions you give an AI model, such as an LLM, to tell it what task to perform and what kind of answer you want.
- Deep LearningAI & Machine Learning, p. 14Deep learning is a subset of machine learning that uses neural networks with many layers to learn complex patterns from raw data such as images and text.
- InferenceAI & Machine Learning, p. 24Inference is the stage where a trained machine learning model is used to make predictions or generate output from new data, without changing what it learned.
- LoRAAI & Machine Learning, p. 26LoRA is a cheap way to fine-tune a large model: its weights stay frozen and only small added matrices are trained, so a new skill fits in a few megabytes.
Spotted a mistake or something missing on this page?Suggest an edit