一、概述
神经网络性能十分依赖超参数。
常见的超参数有:学习率、正则化器、正则化系数、隐藏层数、初始权重值、选择的优化器
二、步骤
- 构建模型
- 保存模型
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 = 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")
三、代码
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()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
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)
X_train = np.array(MinMaxScaler().fit_transform(X_train))
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)
model.save_weights("model.hdf5")
print("Saved model to disk, RMSE=",max_rmse)
print("RMSE =",max_rmse)
四、结果
RMSE = 0.0131