Lesson 1 of 4 Hyperparameters, Tuning and Model Fit

Recognizing the Effects of Fundamental Hyperparameters

This lesson explains the small set of hyperparameters, settings chosen before training, that control how training proceeds, and how to recognise their effects from symptoms.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain epochs, steps and batch size.
  • Diagnose a learning rate that is too high or too low.
  • Tune algorithm-specific hyperparameters such as XGBoost max_depth.

Epochs, steps and batch size

An epoch is one full pass through the training data. The batch size is the number of examples used to compute one gradient update, and a step (iteration) is one such update, so steps per epoch equal the number of examples divided by the batch size. Some jobs are configured by total steps rather than epochs. Too few epochs underfit; too many overfit unless early stopping is used.

Larger batches give smoother gradient estimates and use GPUs more efficiently, so each epoch is faster, but they need more memory and can generalise slightly worse. Smaller batches add noise that can help generalisation but train more slowly. If a GPU runs out of memory, reduce the batch size or use gradient accumulation.

Fill in the value

Epochs and steps

Exam skills 2.2.7

Calculate steps from dataset size, batch size and epochs.

Log in to try this activity Interactive activities are free with an account.

Learning rate

The learning rate sets the size of each weight update and is usually the most important hyperparameter. If it is too high, the loss oscillates or diverges, and may even become NaN; if it is too low, training is slow or gets stuck in a poor solution.

Learning-rate schedules, such as step decay and cosine decay, reduce the learning rate as training progresses, allowing large steps early and fine adjustments later. Warmup, which starts with a small learning rate and increases it over the first steps, stabilises the start of training, particularly for transformers. When the batch size increases, the learning rate is often increased proportionally.

The following diagram sketches training loss under three learning rates.

Training loss under three learning rates. Too high, the loss oscillates and can diverge; too low, it falls very slowly; a suitable rate with warmup and a decaying schedule falls quickly and then settles.
The curves are schematic.
True or false

Learning rate schedules

Exam skills 2.2.7

Check how learning rate schedules and warmup shape training.

Log in to try this activity Interactive activities are free with an account.

Algorithm-specific hyperparameters

Tree ensembles have their own important settings. In XGBoost, num_round sets the number of trees, and max_depth limits how deep each tree grows: deeper trees capture more complex patterns but overfit more. eta is the learning rate, which shrinks each tree's contribution, so lower values need more rounds.

subsample and colsample_bytree train each tree on a random sample of rows and columns, which adds randomness that reduces overfitting, and alpha, lambda and gamma add regularisation. The following table summarises common symptoms and how to respond to them.

SymptomLikely causeAdjust
Loss oscillates or becomes NaNLearning rate too highLower learning rate, add warmup
Loss decreases very slowlyLearning rate too lowRaise learning rate
GPU out of memoryBatch too largeSmaller batch or gradient accumulation
Validation loss rises after some epochsToo many epochsFewer epochs or early stopping
XGBoost overfitsTrees too deepLower max_depth, raise min_child_weight, subsample
Worked example

Tuning a fine-tuning run

A text classifier fine-tunes for 5 epochs on 40,000 examples with batch size 16 and learning rate 5e-4. Training loss jumps around and validation accuracy peaks at epoch 2, then falls.

Steps per epoch = 40,000 / 16 = 2,500  -> 12,500 steps total
Learning rate 5e-4 is high for transformer fine-tuning -> try 2e-5 with 10% warmup
Validation peaks at epoch 2 -> 2-3 epochs with early stopping

With a lower learning rate, warmup and early stopping, loss becomes smooth and the best checkpoint is kept. Increasing epochs, as first proposed, would have made overfitting worse.

Check yourself

Training loss oscillates or becomes NaN. What is the likely cause?

Show answer

A learning rate that is too high.

Exam Tips

Remember for the exam

  • Steps per epoch = examples / batch size.
  • Loss oscillating or NaN: learning rate too high.
  • Out of memory: reduce batch size or use gradient accumulation.
  • Validation loss rising: too many epochs; use early stopping.
  • Raise the learning rate when increasing batch size.