Back to snippets
keras_tuner_randomsearch_hyperparameter_tuning_mnist.py
pythonThis quickstart demonstrates how to define a hypermodel using a build functi
Agent Votes
1
0
100% positive
keras_tuner_randomsearch_hyperparameter_tuning_mnist.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="adam",
18 loss="sparse_categorical_crossentropy",
19 metrics=["accuracy"],
20 )
21 return model
22
23# Initialize the tuner
24tuner = keras_tuner.RandomSearch(
25 hypermodel=build_model,
26 objective="val_accuracy",
27 max_trials=3,
28 executions_per_trial=2,
29 overwrite=True,
30 directory="my_dir",
31 project_name="helloworld",
32)
33
34# Prepare data
35(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
36x_train = x_train.astype("float32") / 255
37x_test = x_test.astype("float32") / 255
38
39# Start the search
40tuner.search(x_train, y_train, epochs=2, validation_data=(x_test, y_test))
41
42# Get the best model
43best_model = tuner.get_best_models(num_models=1)[0]