Fine-tuning LLMs: tutorial, roadmap, courses, small data risks, evaluation, BERT tips

LLM fine-tuning tutorial, roadmap and courses, training stages, small-data risks, evaluation sets, BERT tokens, confusion matrices and batch norm.

Walking a room through the products he has shipped
Walking a room through the products he has shipped

Key takeaways

  • Start with QLoRA and TRL's SFTTrainer on a small open model, and compare against the base model.
  • Pre-training learns language, instruction tuning teaches following requests, and fine-tuning specialises behaviour.
  • Small datasets risk overfitting and forgetting; use LoRA, fewer epochs and a held-out validation set.
  • Fine-tuning cannot guarantee answers come from your data; use RAG with citations and refusals.
  • Keep batch normalization statistics frozen when fine-tuning vision models with small batches.

Fine-tuning adapts a pretrained model to your task or style by training it further on your examples, and for LLMs the practical default is parameter-efficient fine-tuning such as LoRA or QLoRA. I built FineTune Studio, which runs QLoRA with Transformers, PEFT and TRL, streams live loss over WebSockets and compares base and tuned models, so this page covers a tutorial, roadmap and courses, training stages, small-data risks, evaluation datasets, grounding answers, BERT tokenizers, confusion matrices and batch normalization.

What is a good tutorial for fine-tuning an LLM?

A minimal path: pick a small open model, prepare a few hundred to a few thousand high-quality instruction and response pairs in a chat format, train a LoRA or QLoRA adapter with Hugging Face TRL's SFTTrainer, and compare outputs against the base model.

Also asked as: tutorial fine tuning llm

Steps in detail: define the behaviour you want and why prompting is not enough; write or collect examples and hold out a test set; format them with the model's chat template; load the model in 4-bit with bitsandbytes for QLoRA; set LoRA rank, alpha and target modules; train for one to three epochs while watching training and validation loss; then evaluate on held-out examples and save the adapter. Hugging Face's documentation and Unsloth's notebooks provide working starting code.

What is a fine-tuning LLM roadmap?

Learn in stages: Python and PyTorch basics, Transformers and tokenizers, prompting and RAG first, then supervised fine-tuning with LoRA, then preference tuning such as DPO, and finally evaluation, serving and monitoring of tuned models.

Also asked as: fine tuning llm roadmap

Milestones that prove skill: fine-tune a small classifier such as BERT on a labelled dataset; fine-tune a small LLM with QLoRA for a narrow task like structured extraction; run a base-versus-tuned evaluation with a held-out set; try DPO on preference pairs; then deploy the model with an inference server such as vLLM. Along the way, learn data quality practices, because most fine-tuning results depend on the dataset more than on hyperparameters.

What is the best LLM fine-tuning course?

Strong free options include Hugging Face's LLM Course and its fine-tuning chapters, DeepLearning.AI short courses on fine-tuning and preference tuning, and hands-on notebooks from Unsloth and the TRL documentation. Choose ones with runnable code and current libraries.

Also asked as: best llm fine tuning course

Look for courses that cover data preparation, chat templates, LoRA and QLoRA, evaluation and deployment, not only the training call. Check the date, because fine-tuning tooling changes quickly and older tutorials use deprecated arguments. Supplement courses with papers such as LoRA and QLoRA to understand why the methods work. The best learning comes from fine-tuning a model for a real problem, measuring results, and writing up what changed and why.

What is the difference between pre-training, fine-tuning and instruction tuning?

Pre-training teaches a model general language by predicting the next token over vast text. Fine-tuning further trains a pretrained model on a smaller dataset for a task or domain. Instruction tuning is fine-tuning on instruction and response pairs so the model follows requests.

Also asked as: what is the difference between pre-training, fine-tuning, and instruct-tuning exactly

Pre-training costs enormous compute and produces a base model that continues text but does not reliably answer questions. Instruction tuning, also called supervised fine-tuning, turns it into an assistant, and preference tuning such as RLHF or DPO then aligns responses with human preferences. When practitioners say they fine-tuned a model, they usually mean further supervised training of an already instruction-tuned model on their own examples, often with LoRA, to specialise its format, style or domain behaviour.

What is a risk of small fine-tuning datasets?

The main risk is overfitting: the model memorises the few examples, performing well on them but poorly on new inputs. Small datasets can also cause catastrophic forgetting of general abilities and amplify any errors or biases in the examples.

Also asked as: what is a risk associated with small fine tuning datasets

Signs include training loss dropping while validation loss rises, and outputs that copy training phrases. Mitigations: use parameter-efficient methods like LoRA, which change fewer weights; train for fewer epochs with a lower learning rate; hold out a validation set; add diverse examples; and evaluate general capabilities as well as your task. Quality matters more than quantity: a few hundred clean, varied examples often beat thousands of noisy ones. In FineTune Studio, live loss curves make overfitting visible early.

How should you create a dataset for LLM fine-tuning evaluation?

