Install KerasTuner
masterKerasTuner requires Python 3.8+ and TensorFlow 2.0+. You can install the latest release using pip.
pip install keras-tunerrepository·master·Indexed 25 days ago
https://github.com/keras-team/keras-tunerA scalable hyperparameter optimization framework for simplifying the search for optimal model hyperparameters. Supports algorithms including Bayesian Optimization, Hyperband, and Random Search. Requires Python 3.8+ and TensorFlow 2.0+.
KerasTuner requires Python 3.8+ and TensorFlow 2.0+. You can install the latest release using pip.
pip install keras-tunerKerasTuner allows you to automate hyperparameter optimization. The workflow involves:
hp argument. Use this hp object to define your search space (e.g., using hp.Choice).RandomSearch, BayesianOptimization, or Hyperband) and specify the objective (the metric to optimize) and max_trials (the number of hyperparameter combinations to test).tuner.search() with your training data and hyperparameters.tuner.get_best_models() to access the top-performing model.import keras_tuner
from tensorflow import keras
# 1. Define the model building function with hyperparameter definitions
def build_model(hp):
model = keras.Sequential()
model.add(keras.layers.Dense(
hp.Choice('units', [8, 16, 32]),
activation='relu'))
model.add(keras.layers.Dense(1, activation='relu'))
model.compile(loss='mse')
return model
# 2. Initialize the tuner
tuner = keras_tuner.RandomSearch(
build_model,
objective='val_loss',
max_trials=5)
# 3. Start the search
tuner.search(x_train, y_train, epochs=5, validation_data=(x_val, y_val))
# 4. Get the best model
best_model = tuner.get_best_models()[0]