tensorturnBETA
Home › Machine Learning Tasks

Hyperparameter Tuning: Grid Search vs Random Search vs Bayesian Optimization

Hyperparameter tuning is the search for the model settings you choose before training, such as learning rate, tree depth or regularization strength, that give the best performance on held-out data. Grid search tries every combination and is only practical for two or three parameters; random search is a better default for larger spaces; Bayesian optimization uses past trials to pick promising settings and finds good values in fewer runs. Whatever method you use, tune on validation data or cross-validation and keep a test set untouched, or you will overfit to the validation set. On TensorTurn you can ask for a grid or randomized search in plain English and it adds it to the generated notebook.

Parameters vs hyperparameters

Parameters are what the model learns from data: the weights of a neural network, the split points of a tree, the coefficients of a regression. Hyperparameters are the settings that control how learning happens, and you set them before training: how deep trees may grow, how big each gradient step is, how strongly to penalise complexity. The same algorithm can perform very differently depending on them, which is why tuning matters.

That said, tuning has diminishing returns. Good data, sensible features and a correct validation setup usually matter more. Tune once the pipeline is right, not before.

Grid vs random vs Bayesian search

MethodHow it worksProsConsUse when
Manual tuningChange one setting at a time based on intuitionBuilds understanding, cheap for a few triesSlow, biased, misses interactionsEarly exploration
Grid searchEvaluates every combination of listed valuesExhaustive, simple, reproducibleCost explodes with each added parameter; wastes trials on unimportant onesTwo or three parameters with few values
Random searchSamples combinations at random from rangesCovers important parameters better than grid for the same budget, easy to paralleliseNo learning from earlier trialsDefault for four or more parameters
Bayesian optimization (for example TPE or Gaussian processes)Builds a model of score versus settings and picks the most promising next trialFinds good settings in fewer trialsSequential by nature, more setupEach training run is expensive
Successive halving and HyperbandStarts many configurations with a small budget and only continues the bestVery efficient when early performance predicts final performanceCan drop slow starters too earlyNeural networks and large boosting runs

Random search beats grid search in most real problems because usually only a few hyperparameters really matter. A grid spends most of its budget repeating the same values of the important ones, while random sampling tries many more distinct values of each.

Key hyperparameters by model family

ModelHyperparameters that matter mostTips
Logistic regressionC (inverse regularization strength), penalty typeSearch C on a log scale, for example 0.001 to 100
Random forestn_estimators, max_depth, min_samples_leaf, max_featuresMore trees rarely hurt; max_features and min_samples_leaf control overfitting
XGBoost / LightGBMlearning_rate, number of trees with early stopping, max_depth or num_leaves, min_child_weight or min_data_in_leaf, subsample, colsample_bytree, L1 and L2 regularizationFix a smallish learning rate, let early stopping choose the number of trees, then tune tree shape
Support vector machineC, kernel, gammaScale features first; search C and gamma together on log scales
Neural networksLearning rate, batch size, epochs with early stopping, weight decay, dropout, layer width and depthLearning rate is almost always the most important; try a learning rate finder or a log-scale sweep
K-meansNumber of clusters k, initializationChoose k with silhouette and elbow analysis, use multiple restarts

Overfitting to the validation set

Every time you look at a validation score and change something, a little information about the validation data leaks into your choices. Run hundreds of trials and pick the single best, and part of that score is luck: the configuration that happened to fit the quirks of that particular validation split. The reported number becomes optimistic, sometimes badly so on small datasets.

Practical tuning tips

How to tune hyperparameters on TensorTurn

Start building free

Frequently asked questions

What is the difference between grid search and random search?

Grid search tries every combination from a fixed list of values, while random search samples combinations from ranges. For the same number of trials, random search usually finds better settings when there are more than two or three hyperparameters, because it explores more distinct values of the ones that matter.

Is Bayesian optimization always better?

It usually needs fewer trials, which matters when each training run is expensive. For cheap models with plenty of compute, random search is simpler, parallelises easily and is often just as good.

Which hyperparameters should I tune first?

For neural networks and gradient boosting, start with the learning rate and use early stopping for the number of epochs or trees. Then tune model complexity, such as tree depth or number of leaves, and finally regularization.

How do I avoid overfitting during hyperparameter tuning?

Tune with cross-validation or a separate validation set, keep a test set that you use only once at the end, and prefer simpler configurations when scores are close. For small datasets, nested cross-validation gives the most honest estimate.

Is hyperparameter tuning the same as AutoML?

Tuning is one part of AutoML. AutoML systems typically also automate preprocessing, model selection and ensembling, while hyperparameter tuning only searches the settings of a chosen model.