ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to LLM Fine-Tuning
Chapter 1: Foundations of Model Customization
What is Fine-Tuning?
Pre-training vs. Fine-Tuning
When to Fine-Tune: An Analytical Framework
Overview of Fine-Tuning Strategies
The Role of Transfer Learning in LLMs
Setting Up Your Development Environment
Chapter 2: Data Preparation for Fine-Tuning
Sourcing and Selecting High-Quality Datasets
Instruction-Based vs. Conversational Data Formats
Data Cleaning and Preprocessing Techniques
Creating and Structuring Custom Datasets
Tokenization for Fine-Tuning
Hands-on Practical: Building a Fine-Tuning Dataset
Chapter 3: Full Parameter Fine-Tuning
The Mechanics of Full Fine-Tuning
Architectural Considerations for Full Fine-Tuning
Managing Computational Resources
Configuring Training Arguments and Hyperparameters
Monitoring Training: Loss and Metrics
Saving and Loading Fine-Tuned Models
Practice: Full Fine-Tuning on a Small-Scale Model
Chapter 4: Parameter-Efficient Fine-Tuning (PEFT)
Introduction to Parameter-Efficient Fine-Tuning
Low-Rank Adaptation (LoRA): Theory and Operation
Implementing LoRA with the PEFT Library
Quantization and its effect on Fine-Tuning (QLoRA)
Other PEFT Methods: A Brief Survey
Comparing PEFT and Full Fine-Tuning Trade-offs
Hands-on Practical: Fine-Tuning with LoRA
Chapter 5: Evaluation and Deployment
Defining Performance Metrics for Generative Tasks
Quantitative Evaluation: ROUGE, BLEU, and Perplexity
Qualitative Evaluation: Human-in-the-Loop Assessment
Building an Evaluation Pipeline
Strategies for Merging Adapters with the Base Model
Preparing Models for Inference
Practice: Evaluating a Fine-Tuned Model

Quantitative Evaluation: ROUGE, BLEU, and Perplexity

While human judgment is the final arbiter of model quality, automated metrics provide a scalable and objective way to track progress during development. They allow you to rapidly compare different model checkpoints or fine-tuning approaches without the time and expense of manual review for every single change. Three of the most common quantitative metrics for this purpose are Perplexity, ROUGE, and BLEU. Each offers a different perspective on model performance, and together they form a solid foundation for your evaluation pipeline.

Perplexity: Measuring Predictive Accuracy

Perplexity is an intrinsic metric that measures how well a probability model predicts a sample. In the context of language models, it quantifies the model's "surprise" when encountering a sequence of text from a test set. A lower perplexity score indicates that the model is less surprised, meaning it assigned a higher probability to the actual sequence of words. This suggests the model's internal probability distribution is well-aligned with the data it is being tested on.

Perplexity is derived directly from the cross-entropy loss calculated during model validation. If H(p,q)H(p,q)H(p,q) is the average cross-entropy loss per token, the perplexity is calculated as:

Perplexity=ecross-entropy=2H(p,q)\text{Perplexity} = e^{\text{cross-entropy}} = 2^{H(p,q)}Perplexity=ecross-entropy=2H(p,q)

For example, given the sentence prefix "The engineers debugged the...", a model with low perplexity would assign a high probability to a plausible continuation like "code". A model that assigns high probability to an unlikely word like "sky" would have a higher perplexity.

When to Use Perplexity:

  • Model Comparison: It is effective for comparing different models or hyperparameters on the same hold-out dataset. A lower perplexity generally indicates a better-fitting model.
  • Training Monitoring: Tracking perplexity on a validation set during training is a standard way to detect overfitting. If validation perplexity starts to increase while training loss continues to decrease, it's a sign the model is no longer generalizing well.

Limitations:

Perplexity measures how well a model predicts the linguistic patterns of the test data. It does not measure the factual accuracy, coherence, or overall quality of a generated response. A model can achieve a low perplexity score on a dataset of news articles but still generate factually incorrect information. Therefore, it is a useful signal for model fit but insufficient on its own for a complete evaluation.

N-gram Based Metrics: BLEU and ROUGE

Unlike perplexity, which looks at a model's internal confidence, metrics like BLEU and ROUGE evaluate the final generated text by comparing it to one or more human-written reference texts. They both operate on the principle of n-gram overlap. An n-gram is a contiguous sequence of n items (in this case, words) from a text.

  • Unigram (1-gram): "The", "cat", "sat"
  • Bigram (2-gram): "The cat", "cat sat"
  • Trigram (3-gram): "The cat sat"

By comparing the n-grams in the generated text to those in a reference text, these metrics provide a score that approximates the quality of the output.

ROUGE: Recall-Oriented Understudy for Gisting Evaluation

