Understanding Grid Search as an Optimization Algorithm in Machine Learning

Written by Keaun Amani

Published 2023-4-13

Grid search is an optimization technique used to brute force through all possible combinations of a set of variables. Fundamentally it's one of the most basic and simple optimization algorithms, but it's still quite powerful and guarantees finding the most optimal solution to your problem.

It works by creating a grid of all possible combinations of parameter values and testing each combination to find the best one. This grid of parameters is defined before the optimization/search step, hence the name grid search.

Applications in Machine Learning

One application of grid search is in hyperparameter tuning, a commonly used technique for optimizing machine learning models. Machine learning models have several parameters that can be adjusted, known as hyperparameters. These hyperparameters have a significant impact on model performance making it crucial to find an optimized combination so that we can build good models.

Diagram of a 2-dimensional grid search between two hyperparameters.

Diagram of a 2-dimensional grid search between two hyperparameters.

Algorithmic Steps

We'll provide python code for this below. 1. Define a range of values for each parameter. 2. The grid search algorithm then creates a matrix of all possible combinations of parameter values. 3. Each combination is then ran through an objective function that then returns a score used to rank combinations. In the case of hyperparameter tuning, each combination is used to train and evaluate the machine learning model, and the performance metric is recorded. 4. The performance metric could be any metric relevant to the specific problem, such as accuracy or mean squared error. 5. After evaluating all possible combinations, the combination that resulted in the best performance is chosen. In our case, the model is then trained again, this time using the best combination of hyperparameters, and the final model is used for prediction.

Conclusion

One of the advantages of grid search is that it's a simple and straightforward method. It can be used with any machine learning model and any performance metric. Grid search also ensures that all possible combinations of hyperparameters are tested, so the best combination is guaranteed to be found. However, grid search can be computationally expensive and time-consuming, especially if there are many hyperparameters and a large range of values for each hyperparameter.

In conclusion, grid search is a popular hyperparameter tuning technique used in machine learning. It is a simple and effective way of finding the best combination of hyperparameters for a model. Although it can be computationally expensive, grid search is a reliable method for optimizing machine learning models.

Example Code

The following code uses the sklearn ParameterGrid implementation to perform a grid search over a defined parameter space. Make sure you install sklearn via pip using something like pip install -U sklearn.

from sklearn.model_selection import ParameterGrid

# define a search space using relevant metrics
space = {
  "learning_rate": [1e-5, 1e-4, 1e-3,1e-2, 1e-1],
  "test_prcnt": [10, 20, 25],
  "some_other_parameter": [1,2,3,4,5,6,7,8,9,10],
}

# set grid the grid
grid = ParameterGrid(space)

# define an objective function
def objective(args):
  # CODE TO TRAIN MODEL WITH INPUT "args"
  # CODE TO EVALUATE MODEL
  loss = 0
  return loss # return loss, accuracy, etc...

# Calculate the total number of combinations
prod = 1
for v in space.values():
  prod *= len(v)
print(f"[*] Total number of combinations to test {prod}")

# minimize the objective over the parameter space
best_loss = float("inf")
best = None
for i,x in enumerate(grid):
  loss = objective(x)
  print(f"[{i}] loss={loss:.2f}    ", end="\r", flush=True)
  if loss < best_loss:
    print("="*25, "NEW BEST", "="*25)
    print({k:round(v,4) for k,v in x.items()})
    print("-"*60)
    best_loss = loss
    best = x

print(f"[*] Best loss {loss}")
print(f"[*] Best set of parameters {best}")

Accelerate your lab's
research today

Register for free — upgrade anytime.

Interested in getting a license? Contact Sales.

Sign up free