TensorFlow调整超参数步骤
一、概述
神经网络性能十分依赖超参数。
常见的超参数有:学习率、正则化器、正则化系数、隐藏层数、初始权重值、选择的优化器
二、步骤
- 构建模型
- 保存模型
saver = tf.train.Saver()
with tf.Session() as sess:save_path = saver.save(sess,"model.ckpt")print("Model saved in file: %s" % save_path)
- 确定要调整的超参数
- 为超参数选择可能的值
搜索方法 | 代码 |
---|---|
随机搜索 | rate = np.random.rand(5) |
网格搜索 | rate = [i for i in np.arange(0, 1, 0.2)] |
手动搜索 | rate = [0.7, 0.6, 0.5, 0.32, 0.01] |
- 选择最佳响应参数
种类 | 代码 |
---|---|
损失函数 | max_loss = 2 |
标准差 | max_rmse = 0.04 |
精度 | min_accuracy = 90 |
- 使用for循环保存性能更好的模型
# 超参数调优
epochs = [50, 60, 70]
batches = [5, 10, 20]
rmse_min = 0.04
for epoch in epochs:for batch in batches:model = get_model()model.compile(loss='mean_squared_error', optimizer='adam')model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epoch, batch_size=batch, verbose=1)y_test_pred = model.predict(X_test)rmse = mean_squared_error(y_test, y_test_pred)if rmse < rmse_min:rmse_min = rmsemodel_json = model.to_json()with open("model.json", "w") as json_file:json_file.write(model_json)model.save_weights("model.hdf5")print("Saved model to disk")
三、代码
# coding=utf-8
import numpy as np # 用于转换数据
import pandas as pd # 用于分析数据集
import seaborn as sns # 可视化
import matplotlib.pyplot as plt # 可视化
from keras.layers import Dense # 用于添加全连接层
from keras.models import Sequential # 用于定义模型
from sklearn import datasets # 用于获取数据集
from sklearn.metrics import mean_squared_error # 用于评估模型
from sklearn.preprocessing import MinMaxScaler # 用于数据归一化
from sklearn.model_selection import train_test_split # 用于划分训练集、测试集# 数据集
boston = datasets.load_boston() # 读取波士顿房价,返回Bunch对象
df = pd.DataFrame(boston.data, columns=boston.feature_names) # 创建Pandas的数据结构DataFrame
df['target'] = boston.targetX_train, X_test, y_train, y_test = train_test_split(df[['RM', 'LSTAT', 'PTRATIO']], df[['target']], test_size=0.3,random_state=0) # 创建训练集和测试集,测试集占0.3,随机种子0X_train = np.array(MinMaxScaler().fit_transform(X_train)) # 归一化,缩放到0-1。后面要用到Pandas.DataFrame,先进行转换
y_train = np.array(MinMaxScaler().fit_transform(y_train))
X_test = np.array(MinMaxScaler().fit_transform(X_test))
y_test = np.array(MinMaxScaler().fit_transform(y_test))m = len(X_train) # 训练集数
n = 3 # 特征数
n_hidden = 20 # 隐藏层数batch_size = 10 # 每批训练批量大小
eta = 0.01 # 学习率
max_epoch = 5 # 最大迭代数# 定义模型
def get_model():model = Sequential()model.add(Dense(n_hidden, input_dim=n, activation='relu'))model.add(Dense(1, activation='sigmoid'))return model# 超参数调优
epochs = [50, 60, 70]
batches = [5, 10, 20]
max_rmse = 0.04 # 标准差最大值
for epoch in epochs:for batch in batches:model = get_model()model.compile(loss='mean_squared_error', optimizer='adam')model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=epoch, batch_size=batch, verbose=1)y_test_pred = model.predict(X_test)rmse = mean_squared_error(y_test, y_test_pred)if rmse < max_rmse: # 保存更优的模型max_rmse = rmse # 更新标准差最大值model_json = model.to_json()with open("model.json", "w") as json_file:json_file.write(model_json) # 保存为JSON格式model.save_weights("model.hdf5") # 保存权重print("Saved model to disk, RMSE=",max_rmse)
print("RMSE =",max_rmse)
四、结果
RMSE = 0.0131
TensorFlow调整超参数步骤相关推荐
- Ray.tune可视化调整超参数Tensorflow 2.0
Ray.tune官方文档 调整超参数通常是机器学习工作流程中最昂贵的部分. Tune专为解决此问题而设计,展示了针对此痛点的有效且可扩展的解决方案. 请注意,此示例取决于Tensorflow 2.0. ...
- TensorFlow实现超参数调整
TensorFlow实现超参数调整 正如你目前所看到的,神经网络的性能非常依赖超参数.因此,了解这些参数如何影响网络变得至关重要. 常见的超参数是学习率.正则化器.正则化系数.隐藏层的维数.初始权重值 ...
- 09_keras_Tuner使用keras Tuner调整超参数(超参数优化)
""" 09_keras_Tuner使用keras Tuner调整超参数 """import tensorflow as tf from t ...
- 谷歌大脑新优化器VeLO火了!让AI自己调整超参数,自适应不同任务,83个任务训练加速比经典Adam更快!...
点击下方卡片,关注"CVer"公众号 AI/CV重磅干货,第一时间送达 点击进入-> CV 微信技术交流群 萧箫 发自 凹非寺 转载自:量子位(QbitAI) 还在苦恼怎么给 ...
- 让AI自己调整超参数,谷歌大脑新优化器火了,自适应不同任务,83个任务训练加速比经典Adam更快...
萧箫 发自 凹非寺 量子位 | 公众号 QbitAI 还在苦恼怎么给优化器调整更好的参数吗? 现在,谷歌大脑搞出了一个新的优化器VeLO,无需手动调整任何超参数,直接用就完事了. 与其他人工设计的如A ...
- python贝叶斯优化算法_【干货】手把手教你Python实现自动贝叶斯调整超参数
[导读]机器学习中,调参是一项繁琐但至关重要的任务,因为它很大程度上影响了算法的性能.手动调参十分耗时,网格和随机搜索不需要人力,但需要很长的运行时间.因此,诞生了许多自动调整超参数的方法.贝叶斯优化 ...
- 使用keras Tuner调整超参数
Keras Tuner是一个库,可以为TensorFlow程序选择最佳的超参数集.为机器学习(ML)应用程序选择正确的超参数集的过程称为超参数调整或超调整. 超参数是控制训练过程和ML模型拓扑结构的变 ...
- tensorflow超参数优化,Tensorflow的超参数调整
I am searching for a hyperparameter tune package for code written directly in Tensorflow (not Keras ...
- 交叉验证和超参数调整:如何优化您的机器学习模型
In the first two parts of this article I obtained and preprocessed Fitbit sleep data, split the data ...
- tensorflow超参数优化_机器学习模型的超参数优化
引言 模型优化是机器学习算法实现中最困难的挑战之一.机器学习和深度学习理论的所有分支都致力于模型的优化. 机器学习中的超参数优化旨在寻找使得机器学习算法在验证数据集上表现性能最佳的超参数.超参数与一般 ...
最新文章
- 算法题解:对于输入数字串,给出另一种数字排列,使得字典序增加尽可能小...
- 挖洞技巧:支付漏洞之总结
- 随想录(我们从uboot学什么)
- 三维旋转矩阵_旋转之一 - 复数与2D旋转
- 苹果无人驾驶拿 124 个工程师祭天!
- git SSH 公钥拉取代码(使用及配置)
- IC卡读写模块(MFRC522) 简介调试QT实现
- 注塑车间维修师傅如何快速解决9种注塑机故障?
- 基于FPGA的ov5640摄像头简易图像采集装置详解
- 奇门遁甲排盘方:定局
- Excel条形图也可以变身高大上
- html文档注释多行,css注释的写法(单行和多行)
- 苹果手机登录王者荣耀服务器未响应,王者荣耀登陆游戏服务器未响应怎么回事 王者荣耀无法登陆原因介绍...
- 金莹江苏省计算机学会教授,南京大学金莹教授来我院作大学生计算思维与计算机基础能力培养的讲座...
- 3 FFmpeg从入门到精通-FFmpeg转封装
- 机器学习及数据挖掘:幸福感预测
- 09-zookeeper
- php magic模版插件,magic-带64种动画效果的CSS3动画库
- android手机当U盘使用
- 电脑很卡怎么办?重装系统来解决