Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 63

Hyperparameter Tuning with Validation Sets

Recommended resources

Hello! Welcome to the next-to-last lesson in your capstone project.

In our previous lesson, you built the "algorithm gauntlet"—a fantastic script to systematically evaluate all your from-scratch models on benchmark datasets. You saw how their performance stacked up using their default settings. However, almost every model has a set of "dials and knobs" that can be adjusted to significantly improve its performance. These are its hyperparameters.

This lesson is all about learning how to tune those dials. You will learn how to use your validation set—a concept you've already implemented—to systematically search for the best hyperparameter configurations and unlock the true potential of your models.

By the end of this lesson, you will be able to:

  • Distinguish between model parameters and hyperparameters.
  • Implement a manual hyperparameter search using a validation set.
  • Use k-fold cross-validation for more robust hyperparameter tuning.
  • Understand and apply systematic search strategies like Grid Search.
  • Tune at least one key hyperparameter for several of your custom-built models.

Let's get started.

1. Parameters vs. Hyperparameters

First, it's crucial to understand the distinction between two types of "parameters" in machine learning:

  • Model Parameters: These are values that the model learns from the data during training. The goal of training is to find the optimal values for these parameters. Examples include the weights () in your linear and logistic regression models, or the weights and biases in your neural network. You never set these by hand.
  • Hyperparameters: These are configuration settings for the learning algorithm itself, which you, the practitioner, must set before training begins. They are not learned from the data but rather guide the learning process.

Examples of hyperparameters in the models you've built include:

  • The learning_rate () and n_iterations in your gradient descent optimizer.
  • The regularization strength (lambda or alpha) and type (L1 or L2) in your regularized regression models.
  • The soft-margin constant C in your SVM.
  • The number of trees (n_trees), max_depth, and min_samples_split in your Random Forest.
  • The number of hidden layers and neurons in your MLP.

The process of finding the best combination of these settings is called hyperparameter tuning.

Impact of Learning Rate on Gradient Descent Convergence
This image illustrates the impact of tuning just one hyperparameter: the learning rate. A rate that's too low leads to painfully slow convergence. Too high, and you overshoot the minimum, causing the loss to diverge. The 'just right' rate finds the optimal solution efficiently. This is the essence of hyperparameter tuning.

To get a quick overview of the main concepts, watch the first part of this video.

Hyperparameter Tuning Explained in 14 Minutes

The video 'Hyperparameter Tuning Explained in 14 Minutes' by NeuralNine provides a great introduction to the topic.

Watch from the beginning to 03:40. Pay attention to the distinction between model parameters and hyperparameters, and the role of the validation set in the tuning process.


2. The Core of Tuning: The Validation Set

As the video mentioned, we cannot use the test set to tune hyperparameters. If you tune your model's settings based on its performance on the test set, you are effectively "leaking" information from the test set into your model. Your model will become optimized for that specific test set, and its performance score will be an overly optimistic estimate of how it would perform on new, unseen data.

This is where the validation set comes in. You split your original training data into two new parts:

  1. A smaller training set for learning the model parameters (e.g., weights).
  2. A validation set for evaluating the model with a given set of hyperparameters.

The process looks like this:

  1. Pick a set of hyperparameters.
  2. Train the model on the smaller training set.
  3. Evaluate the model's performance on the validation set.
  4. Repeat with a different set of hyperparameters.
  5. After trying all combinations, select the hyperparameters that gave the best score on the validation set.
  6. Finally, train your model one last time on the entire original training set using these best hyperparameters, and then get your final, unbiased performance score from the test set.
