Grid search, also known as a parameter sweep, is a traditional method for hyperparameter optimization in machine learning. It involves exhaustively searching through a manually specified subset of the hyperparameter space of a learning algorithm. The algorithm is guided by a performance metric, typically measured by Cross-Validation on the training set or evaluation on a hold-out validation set. Since the parameter space may include real-valued or unbounded values, manually set bounds and discretization are often necessary before applying grid search.
For example, a typical soft-margin support vector machine (SVM) classifier with an RBF kernel has at least two hyperparameters: a regularization constant C and a kernel hyperparameter γ. Both are continuous, so one selects finite sets of reasonable values, such as C ∈ {10, 100, 1000} and γ ∈ {0.1, 0.2, 0.5, 1.0}. Grid search trains an SVM with each pair (C, γ) in the Cartesian product of these sets and evaluates performance on a held-out validation set or via internal cross-validation. It then outputs the settings achieving the highest validation score.
Advantages and Limitations
Grid search is conceptually simple and easy to implement, making it a common baseline. It is often embarrassingly parallel because the hyperparameter settings it evaluates are typically independent of each other, allowing for efficient distribution across multiple processors or machines. However, it suffers from the curse of dimensionality: the number of evaluations grows exponentially with the number of hyperparameters. For continuous parameters, grid search only explores a finite set of discrete values, which can miss optimal configurations that lie between grid points. Additionally, it can waste computational resources on irrelevant dimensions when only a few hyperparameters significantly affect performance.
Comparison with Random Search
Random search replaces exhaustive enumeration by selecting hyperparameter combinations randomly. It can explore many more values for continuous hyperparameters and can outperform grid search, especially when only a small number of hyperparameters affect the final performance (low intrinsic dimensionality). Random search is also embarrassingly parallel and allows inclusion of prior knowledge via sampling distributions. Despite its simplicity, random search remains an important baseline against which new hyperparameter optimization methods are compared.
Relationship to Other Optimization Methods
Grid search is often contrasted with more advanced techniques. Bayesian optimization builds a probabilistic model of the function mapping hyperparameters to the objective, balancing exploration and exploitation, and typically achieves better results in fewer evaluations. Gradient-based optimization methods compute gradients with respect to hyperparameters, enabling optimization via gradient descent, though they are limited to specific algorithms. Evolutionary optimization uses population-based search inspired by biological evolution, which can handle discrete and continuous spaces. Neural architecture search often employs gradient-based or evolutionary methods to optimize architecture hyperparameters.
Practical Considerations
When applying grid search, practitioners must decide on the range and discretization of each hyperparameter, which requires domain knowledge. The choice of performance metric, such as cross-validation accuracy or loss function value, is critical. Grid search is computationally expensive for large models or datasets, and its exhaustive nature can be prohibitive. In practice, it is often used for small parameter spaces or as a baseline, with more efficient methods preferred for high-dimensional or expensive evaluations. The results of grid search can be sensitive to the chosen grid, and there is no guarantee of finding the global optimum.
Historical Context and Usage
Grid search has been a standard approach since the early days of machine learning, predating modern optimization techniques. It is widely documented in textbooks and implemented in libraries such as scikit-learn. While newer methods like Bayesian optimization have gained popularity, grid search remains a fundamental tool for understanding hyperparameter effects and for benchmarking. Its simplicity makes it a reasonable starting point for small problems, and its parallelizability allows scaling to moderate sizes. However, for modern deep learning models with many hyperparameters, grid search is often infeasible, leading to the adoption of random search or more sophisticated methods.