在 TensorFlow.org 上查看
|
在 Google Colab 中运行
|
在 GitHub 上查看源码
|
下载笔记本
|
概览
Keras Tuner 是一个库,可帮助您为 TensorFlow 程序选择最佳的超参数组合。为机器学习 (ML) 应用选择正确超参数组合的过程称为超参数调优 (hyperparameter tuning) 或 hypertuning。
超参数是控制 ML 模型训练过程和拓扑结构的变量。这些变量在整个训练过程中保持不变,并直接影响 ML 程序的性能。超参数分为两类:
- 模型超参数:影响模型选择的参数,例如隐藏层的数量和宽度
- 算法超参数:影响学习算法速度和质量的参数,例如随机梯度下降 (SGD) 的学习率以及 k-近邻 (KNN) 分类器的邻居数量
在本教程中,您将使用 Keras Tuner 为图像分类应用执行超参数调优。
设置
import tensorflow as tf
from tensorflow import keras
2024-08-16 01:25:04.811063: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-08-16 01:25:04.832191: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-08-16 01:25:04.838460: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] 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-tunerimport 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
定义模型
当您构建用于调优的模型时,除了模型架构外,您还需要定义超参数搜索空间。为调优而设置的模型称为超模型 (hypermodel)。
您可以通过以下两种方式定义超模型:
- 使用模型构建函数
- 通过继承 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 调优器,您必须指定超模型、要优化的 objective(目标)以及训练的最大轮数(max_epochs)。
tuner = kt.Hyperband(model_builder,
objective='val_accuracy',
max_epochs=10,
factor=3,
directory='my_dir',
project_name='intro_to_kt')
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR I0000 00:00:1723771509.637777 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.641612 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.644868 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.648549 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.660168 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.663655 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.666633 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.670142 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.673591 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.677149 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.680118 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771509.683613 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.907409 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.909510 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.911592 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.913615 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.915637 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.917579 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.919546 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.921484 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.923384 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.925354 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.927319 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.929258 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.967243 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.969279 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.971299 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.973289 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.975342 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.977295 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.979282 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.981238 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.983175 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.985662 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.988059 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 I0000 00:00:1723771510.990406 14090 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355 /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.8913333415985107 Best val_accuracy So Far: 0.8913333415985107 Total elapsed time: 00h 05m 37s 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.7774 - loss: 0.6344 - val_accuracy: 0.8590 - val_loss: 0.4024 Epoch 2/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8643 - loss: 0.3766 - val_accuracy: 0.8632 - val_loss: 0.3783 Epoch 3/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8790 - loss: 0.3291 - val_accuracy: 0.8803 - val_loss: 0.3296 Epoch 4/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8884 - loss: 0.3036 - val_accuracy: 0.8708 - val_loss: 0.3529 Epoch 5/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8955 - loss: 0.2840 - val_accuracy: 0.8817 - val_loss: 0.3297 Epoch 6/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8998 - loss: 0.2655 - val_accuracy: 0.8581 - val_loss: 0.4232 Epoch 7/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9040 - loss: 0.2564 - val_accuracy: 0.8808 - val_loss: 0.3396 Epoch 8/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9101 - loss: 0.2421 - val_accuracy: 0.8718 - val_loss: 0.3550 Epoch 9/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9089 - loss: 0.2353 - val_accuracy: 0.8920 - val_loss: 0.3055 Epoch 10/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9160 - loss: 0.2239 - val_accuracy: 0.8918 - val_loss: 0.3077 Epoch 11/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9211 - loss: 0.2111 - val_accuracy: 0.8913 - val_loss: 0.3258 Epoch 12/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9223 - loss: 0.2076 - val_accuracy: 0.8936 - val_loss: 0.3115 Epoch 13/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9260 - loss: 0.1968 - val_accuracy: 0.8892 - val_loss: 0.3134 Epoch 14/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9273 - loss: 0.1914 - val_accuracy: 0.8890 - val_loss: 0.3284 Epoch 15/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9321 - loss: 0.1836 - val_accuracy: 0.8911 - val_loss: 0.3366 Epoch 16/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9363 - loss: 0.1710 - val_accuracy: 0.8952 - val_loss: 0.3252 Epoch 17/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9374 - loss: 0.1710 - val_accuracy: 0.8898 - val_loss: 0.3381 Epoch 18/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9383 - loss: 0.1629 - val_accuracy: 0.8913 - val_loss: 0.3500 Epoch 19/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9414 - loss: 0.1552 - val_accuracy: 0.8954 - val_loss: 0.3418 Epoch 20/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9435 - loss: 0.1495 - val_accuracy: 0.8926 - val_loss: 0.3455 Epoch 21/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9431 - loss: 0.1509 - val_accuracy: 0.8903 - val_loss: 0.3748 Epoch 22/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9454 - loss: 0.1431 - val_accuracy: 0.8960 - val_loss: 0.3444 Epoch 23/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9490 - loss: 0.1361 - val_accuracy: 0.8948 - val_loss: 0.3433 Epoch 24/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9492 - loss: 0.1342 - val_accuracy: 0.8918 - val_loss: 0.3569 Epoch 25/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9488 - loss: 0.1342 - val_accuracy: 0.8910 - val_loss: 0.3757 Epoch 26/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9528 - loss: 0.1263 - val_accuracy: 0.8914 - val_loss: 0.3831 Epoch 27/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9518 - loss: 0.1261 - val_accuracy: 0.8935 - val_loss: 0.3801 Epoch 28/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9545 - loss: 0.1193 - val_accuracy: 0.8914 - val_loss: 0.4115 Epoch 29/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9551 - loss: 0.1182 - val_accuracy: 0.8816 - val_loss: 0.4434 Epoch 30/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9577 - loss: 0.1131 - val_accuracy: 0.8971 - val_loss: 0.3876 Epoch 31/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9558 - loss: 0.1169 - val_accuracy: 0.8903 - val_loss: 0.4025 Epoch 32/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9574 - loss: 0.1101 - val_accuracy: 0.8984 - val_loss: 0.4147 Epoch 33/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9583 - loss: 0.1114 - val_accuracy: 0.8970 - val_loss: 0.4005 Epoch 34/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9623 - loss: 0.1007 - val_accuracy: 0.8935 - val_loss: 0.4260 Epoch 35/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9614 - loss: 0.1021 - val_accuracy: 0.8926 - val_loss: 0.4296 Epoch 36/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9642 - loss: 0.0961 - val_accuracy: 0.8928 - val_loss: 0.4305 Epoch 37/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9633 - loss: 0.0964 - val_accuracy: 0.8891 - val_loss: 0.4603 Epoch 38/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9634 - loss: 0.0962 - val_accuracy: 0.8977 - val_loss: 0.4350 Epoch 39/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9645 - loss: 0.0953 - val_accuracy: 0.8938 - val_loss: 0.4520 Epoch 40/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9661 - loss: 0.0875 - val_accuracy: 0.8923 - val_loss: 0.4823 Epoch 41/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9652 - loss: 0.0903 - val_accuracy: 0.8904 - val_loss: 0.4852 Epoch 42/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9686 - loss: 0.0844 - val_accuracy: 0.8822 - val_loss: 0.5031 Epoch 43/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9672 - loss: 0.0860 - val_accuracy: 0.8942 - val_loss: 0.4723 Epoch 44/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9691 - loss: 0.0818 - val_accuracy: 0.8944 - val_loss: 0.4678 Epoch 45/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9726 - loss: 0.0719 - val_accuracy: 0.8940 - val_loss: 0.4623 Epoch 46/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9697 - loss: 0.0792 - val_accuracy: 0.8934 - val_loss: 0.4757 Epoch 47/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9720 - loss: 0.0728 - val_accuracy: 0.8953 - val_loss: 0.5138 Epoch 48/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9717 - loss: 0.0752 - val_accuracy: 0.8931 - val_loss: 0.5226 Epoch 49/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9728 - loss: 0.0746 - val_accuracy: 0.8975 - val_loss: 0.5169 Epoch 50/50 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9723 - loss: 0.0755 - val_accuracy: 0.8923 - val_loss: 0.5257 Best epoch: 32
重新实例化超模型,并使用上述的最佳轮数对其进行训练。
hypermodel = tuner.hypermodel.build(best_hps)
# Retrain the model
hypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2)
Epoch 1/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 3s 2ms/step - accuracy: 0.7789 - loss: 0.6225 - val_accuracy: 0.8573 - val_loss: 0.4007 Epoch 2/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.8678 - loss: 0.3664 - val_accuracy: 0.8658 - val_loss: 0.3632 Epoch 3/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8781 - loss: 0.3343 - val_accuracy: 0.8702 - val_loss: 0.3546 Epoch 4/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8879 - loss: 0.3070 - val_accuracy: 0.8768 - val_loss: 0.3472 Epoch 5/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.8908 - loss: 0.2902 - val_accuracy: 0.8777 - val_loss: 0.3441 Epoch 6/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9002 - loss: 0.2703 - val_accuracy: 0.8832 - val_loss: 0.3262 Epoch 7/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9053 - loss: 0.2552 - val_accuracy: 0.8917 - val_loss: 0.3017 Epoch 8/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9085 - loss: 0.2475 - val_accuracy: 0.8852 - val_loss: 0.3255 Epoch 9/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9112 - loss: 0.2351 - val_accuracy: 0.8930 - val_loss: 0.3077 Epoch 10/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9157 - loss: 0.2237 - val_accuracy: 0.8913 - val_loss: 0.3110 Epoch 11/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9194 - loss: 0.2138 - val_accuracy: 0.8927 - val_loss: 0.3143 Epoch 12/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9213 - loss: 0.2086 - val_accuracy: 0.8829 - val_loss: 0.3420 Epoch 13/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9240 - loss: 0.2002 - val_accuracy: 0.8898 - val_loss: 0.3196 Epoch 14/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9279 - loss: 0.1937 - val_accuracy: 0.8892 - val_loss: 0.3296 Epoch 15/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9314 - loss: 0.1839 - val_accuracy: 0.8842 - val_loss: 0.3548 Epoch 16/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9333 - loss: 0.1788 - val_accuracy: 0.8895 - val_loss: 0.3340 Epoch 17/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9348 - loss: 0.1738 - val_accuracy: 0.8977 - val_loss: 0.3317 Epoch 18/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9369 - loss: 0.1676 - val_accuracy: 0.8918 - val_loss: 0.3366 Epoch 19/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9379 - loss: 0.1621 - val_accuracy: 0.8974 - val_loss: 0.3227 Epoch 20/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9410 - loss: 0.1548 - val_accuracy: 0.8919 - val_loss: 0.3713 Epoch 21/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9417 - loss: 0.1532 - val_accuracy: 0.8823 - val_loss: 0.4058 Epoch 22/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9467 - loss: 0.1451 - val_accuracy: 0.8979 - val_loss: 0.3486 Epoch 23/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9464 - loss: 0.1425 - val_accuracy: 0.8975 - val_loss: 0.3381 Epoch 24/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9485 - loss: 0.1373 - val_accuracy: 0.8963 - val_loss: 0.3478 Epoch 25/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9500 - loss: 0.1341 - val_accuracy: 0.8926 - val_loss: 0.3846 Epoch 26/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9525 - loss: 0.1279 - val_accuracy: 0.8879 - val_loss: 0.3929 Epoch 27/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9518 - loss: 0.1268 - val_accuracy: 0.8972 - val_loss: 0.3604 Epoch 28/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - accuracy: 0.9546 - loss: 0.1209 - val_accuracy: 0.8967 - val_loss: 0.3876 Epoch 29/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9537 - loss: 0.1233 - val_accuracy: 0.8942 - val_loss: 0.3985 Epoch 30/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9564 - loss: 0.1163 - val_accuracy: 0.8955 - val_loss: 0.4011 Epoch 31/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9559 - loss: 0.1171 - val_accuracy: 0.8975 - val_loss: 0.3997 Epoch 32/32 1500/1500 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9596 - loss: 0.1086 - val_accuracy: 0.8938 - val_loss: 0.4147 <keras.src.callbacks.history.History at 0x7fa8b80d4970>
最后,在测试数据上评估该超模型,完成本教程。
eval_result = hypermodel.evaluate(img_test, label_test)
print("[test loss, test accuracy]:", eval_result)
313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - accuracy: 0.8873 - loss: 0.4595 [test loss, test accuracy]: [0.4649185538291931, 0.8881000280380249]
my_dir/intro_to_kt 目录包含了超参数搜索过程中每次试验(模型配置)的详细日志和检查点。如果您重新运行超参数搜索,Keras Tuner 会使用这些日志中的现有状态来恢复搜索。要禁用此行为,请在实例化调优器时传入额外的 overwrite=True 参数。
总结
在本教程中,您学习了如何使用 Keras Tuner 为模型调优超参数。要了解关于 Keras Tuner 的更多信息,请查看以下其他资源:
还可以查看 TensorBoard 中的 HParams 控制面板,以交互方式调优您的模型超参数。
在 TensorFlow.org 上查看
在 Google Colab 中运行
在 GitHub 上查看源码
下载笔记本