Hyper Parameter Tuning Process with 5-Fold Cross Validation
This flowchart clearly visualizes the full process. Data is split into training and test sets. The training data is then used for the tuning process (here shown with 5-fold cross-validation, which we'll cover next), and the test set is kept separate for the final, honest evaluation.

3. Manual Hyperparameter Tuning: A From-Scratch Approach

Let's implement this logic. In the spirit of your learning style, we will first build a manual loop to tune a single hyperparameter.

The following resource walks through an example of tuning the regularization parameter alpha for a linear model. It plots the performance on both the training and validation sets, which is an excellent way to diagnose overfitting and underfitting.

Foundational Aspects of Machine Learning using Python

The 'Foundational Aspects of Machine Learning using Python' tutorial provides a clear, step-by-step implementation of manual tuning using a validation set. We'll replicate this logic.

Read the section titled 'Approach 2: Adding regularization and validation set'. Focus on the code blocks where they: Split the training data further into X_train, y_train and X_valid, y_valid. Loop through different values of alpha. For each alpha, fit the model on the new, smaller training set. Calculate and store the R-squared score on both the training and validation sets. Plot the training and validation scores against the different alpha values.

Your task now is to apply this logic to one of your own models. Let's choose your Regularized Linear Regression model and tune its lambda (or alpha) hyperparameter.

Your Implementation Task:

  1. Take your full training dataset (X_train, y_train).
  2. Use your train_test_split function from Module 1 to split it again, creating a (X_train_small, y_train_small) and a (X_val, y_val). A 80/20 split is a good starting point.
  3. Create a list of lambda values to test (e.g., [0.001, 0.01, 0.1, 1, 10, 100]).
  4. Write a for loop that iterates through your list of lambdas.
  5. Inside the loop:
    • Instantiate your Linear Regression model with the current lambda.
    • Train it on (X_train_small, y_train_small).
    • Calculate the R-squared score (or MSE) on y_val and store it.
  6. After the loop, identify the lambda that produced the best validation score. This is your optimal hyperparameter from this search!

This manual process gives you a deep, practical understanding of how tuning works.


4. Robust Tuning with K-Fold Cross-Validation

A single validation set works, but the results can be sensitive to how you split the data. A more robust and widely-used method is k-fold cross-validation, which you implemented in Module 1.

Instead of one validation set, we create k of them. For each hyperparameter combination, we train and evaluate the model k times, and the performance metric is the average score across all k folds.

This process, known as Grid Search with Cross-Validation, is the gold standard for hyperparameter tuning. Let's see how it works before automating it.

Machine Learning Tutorial Python - 16: Hyper parameter Tuning (GridSearchCV)

This video from codebasics demonstrates how to perform hyperparameter tuning, first with a manual for loop and then with scikit-learn's GridSearchCV.

Watch from 00:49 to 10:14. Pay close attention to: 00:49 - 05:08: The demonstration of a manual, nested for loop to try different combinations of hyperparameters (C and kernel) using cross_val_score. This is the exact logic we are aiming for. 05:08 - 10:14: The introduction of GridSearchCV as an automated way to perform the same task, and how to interpret its results.

GridSearchCV is a powerful tool that automates this entire process. It takes an estimator (your model), a parameter grid (param_grid), and a cross-validation (cv) strategy.

Here is how you would set it up in practice, integrating the Pipeline concept from our last lesson. This is an extremely common and powerful pattern in professional machine learning.

Foundational Aspects of Machine Learning using Python

Let's revisit the 'Foundational Aspects of Machine Learning' tutorial to see how GridSearchCV is used in a complete pipeline.

Read the section 'Approach 4: “classical” ML pipeline'. This section shows how to combine a scaler and a model into a Pipeline, define a grid_values dictionary for the hyperparameters you want to tune, and then pass everything to GridSearchCV.

The param_grid is a dictionary where keys are the names of the hyperparameters you want to tune (using the step_name__parameter_name syntax for pipelines) and values are lists of the values you want to try. GridSearchCV will then exhaustively test every single combination.

Test your understanding!

You want to tune your RandomForest model. You decide to test 3 values for n_estimators [10, 50, 100], 4 values for max_depth [5, 10, 15, 20], and 2 values for min_samples_split [2, 5]. If you use 5-fold cross-validation, how many times will a model need to be trained in total?

Show answer

The total number of combinations is the product of the number of options for each hyperparameter: 3 (n_estimators) * 4 (max_depth) * 2 (min_samples_split) = 24 combinations.

Since you are using 5-fold cross-validation, each of these 24 combinations will be trained 5 times.

Total trainings = 24 combinations * 5 folds = 120 model trainings. This illustrates why grid search can become computationally expensive very quickly!


5. Your Capstone Tuning Task

It's time to apply these concepts to your project. Your goal is to tune at least one key hyperparameter for three of the models you built. This will involve adapting the logic from your manual tuning exercise to work with your cross-validation code.

Instructions:

  1. Choose Your Models and Hyperparameters: Select at least three models from your collection (e.g., SVM, Random Forest, Logistic Regression) and pick one key hyperparameter for each to tune.

    • Logistic Regression: lambda (regularization strength)
    • SVM: C (soft-margin constant)
    • Random Forest: max_depth
    • Gradient Boosting: n_estimators (number of trees)
    • MLP: learning_rate
  2. Implement the Tuning Logic: For each chosen model and hyperparameter:

    • Define a list of values to test for the hyperparameter.
    • Use your k-fold cross-validation implementation from Module 1.
    • Create a loop that iterates through your list of hyperparameter values.
    • Inside this loop, run your k-fold cross-validation logic. The performance for that hyperparameter value is the average score across all k folds.
    • Keep track of the scores for each hyperparameter value.
    • After the loop, identify the hyperparameter value that yielded the best average cross-validation score.
  3. Update and Re-run Your Evaluation:

    • Take the best hyperparameter values you found for each model.
    • Update your "algorithm gauntlet" script from the previous lesson to use these new, optimized hyperparameters instead of the defaults.
    • Re-run the evaluation on your chosen benchmark datasets.
  4. Analyze the Results: Did the tuned models perform better on the final test set than their default versions? Briefly summarize your findings.

This exercise is the culmination of your work, combining data splitting, model training, evaluation, and now, optimization.


Conclusion

You have now mastered one of the most critical skills in applied machine learning: hyperparameter tuning. You understand not just how to do it, but why it's done, and the importance of using a validation set or cross-validation to do it correctly.

Key Takeaways:

  • Hyperparameters are settings that control the learning process and must be set before training.
  • Tuning should always be done using a validation set or cross-validation on the training data, never the test set.
  • A manual loop is a great way to understand the mechanics of tuning.
  • Grid Search with Cross-Validation is a robust and systematic method for finding optimal hyperparameter combinations.
  • Tools like scikit-learn's GridSearchCV automate this process efficiently, especially when combined with Pipeline.

Preview of the Next Lesson:
This was the final implementation-heavy lesson. You now have trained, evaluated, and tuned a whole suite of supervised learning models. The final step in this capstone is to bring everything together. In our last lesson, you will generate a comparative report that analyzes the performance, training time, and characteristics of each algorithm, concluding your comprehensive study.