53 / 90

Day 53: Evaluation Protocols: Validation Loss Gap, Overfitting, and Perplexity

Formulate training vs validation loss tracking, detect overfitting and data leakage, calculate held-out perplexity, and establish honest evaluation discipline.

Models, Pretraining & LoRA 480 active minutes (8 hours focused study)
01 120 min

Evaluation Protocols: Train vs Validation Loss Gap

Agent-Assisted Engineering Check

Prerequisites

Day 51 Pretraining Loop and Day 42 Cross-Entropy.

Learning Objective

Measure the divergence between train loss and validation loss across training steps and define the exact onset of overfitting.

Instructions

Define estimate_loss() evaluating the model without gradient computation (torch.no_grad()) across eval_iters batches. Plot or tabulate: Step 0: train 4.10, val 4.12; Step 500: train 2.20, val 2.25; Step 1500: train 1.10, val 1.95. Mark step 1000 as the generalization plateau.

Practice & Prediction Log

1. Why must the evaluation loop execute inside torch.no_grad() and model.eval()?
2. What does a growing gap between training loss and validation loss indicate?
3. State the rule for choosing the best checkpoint: train loss or validation loss?

Independent Mastery Criterion

Defines evaluation hygiene and explains the validation-loss selection criterion.

Authoritative Source Resources & Provenance

linking MIT

nanoGPT: model.py architecture implementation

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head (returns cross-entropy loss when targets are given)

