![]() |
![]() |
![]() |
![]() |
Overview
The Keras Tuner is a library that helps you pick the optimal set of hyperparameters for your TensorFlow program. The process of selecting the right set of hyperparameters for your machine learning (ML) application is called hyperparameter tuning or hypertuning.
Hyperparameters are the variables that govern the training process and the topology of an ML model. These variables remain constant over the training process and directly impact the performance of your ML program. Hyperparameters are of two types:
- Model hyperparameters which influence model selection such as the number and width of hidden layers
- Algorithm hyperparameters which influence the speed and quality of the learning algorithm such as the learning rate for Stochastic Gradient Descent (SGD) and the number of nearest neighbors for a k Nearest Neighbors (KNN) classifier
In this tutorial, you will use the Keras Tuner to perform hypertuning for an image classification application.
Setup
import tensorflow as tf
from tensorflow import keras
2024-07-13 04:28:31.186022: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:479] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-07-13 04:28:31.212189: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:10575] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-07-13 04:28:31.212229: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1442] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
Install and import the Keras Tuner.
pip install -q -U keras-tuner
import keras_tuner as kt
Download and prepare the dataset
In this tutorial, you will use the Keras Tuner to find the best hyperparameters for a machine learning model that classifies images of clothing from the Fashion MNIST dataset.
Load the data.
(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0
Define the model
When you build a model for hypertuning, you also define the hyperparameter search space in addition to the model architecture. The model you set up for hypertuning is called a hypermodel.
You can define a hypermodel through two approaches:
- By using a model builder function
- By subclassing the
HyperModel
class of the Keras Tuner API
You can also use two pre-defined HyperModel classes - HyperXception and HyperResNet for computer vision applications.
In this tutorial, you use a model builder function to define the image classification model. The model builder function returns a compiled model and uses hyperparameters you define inline to hypertune the model.
def model_builder(hp):
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28, 28)))
# Tune the number of units in the first Dense layer
# Choose an optimal value between 32-512
hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
model.add(keras.layers.Dense(units=hp_units, activation='relu'))
model.add(keras.layers.Dense(10))
# Tune the learning rate for the optimizer
# Choose an optimal value from 0.01, 0.001, or 0.0001
hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
Instantiate the tuner and perform hypertuning
Instantiate the tuner to perform the hypertuning. The Keras Tuner has four tuners available - RandomSearch
, Hyperband
, BayesianOptimization
, and Sklearn
. In this tutorial, you use the Hyperband tuner.
To instantiate the Hyperband tuner, you must specify the hypermodel, the objective
to optimize and the maximum number of epochs to train (max_epochs
).
tuner = kt.Hyperband(model_builder,
objective='val_accuracy',
max_epochs=10,
factor=3,
directory='my_dir',
project_name='intro_to_kt')
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead. super().__init__(**kwargs)
The Hyperband tuning algorithm uses adaptive resource allocation and early-stopping to quickly converge on a high-performing model. This is done using a sports championship style bracket. The algorithm trains a large number of models for a few epochs and carries forward only the top-performing half of models to the next round. Hyperband determines the number of models to train in a bracket by computing 1 + logfactor
(max_epochs
) and rounding it up to the nearest integer.
Create a callback to stop training early after reaching a certain value for the validation loss.
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
Run the hyperparameter search. The arguments for the search method are the same as those used for tf.keras.model.fit
in addition to the callback above.
tuner.search(img_train, label_train, epochs=50, validation_split=0.2, callbacks=[stop_early])
# Get the optimal hyperparameters
best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""")
Trial 30 Complete [00h 00m 25s] val_accuracy: 0.8756666779518127 Best val_accuracy So Far: 0.8918333053588867 Total elapsed time: 00h 05m 43s The hyperparameter search is complete. The optimal number of units in the first densely-connected layer is 416 and the optimal learning rate for the optimizer is 0.001.
Train the model
Find the optimal number of epochs to train the model with the hyperparameters obtained from the search.
# Build the model with the optimal hyperparameters and train it on the data for 50 epochs
model = tuner.hypermodel.build(best_hps)
history = model.fit(img_train, label_train, epochs=50, validation_split=0.2)
val_acc_per_epoch = history.history['val_accuracy']
best_epoch = val_acc_per_epoch.index(max(val_acc_per_epoch)) + 1
print('Best epoch: %d' % (best_epoch,))
Epoch 1/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 4s 2ms/step - accuracy: 0.7834 - loss: 0.6144 - val_accuracy: 0.8597 - val_loss: 0.3901 Epoch 2/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8655 - loss: 0.3748 - val_accuracy: 0.8707 - val_loss: 0.3614 Epoch 3/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8805 - loss: 0.3306 - val_accuracy: 0.8768 - val_loss: 0.3432 Epoch 4/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8869 - loss: 0.3040 - val_accuracy: 0.8749 - val_loss: 0.3520 Epoch 5/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8938 - loss: 0.2865 - val_accuracy: 0.8823 - val_loss: 0.3322 Epoch 6/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8966 - loss: 0.2744 - val_accuracy: 0.8836 - val_loss: 0.3270 Epoch 7/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9048 - loss: 0.2525 - val_accuracy: 0.8704 - val_loss: 0.3599 Epoch 8/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9060 - loss: 0.2467 - val_accuracy: 0.8904 - val_loss: 0.3158 Epoch 9/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9122 - loss: 0.2340 - val_accuracy: 0.8894 - val_loss: 0.3205 Epoch 10/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9169 - loss: 0.2218 - val_accuracy: 0.8905 - val_loss: 0.3211 Epoch 11/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9194 - loss: 0.2123 - val_accuracy: 0.8921 - val_loss: 0.3132 Epoch 12/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9241 - loss: 0.2038 - val_accuracy: 0.8876 - val_loss: 0.3280 Epoch 13/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9245 - loss: 0.2025 - val_accuracy: 0.8917 - val_loss: 0.3190 Epoch 14/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9316 - loss: 0.1865 - val_accuracy: 0.8958 - val_loss: 0.3175 Epoch 15/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9313 - loss: 0.1841 - val_accuracy: 0.8967 - val_loss: 0.3103 Epoch 16/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9345 - loss: 0.1770 - val_accuracy: 0.8913 - val_loss: 0.3378 Epoch 17/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9378 - loss: 0.1691 - val_accuracy: 0.8930 - val_loss: 0.3432 Epoch 18/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9373 - loss: 0.1640 - val_accuracy: 0.8878 - val_loss: 0.3555 Epoch 19/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9394 - loss: 0.1596 - val_accuracy: 0.8908 - val_loss: 0.3525 Epoch 20/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9427 - loss: 0.1527 - val_accuracy: 0.8913 - val_loss: 0.3433 Epoch 21/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9431 - loss: 0.1507 - val_accuracy: 0.8912 - val_loss: 0.3503 Epoch 22/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9442 - loss: 0.1472 - val_accuracy: 0.8891 - val_loss: 0.3593 Epoch 23/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9468 - loss: 0.1425 - val_accuracy: 0.8949 - val_loss: 0.3600 Epoch 24/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9501 - loss: 0.1343 - val_accuracy: 0.8923 - val_loss: 0.3566 Epoch 25/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9496 - loss: 0.1340 - val_accuracy: 0.8878 - val_loss: 0.3949 Epoch 26/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9539 - loss: 0.1262 - val_accuracy: 0.8907 - val_loss: 0.3792 Epoch 27/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9543 - loss: 0.1209 - val_accuracy: 0.8942 - val_loss: 0.3780 Epoch 28/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9529 - loss: 0.1213 - val_accuracy: 0.8953 - val_loss: 0.3919 Epoch 29/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9576 - loss: 0.1135 - val_accuracy: 0.8949 - val_loss: 0.3889 Epoch 30/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9577 - loss: 0.1130 - val_accuracy: 0.8932 - val_loss: 0.3988 Epoch 31/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9578 - loss: 0.1093 - val_accuracy: 0.8938 - val_loss: 0.4114 Epoch 32/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9579 - loss: 0.1102 - val_accuracy: 0.8950 - val_loss: 0.4106 Epoch 33/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9595 - loss: 0.1077 - val_accuracy: 0.8942 - val_loss: 0.4095 Epoch 34/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9584 - loss: 0.1085 - val_accuracy: 0.8950 - val_loss: 0.4217 Epoch 35/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9617 - loss: 0.1033 - val_accuracy: 0.8909 - val_loss: 0.4860 Epoch 36/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9635 - loss: 0.0971 - val_accuracy: 0.8944 - val_loss: 0.4451 Epoch 37/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9646 - loss: 0.0926 - val_accuracy: 0.8857 - val_loss: 0.5176 Epoch 38/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9659 - loss: 0.0909 - val_accuracy: 0.8892 - val_loss: 0.4487 Epoch 39/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9649 - loss: 0.0969 - val_accuracy: 0.8912 - val_loss: 0.4505 Epoch 40/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9645 - loss: 0.0945 - val_accuracy: 0.8950 - val_loss: 0.4620 Epoch 41/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9669 - loss: 0.0877 - val_accuracy: 0.8902 - val_loss: 0.4589 Epoch 42/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9689 - loss: 0.0817 - val_accuracy: 0.8925 - val_loss: 0.4819 Epoch 43/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9674 - loss: 0.0841 - val_accuracy: 0.8925 - val_loss: 0.4590 Epoch 44/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9708 - loss: 0.0764 - val_accuracy: 0.8966 - val_loss: 0.4496 Epoch 45/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9706 - loss: 0.0812 - val_accuracy: 0.8899 - val_loss: 0.5248 Epoch 46/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9698 - loss: 0.0817 - val_accuracy: 0.8972 - val_loss: 0.4896 Epoch 47/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9716 - loss: 0.0757 - val_accuracy: 0.8912 - val_loss: 0.5208 Epoch 48/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9712 - loss: 0.0777 - val_accuracy: 0.8955 - val_loss: 0.4960 Epoch 49/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9711 - loss: 0.0754 - val_accuracy: 0.8902 - val_loss: 0.5836 Epoch 50/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9738 - loss: 0.0712 - val_accuracy: 0.8942 - val_loss: 0.5111 Best epoch: 46
Re-instantiate the hypermodel and train it with the optimal number of epochs from above.
hypermodel = tuner.hypermodel.build(best_hps)
# Retrain the model
hypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2)
Epoch 1/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.7859 - loss: 0.6252 - val_accuracy: 0.8511 - val_loss: 0.4150 Epoch 2/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8626 - loss: 0.3801 - val_accuracy: 0.8575 - val_loss: 0.3849 Epoch 3/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8738 - loss: 0.3377 - val_accuracy: 0.8665 - val_loss: 0.3635 Epoch 4/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8889 - loss: 0.3028 - val_accuracy: 0.8816 - val_loss: 0.3250 Epoch 5/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8951 - loss: 0.2875 - val_accuracy: 0.8880 - val_loss: 0.3142 Epoch 6/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8996 - loss: 0.2703 - val_accuracy: 0.8840 - val_loss: 0.3205 Epoch 7/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9039 - loss: 0.2545 - val_accuracy: 0.8768 - val_loss: 0.3439 Epoch 8/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9101 - loss: 0.2409 - val_accuracy: 0.8853 - val_loss: 0.3357 Epoch 9/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9166 - loss: 0.2261 - val_accuracy: 0.8899 - val_loss: 0.3255 Epoch 10/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9178 - loss: 0.2207 - val_accuracy: 0.8848 - val_loss: 0.3313 Epoch 11/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9206 - loss: 0.2160 - val_accuracy: 0.8901 - val_loss: 0.3188 Epoch 12/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9215 - loss: 0.2069 - val_accuracy: 0.8901 - val_loss: 0.3178 Epoch 13/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9271 - loss: 0.1945 - val_accuracy: 0.8902 - val_loss: 0.3433 Epoch 14/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9283 - loss: 0.1905 - val_accuracy: 0.8913 - val_loss: 0.3185 Epoch 15/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9310 - loss: 0.1840 - val_accuracy: 0.8965 - val_loss: 0.3224 Epoch 16/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9348 - loss: 0.1747 - val_accuracy: 0.8907 - val_loss: 0.3480 Epoch 17/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9335 - loss: 0.1739 - val_accuracy: 0.8927 - val_loss: 0.3427 Epoch 18/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9381 - loss: 0.1654 - val_accuracy: 0.8959 - val_loss: 0.3328 Epoch 19/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9411 - loss: 0.1554 - val_accuracy: 0.8956 - val_loss: 0.3311 Epoch 20/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9409 - loss: 0.1536 - val_accuracy: 0.8913 - val_loss: 0.3663 Epoch 21/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9448 - loss: 0.1483 - val_accuracy: 0.8931 - val_loss: 0.3675 Epoch 22/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9453 - loss: 0.1419 - val_accuracy: 0.8933 - val_loss: 0.3555 Epoch 23/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9477 - loss: 0.1396 - val_accuracy: 0.8903 - val_loss: 0.3795 Epoch 24/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9480 - loss: 0.1405 - val_accuracy: 0.8949 - val_loss: 0.3739 Epoch 25/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.9490 - loss: 0.1344 - val_accuracy: 0.8935 - val_loss: 0.3846 Epoch 26/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9507 - loss: 0.1301 - val_accuracy: 0.8928 - val_loss: 0.3961 Epoch 27/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9535 - loss: 0.1218 - val_accuracy: 0.8982 - val_loss: 0.3585 Epoch 28/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9540 - loss: 0.1205 - val_accuracy: 0.8923 - val_loss: 0.3853 Epoch 29/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9548 - loss: 0.1213 - val_accuracy: 0.8934 - val_loss: 0.4092 Epoch 30/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9588 - loss: 0.1122 - val_accuracy: 0.8907 - val_loss: 0.4172 Epoch 31/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9572 - loss: 0.1136 - val_accuracy: 0.8975 - val_loss: 0.3871 Epoch 32/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9589 - loss: 0.1119 - val_accuracy: 0.8909 - val_loss: 0.4302 Epoch 33/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9609 - loss: 0.1059 - val_accuracy: 0.8941 - val_loss: 0.4405 Epoch 34/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9609 - loss: 0.1037 - val_accuracy: 0.8947 - val_loss: 0.4228 Epoch 35/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9635 - loss: 0.0993 - val_accuracy: 0.8956 - val_loss: 0.4306 Epoch 36/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9640 - loss: 0.0960 - val_accuracy: 0.8944 - val_loss: 0.4330 Epoch 37/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9618 - loss: 0.0983 - val_accuracy: 0.8962 - val_loss: 0.4432 Epoch 38/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9644 - loss: 0.0937 - val_accuracy: 0.8922 - val_loss: 0.4692 Epoch 39/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9653 - loss: 0.0926 - val_accuracy: 0.8934 - val_loss: 0.4716 Epoch 40/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9648 - loss: 0.0935 - val_accuracy: 0.8926 - val_loss: 0.4649 Epoch 41/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9663 - loss: 0.0886 - val_accuracy: 0.8912 - val_loss: 0.4818 Epoch 42/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9671 - loss: 0.0877 - val_accuracy: 0.8916 - val_loss: 0.5000 Epoch 43/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9682 - loss: 0.0830 - val_accuracy: 0.8877 - val_loss: 0.5475 Epoch 44/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9674 - loss: 0.0860 - val_accuracy: 0.8955 - val_loss: 0.4778 Epoch 45/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9686 - loss: 0.0798 - val_accuracy: 0.8872 - val_loss: 0.5337 Epoch 46/46 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9705 - loss: 0.0763 - val_accuracy: 0.8948 - val_loss: 0.4820 <keras.src.callbacks.history.History at 0x7f53840b6f10>
To finish this tutorial, evaluate the hypermodel on the test data.
eval_result = hypermodel.evaluate(img_test, label_test)
print("[test loss, test accuracy]:", eval_result)
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8913 - loss: 0.5298 [test loss, test accuracy]: [0.5244643092155457, 0.8938999772071838]
The my_dir/intro_to_kt
directory contains detailed logs and checkpoints for every trial (model configuration) run during the hyperparameter search. If you re-run the hyperparameter search, the Keras Tuner uses the existing state from these logs to resume the search. To disable this behavior, pass an additional overwrite=True
argument while instantiating the tuner.
Summary
In this tutorial, you learned how to use the Keras Tuner to tune hyperparameters for a model. To learn more about the Keras Tuner, check out these additional resources:
Also check out the HParams Dashboard in TensorBoard to interactively tune your model hyperparameters.