TensorFlow调整超参数步骤

一、概述

神经网络性能十分依赖超参数。
常见的超参数有:学习率、正则化器、正则化系数、隐藏层数、初始权重值、选择的优化器

二、步骤

  1. 构建模型
  2. 保存模型
saver = tf.train.Saver()
with tf.Session() as sess:
    save_path = saver.save(sess,"model.ckpt")
    print("Model saved in file: %s" % save_path)
  1. 确定要调整的超参数
  2. 为超参数选择可能的值
搜索方法 代码
随机搜索 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]
  1. 选择最佳响应参数
种类 代码
损失函数 max_loss = 2
标准差 max_rmse = 0.04
精度 min_accuracy = 90
  1. 使用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 = rmse
            model_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.target

X_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,随机种子0

X_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
在这里插入图片描述

你可能感兴趣的:(Python,Tensorflow,Keras)