Hyperparameter Tuning

By Updated

Experiment with automated search strategies. Use Grid Search to systematically scan parameters or Random Search to find high-performance zones efficiently.

Overview

Two kinds of number

A parameter is learned: weights and biases move during training because gradient descent moves them. A hyperparameter is fixed before training and never updated by the optimiser — learning rate, number of layers, neurons per layer, batch size, dropout rate, regularisation strength.

The distinction matters because you cannot use gradient descent to find hyperparameters. There is no derivative of validation accuracy with respect to “number of layers”. The only way to evaluate a hyperparameter setting is to train a model with it and see what happens, which is why tuning is expensive.

0.03
4

Tuning Dashboard

READY
Trial Status
-- / --
Best Accuracy
0.0%

Search History

# LR Nodes Accuracy
Start a search to see trials.
Drag to Pan | Scroll to Zoom

Hyperparameter Tuning: A Practical Guide

Parameters are learned from data; hyperparameters are chosen before training starts. Searching that second set well is often worth more than any change to the model itself.

Grid search, random search, and why random usually wins

Grid search tries every combination on a predefined grid. With 5 learning rates and 5 layer widths that is 25 runs, and adding a third hyperparameter with 5 values makes it 125. The cost is exponential in the number of hyperparameters.Random search samples combinations at random from ranges you specify. Counterintuitively it usually finds better settings for the same budget, and the reason is sharp: not all hyperparameters matter equally. If learning rate dominates and layer width barely matters, a 5×5 grid tests only 5 distinct learning rates across 25 runs. Random search with 25 samples tests 25 distinct learning rates. You get five times the resolution on the axis that counts, at no extra cost.Bayesian optimisation goes further, building a model of which regions look promising and sampling there. It is more efficient per trial and worth the complexity when each run takes hours.

The settings you choose rather than learn

Parameters are learned by gradient descent. Hyperparameters are the settings you choose before training starts, and they decide how well that learning goes.

Not all of them matter equally, and the difference is large enough that tuning them in the wrong order wastes most of the effort:

HyperparameterImpactTypical range
Learning rateEnormous1e-5 to 1e-1, log scale
Batch sizeModerate32–256
Number of epochsModerateDecided by early stopping
Weight decayModerate1e-5 to 1e-1, log scale
Architecture depth / widthModerateProblem-dependent
Dropout rateSmall to moderate0.0–0.5
Optimiser choiceSmallAdamW by default
Adam's β valuesNegligibleLeave at defaults

The learning rate deserves its position. It affects results more than the number of layers, the width, or which optimiser you pick — and it is the cheapest to find, via a short exponential sweep.

This is the most useful practical result in the topic, and it is counterintuitive.

A grid search over 3 learning rates and 3 dropout rates tries 9 combinations — but only 3 distinct learning rates. If the learning rate is what matters and dropout barely does, you spent 9 runs to sample 3 values of the important parameter.

Random search with 9 trials samples 9 distinct learning rates. For the same budget it explores the important dimension three times as thoroughly, and the result generalises: when only a few hyperparameters matter, random search finds better configurations than grid search at equal cost.

Bayesian optimisation goes further by modelling the relationship between settings and score, and choosing each next trial where the expected improvement is highest. Optuna and similar libraries implement this, and they add another useful feature — pruning, which abandons a trial early once it is clearly losing.

import optuna

def objective(trial):
    lr = trial.suggest_float("lr", 1e-5, 1e-1, log=True)
    wd = trial.suggest_float("weight_decay", 1e-6, 1e-1, log=True)
    dropout = trial.suggest_float("dropout", 0.0, 0.5)
    return train_and_validate(lr=lr, weight_decay=wd, dropout=dropout)

study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=50)
print(study.best_params)

Note log=True on the learning rate and weight decay. Both span orders of magnitude, and sampling them uniformly wastes almost every trial in the top decade.

An order that does not waste effort

  1. Get a working baseline first. One run, sensible defaults, no tuning. Everything is measured against this.
  2. Find the learning rate with an exponential sweep. Biggest gain, smallest cost.
  3. Fix the schedule — warm-up plus cosine decay.
  4. Set the batch size to what fits, scaling the rate accordingly.
  5. Tune regularisation — weight decay, then dropout — guided by the train/validation gap.
  6. Adjust architecture if the model is clearly under- or over-capacity.
  7. Random or Bayesian search over whatever remains, if the budget allows.

Two habits that matter more than the search algorithm: change one thing at a time so you can attribute the effect, and fix the random seed so a 1% difference is a real difference rather than run-to-run variation.

Which knobs matter, and how to search them

Hyperparameters are not equal -- a few dominate and the rest are noise. This measures which is which on a real training loop, then compares three ways of searching.

example_01.pyNumPy
Output

