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

You choose: hyperparametersTree depthNumber of neighboursRegularisation strengthTraining dataTrainingLearned: parametersCoefficientsSplit thresholdsLeaf values
You choose the hyperparameters, training turns data into a model, and the model's parameters are what it learned.

The math

Training finds the parameters θ\theta that minimise a loss on the training data, with the hyperparameter λ\lambda fixed in advance:

θ^(λ)=arg⁡min⁡θ  ∑i=1nℓ(yi,fθ(xi))+λ Ω(θ)\hat{\theta}(\lambda) = \arg\min_{\theta}\; \sum_{i=1}^{n} \ell\big(y_i, f_\theta(x_i)\big) + \lambda \,\Omega(\theta)

Here ℓ\ell is the error on one row, fθf_\theta is the model, and Ω(θ)\Omega(\theta) measures how complex the parameters are (for example the sum of squared weights). The hyperparameter λ\lambda sets how much complexity is penalised. Different λ\lambda give different learned θ^\hat{\theta}, so the learned parameters depend on the hyperparameters you chose.

You cannot pick λ\lambda by minimising the training loss: the smallest training loss is almost always at λ=0\lambda = 0. You pick it by comparing validation scores, as in the cross-validation lesson.

Worked example

Fit a line y^=a+bx\hat{y} = a + b x 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 Sxx=(1−2)2+0+(3−2)2=2S_{xx} = (1-2)^2 + 0 + (3-2)^2 = 2 and the co-spread is Sxy=(−1)(2−3.667)+0+(1)(5−3.667)=1.667+1.333=3S_{xy} = (-1)(2 - 3.667) + 0 + (1)(5 - 3.667) = 1.667 + 1.333 = 3.

Without regularisation (λ=0\lambda = 0), the best slope is b=Sxy/Sxx=3/2=1.5b = S_{xy} / S_{xx} = 3/2 = 1.5 and the intercept is a=3.667−1.5×2=0.667a = 3.667 - 1.5 \times 2 = 0.667. 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 b=Sxy/(Sxx+λ)b = S_{xy} / (S_{xx} + \lambda). Take λ=2\lambda = 2: 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 λ\lambda 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

  1. Open Pipelines, open a pipeline and go to its Models part.
  2. Expand an algorithm card and switch it from Automatic to Manual.
  3. 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.
  4. Use Reset to defaults to go back. Train once with the defaults before you change anything, so you have a baseline.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. Which of these is a hyperparameter rather than a learned parameter?
  2. 2. Why should your first run use the default settings of an algorithm?
  3. 3. You raise a regularisation strength and the training error goes up. What is the most likely explanation?