Parameters and hyperparameters
Tell what a model learns from what you choose, see how one hyperparameter changes the learned numbers, and learn why default settings are a strong first baseline.
Lesson 41 of 6610 minBeginner
You will learn to
- Tell learned parameters from hyperparameters
- Read a hyperparameter tooltip
- Explain why defaults are a strong baseline
Before this lesson
Intuition
Think of baking bread. The ingredients you measure before baking are your choices: how much flour, how hot the oven, how long. The texture the dough ends up with is the result of what happens during baking. You can change the first and you cannot directly set the second.
A machine learning model works the same way. Parameters are the numbers the model finds by itself while training: the slope of a line, the weights of a network, the thresholds in the splits of a tree. Hyperparameters are the settings you choose before training starts, and they control how the training works: how deep a tree may grow, how many neighbours to look at, how strongly to penalise complexity. The model never learns its own hyperparameters. You set them, or a search tries many values and keeps the best.
Hyperparameters matter because they decide where the model sits between underfitting and overfitting. A tree with depth 1 is too simple; a tree with depth 30 memorises the training rows. The sweet spot depends on the data.
Picture it
The math
Training finds the parameters that minimise a loss on the training data, with the hyperparameter fixed in advance:
Here is the error on one row, is the model, and measures how complex the parameters are (for example the sum of squared weights). The hyperparameter sets how much complexity is penalised. Different give different learned , so the learned parameters depend on the hyperparameters you chose.
You cannot pick by minimising the training loss: the smallest training loss is almost always at . You pick it by comparing validation scores, as in the cross-validation lesson.
Worked example
Fit a line to three points: (1, 2), (2, 4) and (3, 5). The mean of x is 2 and the mean of y is 11/3 = 3.667. The spread of x is and the co-spread is .
Without regularisation (), the best slope is and the intercept is . The learned parameters are a = 0.667 and b = 1.5. The training squared error is 0.167.
With a ridge penalty on the slope, the best slope becomes . Take : b = 3 / 4 = 0.75, and a = 3.667 - 0.75 × 2 = 2.167. The learned parameters changed, although the data did not. The training squared error rose to 1.29, because the line is now flatter and follows the points less closely.
Neither value of is "right" in itself. On three points the penalty only hurts, but on noisy data with many features a flatter line often does better on new rows. Only a validation score can tell you, which is why hyperparameters are tuned with cross-validation.
How mlflow does it
In Pipeline Studio, in the Models part of the pipeline editor (headed "Which models to try") each algorithm is a card with two modes. Automatic uses the algorithm's default values ("Uses the algorithm's default values"). Manual lets you set the parameters yourself, and Advanced parameters lists the less common ones. Reset to defaults goes back to the starting point.
Every hyperparameter has a help tooltip with three parts: what it does, its valid range, and what happens when you increase or decrease it. For example, for a boosted model the learning rate tooltip tells you that a lower rate usually gives better results but needs more trees and more time. Parameters you set manually stay fixed when you ask mlflow to search for better values. See Hyperparameters in the docs.
Common mistakes
- Tuning before you have a baseline. Without the default result you cannot know whether tuning helped. Train once with defaults, note the score, then change settings.
- Changing many hyperparameters at once. You cannot tell which change made the difference. Change one, or use a search (next lesson).
- Choosing by training score. More complexity always lowers training error. Compare cross-validated scores.
- Mixing up the terms. "Learning rate" and "number of trees" are hyperparameters; "weights" are parameters. Mixing them up makes documentation and tooltips confusing.
- Expecting tuning to rescue weak data. Defaults are often close to the best a model can do. Larger gains usually come from better features and cleaner data.
Try it in mlflow
Switch an algorithm to Manual
- Open Pipelines, open a pipeline and go to its Models part.
- Expand an algorithm card and switch it from Automatic to Manual.
- Open Advanced parameters and hover over the help icon of one hyperparameter to read what it does and what happens when you raise or lower it.
- Use Reset to defaults to go back. Train once with the defaults before you change anything, so you have a baseline.