keras tuner hyperparameters 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')
tuner.search_space_summary()
tuner.search(x, y, epochs=5, validation_data=(val_x, val_y))
models = tuner.get_best_models(num_models=2)
tuner.results_summary()