Build the evaluation set before training, from real inputs your model will face, kept completely separate from training data. Include typical cases, hard cases and edge cases, with reference answers or clear grading criteria for each.

Also asked as: how to think about creating a dataset for llm fine-tuning evaluation

Aim for at least 50 to 200 examples for a narrow task, stratified across the categories you care about. Decide metrics in advance: exact match or schema validity for extraction, accuracy for classification, and rubric-based scores from human or model judges for open-ended outputs. Run the same set on the base model, prompted alternatives and the tuned model, so you know fine-tuning actually helped. Check for leakage, near-duplicates between training and evaluation data, which inflate scores.

How do you make sure answers come from your fine-tuning dataset?

You cannot guarantee it with fine-tuning alone. Fine-tuning shapes behaviour and style, but the model still blends its pretrained knowledge and may hallucinate. If answers must come from your data, use retrieval-augmented generation with citations.

Also asked as: how do i make sure answers are from a customized (fine-tuning) dataset

With RAG, the model receives relevant passages from your documents at query time and is instructed to answer only from them, citing sources, and to refuse when evidence is missing. RAG.NextUpgrad adds a confidence gate for exactly this reason: it declines when retrieval support is weak. Fine-tuning can complement RAG by teaching the model to follow your citation format and refusal behaviour. For factual knowledge that changes, update the documents, not the model weights.

Can you add new vocabulary to BERT's tokenizer when fine-tuning?

Yes. In Hugging Face Transformers, call tokenizer.add_tokens([...]) with your new words, then model.resize_token_embeddings(len(tokenizer)) so the embedding matrix has rows for them. The new embeddings start untrained and learn during fine-tuning.

Also asked as: is it possible to add new vocabulary to bert's tokenizer when fine-tuning

Add tokens only when needed: BERT's WordPiece tokenizer already splits unknown words into subword pieces, which often works fine. New tokens help for frequent domain terms, such as chemical names or product codes, that split into many fragments. With little data, new embeddings may learn poorly; initialising them as the average of their subword embeddings can help. For heavily specialised domains, consider a domain-pretrained model instead, such as BioBERT for biomedical text.

How do you check a confusion matrix after fine-tuning?

Run the fine-tuned model on your held-out test set, collect predicted and true labels, and compute the matrix with scikit-learn: confusion_matrix(y_true, y_pred). Use ConfusionMatrixDisplay to plot it with class names.

Also asked as: how can i check a confusion_matrix after fine-tuning with custom datasets

With the Hugging Face Trainer, call trainer.predict(test_dataset), take argmax over the returned logits to get predicted classes, and compare them with the label IDs. Map IDs to names using the model's id2label config. Look for classes that are frequently confused, which often reveal ambiguous labels or too few examples. Also print classification_report for per-class precision, recall and F1, since overall accuracy hides weak classes in imbalanced datasets.

Why freeze batch normalization layers when fine-tuning?

Batch normalization layers keep running statistics, a mean and variance learned from the original large dataset. Fine-tuning with small batches would overwrite those statistics with noisy estimates, which can sharply degrade a pretrained vision model's accuracy.

Also asked as: why it's necessary to frozen all inner state of a batch normalization layer when fine-tuning

So when fine-tuning convolutional networks, practitioners usually keep batch normalization in inference mode, using stored statistics, while training other layers. In Keras, setting a BatchNormalization layer's trainable to False makes it run in inference mode, and the transfer learning guide recommends calling the base model with training=False. In PyTorch, call .eval() on those layers or freeze them explicitly. Transformers mostly use layer normalization instead, which has no running statistics, so this issue rarely affects LLM fine-tuning.

Sources

  1. Hugging Face, TRL SFTTrainerhuggingface.co
  2. Hu et al., LoRA: Low-Rank Adaptation of Large Language Modelsarxiv.org
  3. Dettmers et al., QLoRA: Efficient Finetuning of Quantized LLMsarxiv.org
  4. Hugging Face, LLM Coursehuggingface.co
  5. Keras, Transfer learning and fine-tuning guidekeras.io
  6. scikit-learn, confusion_matrixscikit-learn.org
Pranjul Rathour
Pranjul Rathour
GenAI Engineer · India

GenAI engineer and AI product builder with 2+ years shipping production-grade AI systems: RAG pipelines, fine-tuned LLMs, hybrid retrieval and multi-modal apps across vision, speech and OCR, architected end to end from ingestion to deployment. Leads engineering for SCULT INDIA's 14-member team, founded the 500+ member TechVerse Enclave community and has mentored 200+ students. Three hackathon first prizes: Changethon 2025 (IIT Roorkee), Product Genesis at Vividhotsava 2025 (CSJMU) and BYTEBATTLE (MeetKats).

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way. Email pranjulrathour41@gmail.com.

Keep reading

More on Fine-tuning LLMs

All Fine-tuning LLMs guides →
Campus talks · hackathon judging · mentoring

Want this as a live session at your college?

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way.

Pranjul Rathour, GenAI engineer in India