Verified Version & Date: Git master (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 43-45 attention fundamentals.

Safe Fallback (Offline Resilience):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

Compared Candidate Sources & Selection Analysis (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking CC BY 4.0

Google Machine Learning Crash Course

Author: Google for Developers | Original Source Link ↗

Relevant Section / Range: Modules: Training and Test Sets; Overfitting; Classification accuracy

Verified Version & Date: Current online edition (Verified: 2026-10-30)

Selection Rationale:

Industrial, beginner-legible treatment of training/test splitting and overfitting; complements d2l.ai's mathematical framing.

Source Prerequisites:

Basic algebra; short self-contained modules.

Safe Fallback (Offline Resilience):

Split discipline, leakage rules, and metric definitions are fully explained inside the lesson; the course is further reading.

Compared Candidate Sources & Selection Analysis (2)
Dive into Deep Learning (d2l.ai) ↗ compared_selected_for_day29 [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Co-primary: deeper mathematical coverage of model selection, generalization, and optimization for the mathematics-track capstone.

scikit-learn: Model Evaluation documentation ↗ compared_alternative [BSD-3-Clause]

Normative reference once learners call sklearn; kept as an alternative because these days teach evaluation protocol before library usage.

02 120 min

Held-out Perplexity Measurement on Unseen Text

Agent-Assisted Engineering Check

Prerequisites

Activity 53-01 and Day 42 Perplexity.

Learning Objective

Calculate cross-entropy loss and exponentiate it to obtain held-out perplexity PPL = exp(loss) on a test partition.

Instructions

Given a held-out test split of 100,000 tokens: (1) Compute average cross-entropy loss L_val = 1.6094 nats. (2) Compute Perplexity = exp(1.6094) = 5.0. (3) Compare with random baseline for V=50,000: L = ln(50000) approx 10.82, PPL = 50,000.

Practice & Prediction Log

1. If validation loss is 2.3026 nats, what is the exact perplexity?
2. What does PPL=10 mean in terms of next-token uncertainty?
3. Why is reporting training perplexity alone considered deceptive in model evaluation?

Independent Mastery Criterion

Accurately converts cross-entropy to perplexity and explains its interpretation.

Authoritative Source Resources & Provenance

linking MIT

nanoGPT: model.py architecture implementation

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: model.py: class GPT forward(idx, targets): wte + wpe, Blocks, ln_f, lm_head (returns cross-entropy loss when targets are given)

Verified Version & Date: Git master (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 43-45 attention fundamentals.

Safe Fallback (Offline Resilience):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

Compared Candidate Sources & Selection Analysis (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking Free author-published draft chapter Draft text is not reproduced; linking only

Speech and Language Processing, 3rd ed. draft, Chapter 3: N-gram Language Models

Author: Daniel Jurafsky, James H. Martin | Original Source Link ↗

Relevant Section / Range: Sections 3.1-3.4: n-gram counts, maximum-likelihood estimation, Laplace smoothing, and perplexity

Verified Version & Date: Draft of 2026-08-19 (Verified: 2026-10-30)

Selection Rationale:

Standard reference text for n-gram definitions, count normalization, smoothing, and perplexity used across Day 38.

Source Prerequisites:

Basic probability

Safe Fallback (Offline Resilience):

All formulas and worked tables in the curriculum are self-contained and verified by execution.

Compared Candidate Sources & Selection Analysis (2)
Chen & Goodman: An Empirical Study of Smoothing Techniques for Language Modeling ↗ compared_alternative [CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material)]

Detailed smoothing survey but far beyond the add-one scope of a first n-gram day; optional further reading.

MIT OCW 6.863J Natural Language Processing ↗ compared_alternative [CC BY-NC-SA 4.0]

Solid academic lecture treatment but less current and less integrated with executable count tables than the SLP3 draft.

03 120 min

Diagnosing Overfitting, Data Memorization, and Leakage

Agent-Assisted Engineering Check

Prerequisites

Activities 53-01 and 53-02.

Learning Objective

Identify subtle data leakage bugs where validation text is contaminated by training overlap or improper sequence chunking.

Instructions

Examine three scenarios: (A) Shuffling tokens before chunking into sequences (breaks autoregressive causality and leaks adjacent words across splits); (B) Overlapping n-grams between prompt and validation; (C) Zero dropout on a tiny dataset.

Practice & Prediction Log

1. Why does shuffling a token array before splitting into train/val cause catastrophic data leakage?
2. How do you distinguish true generalization from rote memorization of repetitive training phrases?
3. State the proper split order: split documents first, then tokenize, or tokenize everything and random slice?

Independent Mastery Criterion

Identifies document-level leakage and articulates the clean partition protocol.

Authoritative Source Resources & Provenance

linking CC BY 4.0

Google Machine Learning Crash Course

Author: Google for Developers | Original Source Link ↗

Relevant Section / Range: Modules: Training and Test Sets; Overfitting; Classification accuracy

Verified Version & Date: Current online edition (Verified: 2026-10-30)

Selection Rationale:

Industrial, beginner-legible treatment of training/test splitting and overfitting; complements d2l.ai's mathematical framing.

Source Prerequisites:

Basic algebra; short self-contained modules.

Safe Fallback (Offline Resilience):

Split discipline, leakage rules, and metric definitions are fully explained inside the lesson; the course is further reading.

Compared Candidate Sources & Selection Analysis (2)
Dive into Deep Learning (d2l.ai) ↗ compared_selected_for_day29 [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Co-primary: deeper mathematical coverage of model selection, generalization, and optimization for the mathematics-track capstone.

scikit-learn: Model Evaluation documentation ↗ compared_alternative [BSD-3-Clause]

Normative reference once learners call sklearn; kept as an alternative because these days teach evaluation protocol before library usage.

linking MIT

nanoGPT: model.py architecture implementation

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: N/A

Verified Version & Date: Git master (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 43-45 attention fundamentals.

Safe Fallback (Offline Resilience):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

Compared Candidate Sources & Selection Analysis (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

04 120 min

Independent Model Generalization and Checkpoint Assessment

Independent Mastery Check

Prerequisites

Complete activities 53-01 through 53-03.

Learning Objective

Audit an evaluation script, compute empirical train/val metrics, and write an honest assessment report.

Instructions

Given an evaluation trace of a 10M parameter model on tiny Shakespeare: train loss 0.85, val loss 1.75. Write an honest diagnostic summary acknowledging the 0.90 loss gap.

Practice & Prediction Log

1. Is the model underfitting, perfectly fitting, or overfitting?
2. What concrete regularization steps (weight decay, dropout, early stopping) should be applied?
3. Formulate the single sentence you would include in a published report summarizing this outcome.

Independent Mastery Criterion

Honest assessment without defensive excuses or fabricated success claims.

Authoritative Source Resources & Provenance

linking MIT

nanoGPT: model.py architecture implementation

Author: Andrej Karpathy | Original Source Link ↗

Relevant Section / Range: N/A

Verified Version & Date: Git master (Verified: 2026-10-30)

Selection Rationale:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

Source Prerequisites:

Python, PyTorch, and Day 43-45 attention fundamentals.

Safe Fallback (Offline Resilience):

Local models and classes in the curriculum mirror nanoGPT semantics directly.

Compared Candidate Sources & Selection Analysis (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking CC BY 4.0

Google Machine Learning Crash Course

Author: Google for Developers | Original Source Link ↗

Relevant Section / Range: Modules: Training and Test Sets; Overfitting; Classification accuracy

Verified Version & Date: Current online edition (Verified: 2026-10-30)

Selection Rationale:

Industrial, beginner-legible treatment of training/test splitting and overfitting; complements d2l.ai's mathematical framing.

Source Prerequisites:

Basic algebra; short self-contained modules.

Safe Fallback (Offline Resilience):

Split discipline, leakage rules, and metric definitions are fully explained inside the lesson; the course is further reading.

Compared Candidate Sources & Selection Analysis (2)
Dive into Deep Learning (d2l.ai) ↗ compared_selected_for_day29 [CC BY-SA 4.0 (book text) / modified MIT (sample code)]

Co-primary: deeper mathematical coverage of model selection, generalization, and optimization for the mathematics-track capstone.

scikit-learn: Model Evaluation documentation ↗ compared_alternative [BSD-3-Clause]

Normative reference once learners call sklearn; kept as an alternative because these days teach evaluation protocol before library usage.