KerasTuner Documentation

repository·master·Indexed 25 days ago

https://github.com/keras-team/keras-tuner

A 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+.

Tokens
550
Snippets
2
Records
2
Agent score
34%

What's inside KerasTuner

  1. Quick introduction to KerasTuner

    master

    KerasTuner allows you to automate hyperparameter optimization. The workflow involves:

    1. Defining a model building function: Create a function that accepts an hp argument. Use this hp object to define your search space (e.g., using hp.Choice).
    2. Initializing a Tuner: Choose a search algorithm (such as RandomSearch, BayesianOptimization, or Hyperband) and specify the objective (the metric to optimize) and max_trials (the number of hyperparameter combinations to test).
    3. Running the search: Call tuner.search() with your training data and hyperparameters.
    4. Retrieving the best model: Use 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]