"""
09_keras_Tuner使用keras Tuner调整超参数
"""import  tensorflow as tf
from tensorflow import keras
import keras_tuner as kt#加载数据集
(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)))# Tune the number of units in the first Dense layer# Choose an optimal value between 32-512hp_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.0001hp_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
"""
#实例化调节器并执行超调
tuner = kt.Hyperband(model_builder,objective='val_accuracy',max_epochs=10,factor=3,directory='my_dir',project_name='intro_to_kt')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')}.
""")# 生命达到特定值后提前停止训练
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')}.
""")#训练模型
"""
使用从搜索中获得的超参数找到训练模型的最佳生命周期数。
"""
# 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,))#最佳循环数不再进行训练
hypermodel = tuner.hypermodel.build(best_hps)# Retrain the model
hypermodel.fit(img_train, label_train, epochs=best_epoch, validation_split=0.2)#测试数据上评估超模型
eval_result = hypermodel.evaluate(img_test, label_test)
print("[test loss, test accuracy]:", eval_result)

09_keras_Tuner使用keras Tuner调整超参数(超参数优化)相关推荐

  1. 使用keras Tuner调整超参数

    Keras Tuner是一个库,可以为TensorFlow程序选择最佳的超参数集.为机器学习(ML)应用程序选择正确的超参数集的过程称为超参数调整或超调整. 超参数是控制训练过程和ML模型拓扑结构的变 ...

  2. TensorFlow2中使用Keras Tuner搜索网络的超参数

    1. 导入所需的库 import tensorflow as tf import kerastuner as kt import IPythonfor i in [tf, kt]:print(i.__ ...

  3. tensorflow官方Blog-使用Keras Tuner超参数优化框架 进行超参数调整 ,具体实现版本

    文章目录 进入正题,keras tuner超参数优化框架 模型构建def build_model(hp): 实例化tuner 加载数据集,进行超参数搜索tuner.search() 找到最佳的模型tu ...

  4. Tensorflow2.0学习-Keras Tuner 妙用 (六)

    文章目录 Keras Tuner调整超参数 引包 数据准备 模型准备 跑起来 Keras Tuner调整超参数 Keras Tuner 是一个库,可帮助您为 TensorFlow 程序选择最佳的超参数 ...

  5. Keras Tuner自动调参工具使用入门教程

    主体是翻译的Keras Tuner的说明:https://keras-team.github.io/keras- tuner/documentation/tuners/ github地址:https: ...

  6. 机器学习笔记 - 使用Keras Tuner进行自动化超参数调整

    一.什么是超参数调整? 在机器学习工作流程中,您已经根据对数据集的先验分析为模型选择或提取了特征和目标 - 可能使用了PCA等降维技术. 训练机器学习模型时会进行如下迭代: 在训练开始之前,以随机或几 ...

  7. 用keras tuner 来优化tensorflw超参数

    用keras tuner 来优化tensorflw超参数 安装依赖包 !pip install --upgrade pip 导入模块 import tensorflow as tf from tens ...

  8. Keras Tuner模型自动超参数调优

    Keras Tuner安装 pip install keras-tuner from tensorflow.keras import layers from tensorflow import ker ...

  9. sklearn自学指南(part61)--调整估计器的超参数

    学习笔记,仅供参考,有错必纠 文章目录 调整估计器的超参数 详尽的网格搜索 随机参数优化 连续减半法寻找最优参数 选择`min_resources`和候选数[(略)](https://scikit-l ...

最新文章

  1. myeclipse乱码
  2. ErWin简单使用说明
  3. 070901css基础知识
  4. python 不确定度_python机器学习-chapter2_16
  5. 解决/usr/local/lib/libglog.so.0: error adding symbols: DSO missing from command line
  6. lnmp无法删除.user.ini
  7. Spring的AOP面向切面编程
  8. 胡寿松自动控制原理第七版勘误-152页
  9. win pxe安装linux,windows系统架设PXE+TFTP+HTTP安装CentOS 4.7 体验
  10. matlab风玫瑰图,风玫瑰图的Matlab编程绘制
  11. 查找算法 binary_serach
  12. Arduino基础项目五:制作彩色LED灯
  13. 计算机中解决不匹配,电脑显示屏显示不匹配.怎么办
  14. 金蝶KIS标准迷你版专业版 K3 引出报表提示保存文件失败,原因:Automation错误
  15. sub 对应php什么函数,subtotal函数的使用方法1-9分别什么意思
  16. 服务器系统记事本乱码,win7系统记事本出现乱码的五种解决方法
  17. 如何解决wup.exe文件占用cpu资源
  18. CAN总线接口静电保护及ESD二极管选型
  19. 自己做了一个分享网盘资源的网站
  20. 正则表达式中的贪婪匹配与懒惰匹配

热门文章

  1. python3 学习之路1
  2. 关于localhost404打不开
  3. 快慢指针之练习【2】
  4. 如何理解Redis中的事务
  5. @Autowired 和 @Resource 的区别(为什么更推荐使用@Resource ?)
  6. 群晖NAS 7.X版搭建博客网站,并内网穿透发布公网可访问 4-8
  7. ubuntu中pycharm没有启动图标
  8. 大数据(分布式计算框架+分布式存储)
  9. vbs小游戏(猜字谜)
  10. 墙裂推荐,2023年最强、最实用的IDEA插件推荐合集