Course contentsShow
← Machine Learning and Deep Learning
Lesson 498 of 3,53812. Model Validation and TuningFree lesson

Nested Cross-Validation for Hyperparameter Tuning

Understanding the outer loop for model assessment and inner loop for hyperparameter selection.

Nested Cross-Validation for Hyperparameter Tuning

What You'll Learn

You'll learn how nested cross-validation uses two separate loops—an outer one for unbiased model evaluation and an inner one for hyperparameter selection—to prevent overfitting during the tuning process.

The Problem with Simple Cross-Validation + Tuning

When you use cross-validation to both tune hyperparameters and evaluate your model, you're peeking at the test data indirectly. The hyperparameters get optimized for that specific fold split, leading to overly optimistic performance estimates.

The Two-Loop Solution

Nested cross-validation solves this by creating two independent validation processes:

Outer Loop (Model Assessment)

This is your honest evaluation layer. It splits your data into training and test sets multiple times (just like regular K-Fold CV). Each fold gives you an unbiased performance estimate because the test set is completely held out.

Inner Loop (Hyperparameter Selection)

Within each outer training fold, you run another full cross-validation process to find the best hyperparameters. This inner CV only sees the outer training data—never the outer test fold.

How It Works Together

Think of it like a cooking competition with two judging rounds:

  • Inner loop: Practice rounds where you test recipes (hyperparameters) on your kitchen team (inner CV splits)
  • Outer loop: The real judges (outer test folds) who never saw your practice sessions

For each outer fold, the inner loop identifies optimal hyperparameters, trains a model with those settings, and evaluates it on the outer test fold. You average all outer fold scores for your final, unbiased performance estimate.

Key Takeaway: Nested cross-validation prevents hyperparameter overfitting by keeping model evaluation (outer loop) completely separate from hyperparameter selection (inner loop), giving you trustworthy performance estimates.