Sub topic 8
How Training Works: Parameters, Loss, and Optimization
This chapter assumes you can write basic Python, know what features and labels are, and remember high-school algebra (a line is y = w*x + b). It explains what "training" means inside a supervised model, using the regression example from the previous chapter.
Parameters: the numbers a model learns
A model is a function with adjustable numbers. These numbers are its parameters. For a line y = w*x + b, the parameters are the slope w and the intercept b. Training means finding parameter values that make the predictions good.
Parameters are different from hyperparameters. You choose hyperparameters before training, for example max_iter or a tree's max_depth. The training procedure does not change them.
Loss: scoring how wrong the model is
To search for good parameters, you need a number that says how bad the current ones are. This number is the loss, computed by a loss function. Lower is better.
A common loss for regression is mean squared error (MSE). For each example, take the prediction minus the true value, square it, and average over all examples. Squaring makes every error positive and punishes large errors more than small ones.
For classification, the usual loss is log loss (cross-entropy). It is small when the model gives high probability to the correct class and large when it is confidently wrong.
Optimization: lowering the loss
Optimization is the search for parameters that minimize the loss. The most common method is gradient descent:
- Start with initial parameter values.
- Compute the loss on the training data.
- Compute the gradient, which says how the loss changes when each parameter changes slightly.
- Move each parameter a small step in the direction that lowers the loss.
- Repeat until the loss stops improving.
The size of the step is the learning rate, a hyperparameter. If it is too large, the loss can bounce around or grow. If it is too small, training is slow.
A worked example
The code below fits the line y = w*x + b with plain gradient descent on MSE. The data is generated from y = 3x + 2 plus noise, so w should end near 3 and b near 2.
import numpy as np
rng = np.random.default_rng(0)
x = rng.uniform(0, 10, size=100)
y = 3 * x + 2 + rng.normal(0, 1, size=100)
w, b = 0.0, 0.0
learning_rate = 0.01
for step in range(2000):
pred = w * x + b
error = pred - y
loss = np.mean(error ** 2)
grad_w = 2 * np.mean(error * x)
grad_b = 2 * np.mean(error)
w -= learning_rate * grad_w
b -= learning_rate * grad_b
if step % 500 == 0:
print(step, round(loss, 3))
print(w, b)The printed loss falls quickly at first and then flattens. The final w and b come out close to 3 and 2. LinearRegression in scikit-learn reaches nearly the same answer, but it solves the problem directly instead of stepping toward it.
Common errors
- Reading a low training loss as success. Loss on the training data can keep falling while the model memorizes noise. Judge the model on held-out data, as later chapters show.
- A learning rate that is too high. If the loss grows or becomes
nan, lower the learning rate. - Unscaled features. When features have very different ranges, gradient descent can be slow or unstable. Standardize them first.
- Stopping too early. If
max_iteris too small, scikit-learn warns that the solver did not converge. Raise it.
Exercise
In the worked example, you change learning_rate from 0.01 to 0.5 and the printed loss grows with every step until it becomes nan. What is happening, and what is the fix?
Show answer
Each update step is so large that it overshoots the minimum, and the next gradient is even larger, so the loss diverges. Lower the learning rate (for example back to 0.01) or standardize x so the gradients are smaller.
Exercise
Name the parameters and one hyperparameter in the line-fitting example.
Show answer
The parameters are w and b, which gradient descent updates. The learning rate (or the number of steps) is a hyperparameter, because you set it before training and the loop never changes it.