๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #386

Grid Search

Grid search is the simplest systematic hyperparameter search strategy: define a discrete set of candidate values for each hyperparameter, then exhaustively try every possible combination.

The Core Idea

learning_rates = [1e-4, 1e-3, 1e-2]
batch_sizes = [32, 64, 128]
dropout_rates = [0.2, 0.5]

best_config, best_val_acc = None, 0
for lr in learning_rates:
    for bs in batch_sizes:
        for dropout in dropout_rates:
            model = build_model(dropout=dropout)
            train(model, lr=lr, batch_size=bs)
            val_acc = evaluate(model, val_loader)
            if val_acc > best_val_acc:
                best_val_acc, best_config = val_acc, (lr, bs, dropout)
print(best_config, best_val_acc)

The Combinatorial Explosion Problem

\[ \text{total combinations} = \prod_i |\text{values}_i| \]

For the example above: \(3\times3\times2=18\) combinations โ€” manageable. But grid search's cost grows multiplicatively with each additional hyperparameter and each additional candidate value per hyperparameter โ€” 5 hyperparameters with 4 values each would require \(4^5=1024\) full training runs, quickly becoming computationally infeasible for anything beyond a handful of hyperparameters with a handful of values each.

Using scikit-learn's GridSearchCV

from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier

param_grid = {'hidden_layer_sizes': [(64,), (128,)], 'alpha': [1e-4, 1e-3], 'learning_rate_init': [1e-3, 1e-2]}
grid_search = GridSearchCV(MLPClassifier(max_iter=200), param_grid, cv=3)
grid_search.fit(X_train, y_train)
print(grid_search.best_params_)

When Grid Search Still Makes Sense

Despite its scaling limitations, grid search remains a reasonable choice when tuning just one or two hyperparameters with a small number of candidate values each โ€” its exhaustiveness guarantees the best combination within the specified grid will be found, with no risk of randomly missing a good combination the way random search (next note) theoretically could.

Common Mistakes

  • Applying grid search to more than a handful of hyperparameters simultaneously โ€” the combinatorial explosion quickly makes this computationally infeasible for real deep learning training runs, where each individual combination can itself take hours.
  • Choosing grid values without any informed reasoning (e.g. arbitrary, evenly-spaced values on a linear scale for a hyperparameter like learning rate, which behaves more naturally on a logarithmic scale) โ€” poorly chosen grid points can miss the actually useful region of the search space entirely.

Interview Relevance

Q: "Why does grid search become impractical as the number of hyperparameters being tuned increases?" Its total cost is the product of the number of candidate values across every hyperparameter โ€” this grows multiplicatively (combinatorially), not additively, with each new hyperparameter or additional candidate value. Even a modest number of hyperparameters with a modest number of values each can require an infeasibly large number of full training runs, especially when each individual run (as with most deep learning training) is itself computationally expensive.

Practice Question

If you're tuning 4 hyperparameters, each with 5 candidate values, how many total training runs would exhaustive grid search require?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Grid Search โ€“ FAQs

Quick answers about learning Grid Search in Deep Learning.

This free note from CodingNow 2.0 explains Grid Search in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Grid Search, is 100% free with no signup required.
With focused practice, most students grasp Grid Search in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now