The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Use KerasTuner’s GridSearch to test a finite set of Keras model configurations. Define candidate values with a HyperParameters object, pass a model-building function and a validation metric to the tuner, then call tuner.search(). Before running it, calculate how many configurations the grid contains: 3 learning rates × 3 layer sizes × 3 dropout values means 27 trials. Choose the best configuration using validation data, and keep your final test set out of the search.
What grid search does—and when it is practical
Grid search evaluates every combination of the candidate values you specify. It is useful when you have a small, finite search space and want exhaustive, easy-to-audit coverage. Its main drawback is cost: as you add parameters or candidate values, the number of model fits grows multiplicatively.
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Deep Learning (Adaptive Computation and Machine Learning series) | $51.51 | Buy on Amazon |
| 2 |
|
Deep Learning: Foundations and Concepts | $48.83 | Buy on Amazon |
| 3 |
|
Understanding Deep Learning | $99.22 | Buy on Amazon |
| 4 |
|
Deep Learning (The MIT Press Essential Knowledge series) | $11.36 | Buy on Amazon |
| 5 |
|
Deep Learning: A Visual Approach | $66.77 | Buy on Amazon |
Calculate the trial count first
Multiply the number of candidates for each independent hyperparameter. For example, three learning rates, three unit counts, and three dropout rates create 3 × 3 × 3 = 27 configurations. That is 27 trials before any repeated runs or cross-validation folds. A setting such as max_trials can cap the work, but it does not make a large grid inexpensive; it may simply prevent some combinations from being evaluated.
Start with a compact grid focused on values that could plausibly change the result. For reproducibility, fix random seeds where appropriate and record each trial’s configuration and validation metric.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
- Language Published: English
- Binding: hardcover
- It ensures you get the best usage for a longer period
Define a KerasTuner grid
Install the KerasTuner package if it is not already available in your environment:
pip install keras-tuner
The following example searches 27 configurations: three learning rates, three hidden-layer sizes, and three dropout rates. It assumes n_features and n_classes are defined for your dataset, and that the training and validation arrays are prepared.
Rank #2
import keras
import keras_tuner
def build_model(hp):
model = keras.Sequential([
keras.layers.Input(shape=(n_features,)),
keras.layers.Dense(
units=hp.Choice("units", [64, 128, 256]),
activation="relu",
),
keras.layers.Dropout(
rate=hp.Choice("dropout", [0.0, 0.25, 0.5])
),
keras.layers.Dense(n_classes, activation="softmax"),
])
model.compile(
optimizer=keras.optimizers.Adam(
learning_rate=hp.Choice(
"learning_rate", [1e-2, 1e-3, 1e-4]
)
),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)
return model
tuner = keras_tuner.GridSearch(
hypermodel=build_model,
objective="val_accuracy",
max_trials=27,
directory="tuner_runs",
project_name="keras_grid",
)
early_stop = keras.callbacks.EarlyStopping(
monitor="val_loss",
patience=5,
restore_best_weights=True,
)
tuner.search(
x_train,
y_train,
epochs=50,
validation_data=(x_val, y_val),
callbacks=[early_stop],
)
best_hp = tuner.get_best_hyperparameters(num_trials=1)[0]
best_model = tuner.get_best_models(num_models=1)[0]
KerasTuner’s API can vary by installed version. Check the constructor and search arguments in the documentation for your installed release if this example does not match it exactly.
Choose candidates that match the parameter
hp.Choice() is appropriate for a finite list such as the three learning rates above. KerasTuner also provides integer and floating-point ranges, including stepped or logarithmic sampling. A grid requires finite candidates: for a stepped integer or float range, the step and endpoints determine the values to evaluate. Integer ranges include the stated maximum value when it falls on a step.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesRank #3
Not every hyperparameter needs to be tuned. You can leave model and compile settings fixed while searching a subset, or include settings such as the optimizer, loss, and metrics when they are part of the question you are trying to answer. KerasTuner’s hyperparameter definitions support conditional scopes for parameters that apply only to particular model branches; account for those branches when estimating the search size.
Run trials with validation data and callbacks
In the example, objective="val_accuracy" tells the tuner which validation metric to use when comparing trials. The validation_data argument supplies the held-out data for that measurement. Use a validation set for model selection; do not use the final test set to pick hyperparameters.
Pass callbacks to tuner.search() so they reach the model’s fit process during each trial. Early stopping can stop a trial when its monitored validation loss no longer improves, while restoring the best weights seen in that trial. Checkpointing and TensorBoard callbacks can also be supplied this way. KerasTuner’s getting-started guidance says fit-related keyword arguments should be passed through because callbacks are used for model saving and TensorBoard plugins.
Early stopping can reduce wasted epochs, but it does not reduce the number of configurations in the grid. In many setups it also makes tuning a fixed epoch count unnecessary: the callback can stop training based on validation behavior and retain the best weights.
Best Value
Retrieve the selected model and evaluate it fairly
After the search, get_best_hyperparameters() returns the selected settings, and get_best_models() returns the corresponding trained model or models. Inspect the best hyperparameters and trial metrics before moving on; the top result is the best among the configurations and runs you actually evaluated, not proof that the grid contained the globally best model.
Once you have selected a configuration using validation results, evaluate it against the untouched test set for a final estimate of performance. If you retrain the chosen configuration using both the original training and validation data, make that decision before the final test evaluation, and do not tune again based on the test result.
Choose between KerasTuner and scikit-learn
KerasTuner is the natural route when the model is built and trained as a Keras model. scikit-learn’s GridSearchCV performs exhaustive search over specified parameter values for an estimator and evaluates candidates with cross-validation. A Keras model can use that route only if it is exposed through an estimator interface compatible with scikit-learn.
| Option | Search coverage | Compute and validation | Keras integration | Best fit |
|---|---|---|---|---|
KerasTuner GridSearch |
Exhaustively evaluates the finite grid, subject to the configured trial limit. | Cost rises with the number of combinations; commonly compares trials using a validation metric. | Direct model-building-function workflow with Keras models and fit callbacks. | A small, finite search where complete candidate coverage matters. |
KerasTuner RandomSearch, BayesianOptimization, or Hyperband |
Uses an alternative search strategy rather than requiring exhaustive evaluation of every combination. | Useful alternatives when exhaustive search becomes too costly; actual work depends on the search configuration. | Built-in KerasTuner algorithms. | A larger search space where you need to limit or prioritize trial effort. |
scikit-learn GridSearchCV |
Exhaustive search over the specified values. | Uses cross-validated grid search, multiplying work across folds. | Requires a Keras model exposed through a compatible scikit-learn estimator interface. | A workflow where estimator compatibility and cross-validation are priorities. |
KerasTuner describes Bayesian Optimization, Hyperband, and Random Search as built-in optimization algorithms; its API also lists GridSearch and SklearnTuner. Prefer an alternative to exhaustive grid search when the product of candidate counts is no longer affordable, or when the search space is not naturally a small finite grid.
Recommended Free Tools
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

