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
| Method | How it works | Pros | Cons | Use when |
|---|---|---|---|---|
| Manual tuning | Change one setting at a time based on intuition | Builds understanding, cheap for a few tries | Slow, biased, misses interactions | Early exploration |
| Grid search | Evaluates every combination of listed values | Exhaustive, simple, reproducible | Cost explodes with each added parameter; wastes trials on unimportant ones | Two or three parameters with few values |
| Random search | Samples combinations at random from ranges | Covers important parameters better than grid for the same budget, easy to parallelise | No learning from earlier trials | Default 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 trial | Finds good settings in fewer trials | Sequential by nature, more setup | Each training run is expensive |
| Successive halving and Hyperband | Starts many configurations with a small budget and only continues the best | Very efficient when early performance predicts final performance | Can drop slow starters too early | Neural 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
| Model | Hyperparameters that matter most | Tips |
|---|---|---|
| Logistic regression | C (inverse regularization strength), penalty type | Search C on a log scale, for example 0.001 to 100 |
| Random forest | n_estimators, max_depth, min_samples_leaf, max_features | More trees rarely hurt; max_features and min_samples_leaf control overfitting |
| XGBoost / LightGBM | learning_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 regularization | Fix a smallish learning rate, let early stopping choose the number of trees, then tune tree shape |
| Support vector machine | C, kernel, gamma | Scale features first; search C and gamma together on log scales |
| Neural networks | Learning rate, batch size, epochs with early stopping, weight decay, dropout, layer width and depth | Learning rate is almost always the most important; try a learning rate finder or a log-scale sweep |
| K-means | Number of clusters k, initialization | Choose 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.
- Keep a final test set that you evaluate exactly once, after all tuning is done.
- Use k-fold cross-validation for tuning on small and medium datasets so scores are averaged over several splits.
- Use nested cross-validation when you need an unbiased estimate of the whole tuning procedure.
- Prefer simpler or more regularized configurations when scores are within noise of each other.
- Respect structure: use time-based splits for time series and group splits when rows share an entity, both during tuning and testing.
Practical tuning tips
- Search learning rates and regularization strengths on a log scale, not a linear one.
- Use early stopping instead of tuning the number of epochs or trees directly.
- Start with wide ranges and a small budget, then narrow around the best region.
- Fix random seeds so differences come from the settings, not chance, and check the best result with a couple of seeds.
- Set a budget in advance; the last 1 percent of improvement is rarely worth days of compute.
How to tune hyperparameters on TensorTurn
- Upload and check your data first: target analysis and leakage detection catch the problems that no amount of tuning can fix.
- Describe the model and the search in chat, for example: train LightGBM with early stopping and run a randomized search over num_leaves, min_data_in_leaf and learning rate with 5-fold cross-validation, then report the score on a held-out test set.
- TensorTurn generates the notebook using standard tools such as scikit-learn's GridSearchCV or RandomizedSearchCV, and runs it on cloud GPUs or free on your own connected GPUs. You can edit the search ranges directly in the cells.
- If a trial fails, the AI reads the traceback, patches the broken cell and retries.
- Deploy the tuned model as a /predict endpoint or export it to ONNX.
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.