Lessons › Chapter 5: Generalization
Hyperparameter tuning
Some settings aren’t learned from data: you pick them before training, like the learning rate or how deep a tree may grow. These are called hyperparameters, and they can make or break a model.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
Which settings?
There are dozens of settings to choose and no obvious answer for any of them.
Settings you choose yourself
Some settings aren’t learned from data: you pick them before training, like the learning rate or how deep a tree may grow. These are called hyperparameters, and they can make or break a model.
Grid search: try every combination
grid search tries every pair from your lists (here 3 learning rates × 4 depths = 12 models) and scores each one with cross-validation. Taller blocks mean better scores.
Keep the winner
The best combination here is learning rate 0.01 with depth 8: 88%. Notice that neither the smallest nor the biggest setting won. The middle often does.
Random search is often smarter
With many settings, a full grid explodes in size. Trying random combinations usually finds a great one in far fewer runs, because it explores more distinct values of the settings that matter.
In real life: baking the perfect cookie
A baker testing oven temperatures and baking times is doing grid search: try each combination, taste, keep the best. ML teams do the same thing, letting computers try hundreds of combinations overnight.
Quick check: What is a hyperparameter?
- A weight the model learns from data
- A setting you choose before training, like the learning rate (correct)
- A type of data error
Hyperparameters are set by you, not learned; tuning means searching for good values.
Pseudocode
# settings you choose before training
options = learning_rate in [0.001, 0.01, 0.1]
× tree_depth in [2, 4, 8, 16]
for each combination:
score = cross_validate(model with these settings)
best = the combination with the highest score
# random search: try random combinations instead
Key terms
- hyperparameter
- A setting you choose before training (like the learning rate), rather than one the model learns.
- grid search
- Trying every combination of settings from a list and keeping the best.