基本的代码,导入一些库文件。
from tensorflow.keras.datasets import fashion_mnist
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Activation
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
定义模型构建函数,这里hp就是超参数调整对象。
from kerastuner.tuners import RandomSearch
# 构建模型,传入hp参数,使用其定义需要优化的参数范围,构成参数空间
def build_model(hp):
model = keras.Sequential()
model.add(layers.Input(shape=(28, 28, 1)))
model.add(layers.Flatten())
model.add(layers.Dense(units=hp.Int('units',
min_value=32,
max_value=512,
step=32),
activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(
optimizer=keras.optimizers.Adam(
hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
# loss='categorical_crossentropy',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
定义优化器
# 选用随机搜索
tuner = RandomSearch(
build_model,
objective='val_accuracy', #优化目标为精度'val_accuracy'(最小化目标)
max_trials=5, #总共试验5次,选五个参数配置
executions_per_trial=3, #每次试验训练模型三次
directory='my_dir',
project_name='helloworld')
开始搜索
tuner.search(x=x_train,
y=y_train,
verbose=2, # just slapping this here bc jupyter notebook. The console out was getting messy.
epochs=1,
batch_size=64,
#callbacks=[tensorboard], # if you have callbacks like tensorboard, they go here.
validation_data=(x_test, y_test))
保存相关参数
tuner.results_summary()
with open(f"tuner_{int(time.time())}.pkl", "wb") as f:
pickle.dump(tuner, f)
重读相关参数
import pickle
tuner = pickle.load(open("tuner_1576628824.pkl","rb"))
tuner.get_best_hyperparameters()[0].values
tuner.get_best_models()[0].summary()
参考博客:
Keras Tuner自动调参工具使用入门教程
Optimizing Neural Network Structures with Keras-Tuner
youtube:Optimizing Neural Network Structures with Keras-Tuner