ROUGE (Recall-Oriented Understudy for Gisting Evaluation) is a set of metrics designed primarily for evaluating tasks like text summarization. Its main idea is to measure recall: how many of the n-grams from the human-written reference summary are "recalled" or found in the model-generated summary.

The most common variants are:

  • ROUGE-N: Measures the overlap of n-grams. ROUGE-1 measures unigram overlap, which reflects content overlap, while ROUGE-2 measures bigram overlap, which is a better indicator of fluency.
  • ROUGE-L: Measures the Longest Common Subsequence (LCS). The LCS finds the longest sequence of words that are shared between the generated and reference texts, but they don't need to be in a contiguous block. This makes ROUGE-L good at capturing sentence-level structural similarity.

A high ROUGE score implies that the generated text shares significant content with the reference text.

ROUGE-1 unigram overlap between a reference text and a model's output. The green nodes represent shared words, contributing to a higher recall score.

The evaluate library from Hugging Face makes calculating ROUGE straightforward.

from evaluate import load

rouge = load('rouge')
predictions = ["the cat was on the mat"]
references = ["the cat sat on the mat"]

results = rouge.compute(predictions=predictions, references=references)
print(results)
# {'rouge1': 0.857..., 'rouge2': 0.666..., 'rougeL': 0.857...}

BLEU: Bilingual Evaluation Understudy

BLEU (Bilingual Evaluation Understudy) was originally created for evaluating machine translation. Unlike ROUGE, it is a precision-oriented metric. It measures what fraction of the n-grams in the generated text appear in the reference text.

BLEU includes two important modifications to a standard precision calculation:

  1. Modified N-gram Precision: It clips the count of each candidate n-gram to the maximum number of times it appears in any single reference. This prevents a model from getting an artificially high score by over-generating a reasonable word (e.g., outputting "the the the the").
  2. Brevity Penalty (BP): It penalizes generated texts that are shorter than the reference. A very short output can have perfect precision but be an inadequate translation or summary. The BP ensures the model produces outputs of a reasonable length.

A high BLEU score indicates that the generated text is fluent (n-gram matches) and adequate (in length). It is commonly used for tasks where fluency is a high priority, such as translation and code generation.

from evaluate import load

bleu = load('bleu')
predictions = ["the cat was on the mat"]
references = [["the cat sat on the mat"]] # Note: references is a list of lists

results = bleu.compute(predictions=predictions, references=references)
print(results)
# {'bleu': 0.759..., 'precisions': [0.833..., 0.8, 0.6, 0.5], 'brevity_penalty': 1.0, ...}

Choosing Between ROUGE and BLEU

The choice between ROUGE and BLEU depends on what you want to measure. ROUGE is about capturing content (recall), while BLEU is about fluency and fidelity (precision).

  • For summarization, where capturing all important points from the source is the goal, ROUGE is generally preferred.
  • For machine translation, where the output must be grammatically correct and convey the source meaning accurately, BLEU is the standard.

Often, it is useful to report both. A model might achieve a high ROUGE score by producing a long, rambling summary that includes all the right keywords but is not very readable (low BLEU). Another might produce a fluent, concise sentence (high BLEU) that misses important information (low ROUGE).

Model A is stronger on ROUGE-L, indicating it captures more of the reference content. Model B is stronger on BLEU, suggesting its output is more fluent and precise, though it may miss some content.

Ultimately, these quantitative metrics are powerful tools for automated analysis. They provide fast, reproducible, and scalable signals about model performance. However, they are proxies for human perception of quality. A high score is a good sign, but it is not a guarantee of a great model. Always supplement these scores with the qualitative, human-in-the-loop assessments we discuss next to get a complete picture of your model's capabilities.

  • BLEU: A Method for Automatic Evaluation of Machine Translation, Kishore Papineni, Salim Roukos, Todd Ward, and Wei-Jing Zhu, 2002 Proceedings of the 40th Annual Meeting of the Association for Computational Linguistics (ACL) (Association for Computational Linguistics) DOI: 10.3115/1073083.1073135 - The original research paper introducing the BLEU metric, outlining its calculation based on modified n-gram precision and brevity penalty for machine translation evaluation.
  • ROUGE: A Package for Automatic Evaluation of Summaries, Chin-Yew Lin, 2004 Text Summarization Branches Out: Proceedings of the ACL-04 Workshop (Association for Computational Linguistics) DOI: 10.3115/1621378.1621389 - The original publication that introduced the ROUGE suite of metrics, explaining how it measures recall-oriented n-gram overlap for automatic summary evaluation.
  • Speech and Language Processing (3rd ed. draft), Daniel Jurafsky and James H. Martin, 2025 - A comprehensive textbook covering natural language processing, including in-depth discussions on language models, perplexity definition and calculation, and various evaluation metrics such as BLEU and ROUGE.

© 2026 ApX Machine Learning