什么是Keras Tuner?
- Keras Tuner 是为解决寻找最优超参数问题而设计的简单易用的,可扩展的超参数优化框架
如何安装Keras Tuner?
Python≥3.6, TensorFlow ≥2.0, 安装命令:
pip install keras-tuner --upgrade
如何使用Keras Tuner?范例如下:
import tensorflow as tf
import keras_tuner as kt
from tensorflow import keras
(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
def model_builder(hp):
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
# 调整第一个Dense层的神经元个数
# 个数选择32--512
hp_units = hp.Int('units', min_value=32, max_value=512, step=8)
model.add(keras.layers.Dense(units=hp_units, activation='relu'))
model.add(keras.layers.Dense(10))
# Tune优化器的learning rate
# 优化参数为:0.01, 0.001,或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
tuner = kt.Hyperband(
model_builder,
objective='val_accuracy',
max_epochs=10,
factor=3,
directory='my_dir',
project_name='kt_demo'
)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
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')}.
""")
运行结果:
Keras-tuner找到了最佳参数
网友评论