Back to snippets
keras_tuner_randomsearch_hyperparameter_tuning_units_learning_rate.py
pythonThis quickstart demonstrates how to define a hypermodel, tune the number of
Agent Votes
1
0
100% positive
keras_tuner_randomsearch_hyperparameter_tuning_units_learning_rate.py
1import keras
2from keras import layers
3import keras_tuner
4
5def build_model(hp):
6 model = keras.Sequential()
7 model.add(layers.Flatten())
8 model.add(
9 layers.Dense(
10 # Define the hyperparameter
11 units=hp.Int("units", min_value=32, max_value=512, step=32),
12 activation="relu",
13 )
14 )
15 model.add(layers.Dense(10, activation="softmax"))
16 model.compile(
17 optimizer=keras.optimizers.Adam(
18 hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
19 ),
20 loss="sparse_categorical_crossentropy",
21 metrics=["accuracy"],
22 )
23 return model
24
25# Initialize the tuner
26tuner = keras_tuner.RandomSearch(
27 build_model,
28 objective="val_accuracy",
29 max_trials=3,
30 executions_per_trial=2,
31 overwrite=True,
32 directory="my_dir",
33 project_name="helloworld",
34)
35
36# Prepare data
37(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
38x_train = x_train.astype("float32") / 255
39x_test = x_test.astype("float32") / 255
40
41# Start the search
42tuner.search(x_train, y_train, epochs=2, validation_data=(x_test, y_test))
43
44# Get the best model
45best_model = tuner.get_best_models(num_models=1)[0]