Grid search is an exhaustive hyperparameter optimization method that evaluates a model at every combination of predefined values for its hyperparameters and selects the configuration with the best validation score. In optimization for artificial intelligence, it is the simplest form of hyperparameter search: the analyst defines a discrete set of candidate values per hyperparameter, the method forms their Cartesian product, and each point of that grid is trained and scored independently.
Grid search is valued for being deterministic, transparent and trivially parallel, but its cost grows exponentially with the number of hyperparameters. For that reason, practitioners treat it as a sound choice for small, well-understood search spaces and as a baseline against which random search, Hyperband and Bayesian optimization are judged.
How grid search enumerates a search space
Grid search works by turning a continuous or mixed search space into a finite lattice of candidate configurations. For each hyperparameter, the user lists a small number of values, often on a logarithmic scale for quantities such as the learning rate or the regularization strength. The algorithm then visits every combination, which makes it a brute-force, derivative-free procedure that needs no gradient of the objective function.
Each grid point is scored with a validation protocol, most often k-fold cross-validation. The objective is typically a metric such as accuracy, F1 score, log loss or root mean squared error, averaged over folds. After all points are evaluated, the configuration with the best mean score is refit on the full training set and finally assessed on a held-out test set that played no part in the search.
The following table summarizes the elements that define a grid search run and what each one controls.
| Component | What it specifies | Typical choice |
|---|---|---|
| Hyperparameter list | Which settings are tuned | Two to four influential settings |
| Value grid | Candidate values per setting | Three to seven values, often log-spaced |
| Scoring metric | What counts as better | Accuracy, F1, AUC, RMSE |
| Validation scheme | How each point is scored | 5-fold or 10-fold cross-validation |
| Refit rule | What happens after the search | Retrain the best point on all training data |
Computational cost and the curse of dimensionality
The total number of model fits equals the product of the grid sizes multiplied by the number of validation folds. With d hyperparameters and n values each, the grid contains n to the power d points, so adding one more hyperparameter multiplies the cost rather than adding to it. This exponential growth is a direct instance of the curse of dimensionality.
The table below shows how quickly the number of fits grows under 5-fold cross-validation.
| Hyperparameters | Values per hyperparameter | Grid points | Model fits |
|---|---|---|---|
| 2 | 5 | 25 | 125 |
| 3 | 5 | 125 | 625 |
| 4 | 5 | 625 | 3,125 |
| 5 | 5 | 3,125 | 15,625 |
| 6 | 5 | 15,625 | 78,125 |
A second and subtler cost is poor coverage of important dimensions. When only one or two hyperparameters strongly affect performance, which is common, a grid of 25 points tests only five distinct values of the important one. The remaining evaluations repeat those same values while varying settings that barely matter. This observation underlies the well-known argument that random search often finds better configurations at the same budget.
Grid search compared with other hyperparameter search methods
Grid search sits at the simplest end of a family of methods that differ in how they choose the next configuration to evaluate. The comparison below covers its closest neighbors within hyperparameter optimization.
| Method | How points are chosen | Main strength | Main weakness |
|---|---|---|---|
| Grid search | Every combination of fixed values | Exhaustive and reproducible | Exponential cost in dimensions |
| Random search | Independent random samples | Better coverage per budget | No guarantee of hitting a value |
| Bayesian optimization | Surrogate model and acquisition function | Sample efficient for costly fits | Sequential and harder to parallelize |
| Hyperband | Early stopping of weak configurations | Saves compute on long training runs | Needs a meaningful partial budget |
| Tree-structured Parzen estimator | Density models of good and bad points | Handles conditional spaces well | Less transparent than a fixed grid |
Grid search does not learn from earlier evaluations, so it is an uninformed, non-adaptive method. Bayesian optimization and the tree-structured Parzen estimator use past results to concentrate effort near promising regions, while Hyperband reallocates compute toward configurations that look strong early. In exchange, grid search offers perfect reproducibility and results that are easy to visualize as heat maps.
Designing an effective grid
A good grid reflects prior knowledge about scale and sensitivity. Parameters that act multiplicatively, such as the learning rate, the C parameter of a support vector machine or the L2 penalty, should be spaced geometrically, for example 0.001, 0.01, 0.1 and 1. Integer parameters such as tree depth or the number of neighbors can use linear steps.
Many practitioners run a coarse-to-fine search: a wide, sparse grid first locates the promising region, and a second, denser grid refines it. If the best value lands on the edge of the grid, the true optimum may lie outside the tested range, so the grid should be extended in that direction before drawing conclusions.
The decision table below matches common situations with a suitable search strategy.
| Situation | Recommended approach | Reason |
|---|---|---|
| One to three hyperparameters, cheap model | Full grid search | Exhaustive coverage is affordable |
| Four or more hyperparameters | Random search or Bayesian optimization | Grid cost explodes exponentially |
| Deep network with long training | Hyperband or Bayesian optimization | Each fit is too expensive to waste |
| Need an auditable, repeatable study | Grid search with a fixed seed | Every point is known in advance |
| Best value on the grid boundary | Extend or shift the grid | The optimum may lie outside the range |
Limitations and common mistakes
The most serious mistake is selection bias from reusing the validation data. When hundreds of configurations are compared on the same folds, the best score is optimistically biased, so the winning number overstates real performance. Nested cross-validation or a separate test set gives an honest estimate.
- Data leakage: fitting scalers or feature selectors outside the cross-validation loop contaminates every grid point.
- Linear spacing for scale parameters: testing 0.1, 0.2 and 0.3 for a learning rate wastes most of the budget.
- Ignoring interactions: tuning one hyperparameter at a time is not grid search and can miss joint optima.
- Overly fine grids: differences below the noise of cross-validation are not meaningful.
- Conditional parameters: values that only apply under certain settings create redundant or invalid points.
Grid search also assumes that the optimum lies near a grid point. For smooth objectives this is usually acceptable, but a narrow optimum between two tested values can be missed entirely, which no amount of exhaustive enumeration over the same grid will correct.
In plain words
A machine learning model has settings that someone must choose before training. Grid search tries every combination of a few chosen values. It then keeps the combination that scores best.
Think of tuning an oven. You test three temperatures and three baking times, so you bake nine cakes. You taste each one and pick the best. That works well with two dials. With six dials, the number of cakes grows so fast that testing them all becomes impractical. That is why people switch to smarter methods when there are many settings.
A real-world example
A data scientist at a regional bank is building a support vector machine to flag loan applications for manual review. The training set has about 40,000 labeled applications. Two hyperparameters matter most: the penalty C and the kernel width gamma of the radial basis function kernel.
She defines a grid of six log-spaced values for each, from 0.001 to 100, giving 36 configurations. With 5-fold cross-validation, the search requires 180 model fits, which finish in under an hour on a multicore server. The best point is C equal to 10 and gamma equal to 0.01, with a cross-validated F1 score of 0.81.
Because the best gamma sits in the middle of the range, she adds a finer grid around that region and confirms the choice. The refit model reaches an F1 score of 0.80 on a held-out test set, which shows that the validation estimate was only slightly optimistic.