自动调参数工具:Keras-Tuner的基本使用

基本的代码,导入一些库文件。

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

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值