在 TensorFlow.org 上查看 | 在 Google Colab 中运行 | 在 GitHub 上查看源代码 | 下载笔记本 |
概述
Keras Tuner 是一个库,它可以帮助您为 TensorFlow 程序选择最佳的超参数集。为机器学习 (ML) 应用程序选择合适的超参数集的过程称为超参数调整或超参数优化。
超参数是控制训练过程和 ML 模型拓扑结构的变量。这些变量在训练过程中保持不变,并直接影响 ML 程序的性能。超参数分为两种类型
- 模型超参数,影响模型选择,例如隐藏层的数量和宽度
- 算法超参数,影响学习算法的速度和质量,例如随机梯度下降 (SGD) 的学习率和 k 近邻 (KNN) 分类器的最近邻数量
在本教程中,您将使用 Keras Tuner 为图像分类应用程序执行超参数优化。
设置
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
安装并导入 Keras Tuner。
pip install -q -U keras-tuner
import keras_tuner as kt
下载并准备数据集
在本教程中,您将使用 Keras Tuner 为机器学习模型找到最佳超参数,该模型可以从Fashion MNIST 数据集中对服装图像进行分类。
加载数据。
(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
定义模型
在构建用于超参数优化的模型时,除了模型架构之外,您还需要定义超参数搜索空间。您为超参数优化设置的模型称为超模型。
您可以通过两种方法定义超模型
- 使用模型构建器函数
- 通过子类化 Keras Tuner API 的
HyperModel
类
您还可以使用两个预定义的HyperModel 类 - HyperXception 和 HyperResNet 用于计算机视觉应用程序。
在本教程中,您使用模型构建器函数来定义图像分类模型。模型构建器函数返回一个已编译的模型,并使用您在内联中定义的超参数来对模型进行超参数优化。
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
实例化调优器并执行超参数优化
实例化调优器以执行超参数优化。Keras Tuner 提供了四个调优器 - RandomSearch
、Hyperband
、BayesianOptimization
和 Sklearn
。在本教程中,您将使用Hyperband 调优器。
要实例化 Hyperband 调优器,您必须指定超模型、要优化的目标
以及训练的最大时期数 (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)
Hyperband 调整算法使用自适应资源分配和提前停止来快速收敛到高性能模型。这是通过类似体育锦标赛的支架来实现的。该算法对大量模型进行少量时期的训练,并将仅前一半性能最佳的模型带到下一轮。Hyperband 通过计算 1 + logfactor
(max_epochs
) 并将其四舍五入到最接近的整数来确定支架中要训练的模型数量。
创建一个回调,在验证损失达到某个值后提前停止训练。
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
运行超参数搜索。搜索方法的参数与tf.keras.model.fit
中使用的参数相同,此外还有上面的回调。
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.
训练模型
找到使用从搜索中获得的超参数训练模型的最佳时期数。
# 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
重新实例化超模型,并使用上面获得的最佳时期数进行训练。
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>
要完成本教程,请在测试数据上评估超模型。
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]
my_dir/intro_to_kt
目录包含超参数搜索期间运行的每个试验(模型配置)的详细日志和检查点。如果您重新运行超参数搜索,Keras Tuner 将使用这些日志中的现有状态来恢复搜索。要禁用此行为,在实例化调优器时传递额外的overwrite=True
参数。
总结
在本教程中,您学习了如何使用 Keras Tuner 为模型调整超参数。要了解有关 Keras Tuner 的更多信息,请查看以下其他资源
还可以查看 TensorBoard 中的HParams 仪表板,以交互方式调整模型超参数。