What is fine-tuning?

In short
Fine-tuning means taking a large pretrained model and training it further on a smaller dataset built for a specific task or domain. The model starts from what it already knows, so it needs far less data, compute, and time than training from scratch.
Fine-tuning is a way to adapt an existing machine learning model to a new job. Instead of building a model from random starting values, you begin with a model that someone already trained on a large amount of general data. Then you continue training it on a smaller, focused dataset. For example, if you feed it coding examples, it gets better at writing code.
How it works
A large model learns general patterns during pretraining. Fine-tuning reuses those learned patterns and adjusts them with new examples. The process is the same as pretraining in its mechanics. The difference is the starting point. Because the model already has useful knowledge, the new dataset can be much smaller, and the training run needs less compute and time.
The official Hugging Face guide shows this workflow with the Trainer class. The main steps are:
- Load a dataset and turn its text into tokens, the numeric pieces a model reads.
- Load a pretrained checkpoint, which is a saved set of model weights.
- Set training options such as the number of epochs, batch size, and learning rate.
- Create a Trainer and call train() to start the run.
- Optionally push the result to the Hugging Face Hub so you can share it.
The learning rate matters here. It controls how big each update to the weights is. A small value, such as 2e-5 in the guide's example, keeps the model close to what it already knows while it learns the new task.
Preparing the data
Fine-tuning starts with data that matches the task. The guide tokenizes a text column and drops the columns the model does not use. It also truncates long sequences to a fixed maximum length, so very long examples do not overflow the model's input limit. A held-out test split is created so you can check how well the model performs on examples it did not train on.
A data collator groups samples into batches. The guide uses a collator that pads each batch only to the length of its longest sequence. This avoids wasting memory and compute on padding tokens that carry no meaning. If you are new to tokenizers, our Tokenization explainer is a useful starting point.
Memory and loading choices
Model weights take real memory, so the way you load them matters. The guide sets the dtype to "auto", which loads the weights in the format they were saved in. Without that setting, PyTorch loads them in a larger format by default, and that can double memory use when the saved weights are in a smaller format.
The guide also turns on gradient checkpointing and gradient accumulation. These options trade some speed for lower memory use. You should read their details in the official documentation before using them in production, because the right settings depend on your hardware.
Common mistakes
A few problems come up often. Using a dataset that is too small or too narrow can make the model forget general skills. Skipping a test split makes it hard to tell whether training helped. Choosing a learning rate that is too high can make the model's answers worse. Forgetting to match the tokenizer to the model is another frequent source of errors, since each model expects its own token format.
Alternatives and next steps
Fine-tuning changes the model's weights. Other approaches change only the input, such as writing better prompts or adding relevant documents at query time. Prompting is faster to try, but it does not teach the model new habits. Fine-tuning does, but it needs a prepared dataset and a training run.
When you are ready to go further, read the Trainer features guide, the callbacks guide, and the data collators guide in the official documentation. They cover custom loss functions, checkpointing, early stopping, and batching options.
FAQ
Is fine-tuning the same as training a model from scratch?
No. Training from scratch starts with random weights and needs a very large dataset. Fine-tuning starts from a model that was already trained, so it uses a smaller dataset and less compute.
How much data do I need to fine-tune a model?
The amount depends on the task and the model. The official guide does not give a fixed number, so test your results on a held-out split and add data if the model does not improve.
Why does the learning rate matter so much?
The learning rate sets how far the weights move on each update. Too high and the model can lose what it learned earlier. Too low and training may take a long time to show any change.
Do I need to upload my model to the Hugging Face Hub?
No. Uploading is optional. The guide shows push_to_hub() as a way to share the weights, tokenizer, and config, but you can also keep the files locally.
Sources
This page is based on the official documentation below. Read it for full detail.
Fine-tuning — official documentation ↗