instal keras tuner code example

Example 1: keras tuner

pip install -U keras-tuner

Example 2: keras tuner

from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch

def build_model_function(hp):
    model = keras.Sequential()
    model.add(layers.Dense(units=hp.Int('units', min_value=32,
                           max_value=512, step=32), activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    model.compile(optimizer=keras.optimizers.Adam(
        hp.Choice('learning_rate',values=[1e-2, 1e-3, 1e-4])),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    return model
    
tuner = RandomSearch(
    build_model_function,
    objective='val_accuracy',
    max_trials=5,
    executions_per_trial=3,
    directory='my_dir',
    project_name='helloworld')
# You can print a summary of the search space:
tuner.search_space_summary()
# The call to search has the same signature as model.fit()
tuner.search(x, y, epochs=5, validation_data=(val_x, val_y))
# When search is over, you can retrieve the best model(s):
models = tuner.get_best_models(num_models=2)
# Or print a summary of the results:
tuner.results_summary()