Things to try

  1. Run a small search. Set N Trials to 5 and press Run Trial repeatedly. Results scatter widely — five samples is not enough to distinguish a good region from luck.
  2. Give it a real budget. Press Reset Search, set N Trials to 20, and run again. A pattern emerges: certain learning-rate bands consistently do better regardless of the other settings.
  3. Compare the strategies. Switch Search Strategy between grid and random at the same N Trials. Random covers the learning-rate axis far more finely, because grid keeps re-testing the same few values.
  4. Test which knob matters. Hold Learning Rate fixed and vary Neurons/Layer across its range, then do the reverse. The learning rate changes the outcome far more — that asymmetry is the whole argument for random search.

Search on a log scale

Learning rate should be sampled logarithmically, not uniformly. Sampling uniformly from 0.0001 to 0.1 puts 90% of the samples above 0.01, leaving the small-rate region — where the answer usually is — almost untested.

Sample the exponent instead: draw uniformly from −4 to −1 and use 10x. That gives equal attention to 0.0001–0.001, 0.001–0.01, and 0.01–0.1. The same applies to regularisation strength and any other quantity that spans orders of magnitude.

What trips people up

  • Tuning on the test set. The single most damaging mistake. Selecting hyperparameters by test performance leaks the test set into the model and the reported score becomes optimistic. Tune on a validation split and touch test once, at the end.
  • Sampling learning rate uniformly. Wastes most of the budget in a range that is almost always too large.
  • Tuning everything at once with no budget. Start with learning rate, which usually dominates, then batch size and architecture. Regularisation is worth tuning only once the model can overfit.
  • Ignoring seed variance. If two settings differ by less than the run-to-run variation from random initialisation, you have not measured a difference. Repeat the promising ones.

Key takeaway

Hyperparameters sit outside gradient descent, so the only way to evaluate them is to train and measure — which makes the search strategy itself worth thinking about. Random search beats grid search at equal budget because it spends its samples on more distinct values of whichever hyperparameter actually matters, and anything spanning orders of magnitude should be sampled on a log scale. Whatever the strategy, select on validation data and keep the test set untouched.

Not tuning on the test set

Every hyperparameter decision made by looking at a score consumes some of that set's independence. Search fifty configurations against a validation set and keep the best, and its score is optimistic — you selected the luckiest of fifty.

The disciplined arrangement is three splits: train fits the parameters, validation chooses the hyperparameters, and test is looked at once, at the end, to produce the number you report.

For a rigorous estimate, nested cross-validation puts the hyperparameter search inside an outer loop whose test folds never participate in tuning. It costs k times more compute, and it is what you want when the number will inform a real decision.

The pragmatic middle ground — cross-validation for tuning, plus one genuinely untouched hold-out set for the final number — is what most projects should do.

Budget, and knowing when to stop

Hyperparameter search has diminishing returns, and it is worth knowing roughly where they set in.

The first few trials usually find most of the available gain, because they fix the learning rate. Trials ten to fifty refine it. Beyond a hundred, improvements on a fixed dataset are typically fractions of a percent — at which point better data, better features or a different architecture is a far better use of the compute.

Two shortcuts worth knowing. Tune on a subset — 10–20% of the data — to find a promising region cheaply, then verify the best few configurations on the full dataset. And use early pruning, so a configuration that is clearly losing after two epochs does not consume twenty.

Questions people ask

What should I tune first? The learning rate, always. It dominates.

How many trials do I need? Twenty to fifty random or Bayesian trials cover most of the available gain for a handful of hyperparameters.

Grid or random search? Random, or Bayesian. Grid search wastes its budget on dimensions that do not matter.

Should I tune the architecture? Only after the training recipe is right, and prefer a proven architecture over designing one.

Do I need to tune Adam's betas? Almost never. The defaults are robust.

Why do my results change between runs with the same settings? Random initialisation, shuffling and non-deterministic GPU kernels. Fix the seeds, and treat differences smaller than the run-to-run variation as noise.

Recap in one screen

  • Hyperparameters are chosen, not learned, and they are not equally important.
  • The learning rate matters most and is the cheapest to find — sweep it first.
  • Random and Bayesian search beat grid search at equal budget, because only a few dimensions matter.
  • Sample learning rate and weight decay on a log scale.
  • Tune on validation, report on a test set you touched once, and fix the seed before comparing anything.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. Without scrolling back — what is the one-line takeaway from this module?

  2. What does this module say about “Two kinds of number”?

  3. What does this module say about “Grid search, random search, and why random usually wins”?

Cheat sheet

Hyperparameter Tuning

Experiment with automated search strategies. Use Grid Search to systematically scan parameters or Random Search to find high-performance zones efficiently.

DEEP LEARNING · vizlearn.in/deep_learning/hyper-paramter_tuning.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.