东方耀AI技术分享
标题:
07、神经网络的超参数搜索
[打印本页]
作者:
东方耀
时间:
2019-10-30 16:03
标题:
07、神经网络的超参数搜索
07、神经网络的超参数搜索
# 搜索学习率这个超参:[1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2]
手动搜索:需要用多层for循环 同时每次只能搜索一组超参 无法并行化
# 搜索学习率这个超参:[1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2]
learning_rates = [1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2]
historys = []
test_losses = []
for lr in learning_rates:
model = keras.models.Sequential(layers=[
keras.layers.Dense(30, activation='relu', input_shape=(x_train.shape[1], )),
keras.layers.Dense(1)
], name='model_dfy')
model.compile(optimizer=keras.optimizers.Adam(lr), loss='mean_squared_error')
callbacks = [
keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)
]
history = model.fit(x_train_scaled, y_train, validation_data=(x_valid_scaled, y_valid), epochs=50,
callbacks=callbacks)
# Returns the loss value & metrics values for the model in test mode.
test_losses.append(model.evaluate(x_test_scaled, y_test))
historys.append(history)
复制代码
sklearn封装keras模型Demo:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
# print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)
from sklearn.model_selection import train_test_split
x_train_all, x_test, y_train_all, y_test = train_test_split(
housing.data, housing.target, random_state=7)
x_train, x_valid, y_train, y_valid = train_test_split(
x_train_all, y_train_all, random_state=11)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)
# 使用sklearn中RandomizedSearchCV
# 1、sklearn封装keras模型
# 2、定义超参集合
# 3、搜索超参
def build_model(hidden_layer=2, layer_size=30, learning_rate=3e-3):
model = keras.models.Sequential(name='model_for_sklearn')
model.add(keras.layers.Dense(layer_size, activation='relu', input_shape=(x_train.shape[1], )))
for _ in range(hidden_layer-1):
model.add(keras.layers.Dense(layer_size, activation='relu'))
model.add(keras.layers.Dense(1))
optimizer = keras.optimizers.Adam(learning_rate)
model.compile(optimizer, loss='mse')
model.summary()
return model
# Implementation of the scikit-learn regressor API for Keras
# 注意 build_fn= 函数名
sklearn_model = keras.wrappers.scikit_learn.KerasRegressor(build_fn=build_model)
# sklearn_model.summary()
callbacks = [
keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)
]
history = sklearn_model.fit(x_train_scaled, y_train, validation_data=(x_valid_scaled, y_valid), epochs=50,
callbacks=callbacks)
def plot_learning_curve(history):
# ValueError: arrays must all be same length
# 表格型数据 要求每一列的len一致 这里即:history.history字典里每个key对应的value长度一致
df_history = pd.DataFrame(data=history.history)
print(df_history)
df_history.plot(figsize=(8, 5))
plt.grid(True)
# x就是DataFrame的索引
plt.ylim(0, 1)
plt.show()
plot_learning_curve(history)
from sklearn.metrics import mean_squared_error
# Returns the loss value & metrics values for the model in test mode.
# print(sklearn_model.score(x_test_scaled, y_test))
print(mean_squared_error(y_test, sklearn_model.predict(x_test_scaled)))
复制代码
使用sklearn(封装keras模型)中RandomizedSearchCV示例:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
# print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)
from sklearn.model_selection import train_test_split
x_train_all, x_test, y_train_all, y_test = train_test_split(
housing.data, housing.target, random_state=7)
x_train, x_valid, y_train, y_valid = train_test_split(
x_train_all, y_train_all, random_state=11)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)
# 使用sklearn中RandomizedSearchCV, GridSearchCV
# 1、sklearn封装keras模型
# 2、定义超参集合
# 3、搜索超参
def build_keras_model(hidden_layer=2, layer_size=30, learning_rate=3e-3):
model = keras.models.Sequential(name='model_for_sklearn')
model.add(keras.layers.Dense(layer_size, activation='relu', input_shape=(x_train.shape[1], )))
for _ in range(hidden_layer-1):
model.add(keras.layers.Dense(layer_size, activation='relu'))
model.add(keras.layers.Dense(1))
optimizer = keras.optimizers.Adam(learning_rate)
model.compile(optimizer, loss='mse')
model.summary()
return model
# Implementation of the scikit-learn regressor API for Keras
# 注意 build_fn= 函数名
sklearn_model = keras.wrappers.scikit_learn.KerasRegressor(build_fn=build_keras_model)
# sklearn_model.summary()
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
# 参数网格
param_grid = [
{
'hidden_layer': [2, 3, 4, 5, 6],
'layer_size': np.arange(30, 100),
'learning_rate': [1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2]
}
]
# grid_search = GridSearchCV(estimator=sklearn_model, param_grid=param_grid, n_jobs=1, cv=5, verbose=1)
# grid_search.fit(x_train_scaled, y_train)
# print(grid_search.best_params_)
# print(grid_search.best_score_)
from scipy.stats import reciprocal
# 参数分布
param_distributions = {
'hidden_layer': [2, 3, 4, 5, 6],
'layer_size': np.arange(30, 100),
'learning_rate': reciprocal(1e-4, 1e-2)
}
# estimator 推算子 n_iter=10 随机生成10个候选candidates参数集合
# Fitting 5 folds for each of 10 candidates, totalling 50 fits
random_search = RandomizedSearchCV(estimator=sklearn_model, param_distributions=param_distributions,
n_iter=10, n_jobs=1, cv=5, verbose=1, random_state=666)
# 报错:PicklingError: Could not pickle the task to send it to the workers. 原因:无法并行化 设置n_jobs=1
# random_search.fit(x_train_scaled, y_train)
callbacks = [
keras.callbacks.EarlyStopping(patience=3, min_delta=1e-2)
]
random_search.fit(x_train_scaled, y_train, validation_data=(x_valid_scaled, y_valid), epochs=50,
callbacks=callbacks)
print(random_search.best_params_)
# {'hidden_layer': 6, 'layer_size': 32, 'learning_rate': 0.0048794771174957945}
print(random_search.best_score_)
print(random_search.best_estimator_)
best_sklearn_model = random_search.best_estimator_
def plot_learning_curve(history):
# ValueError: arrays must all be same length
# 表格型数据 要求每一列的len一致 这里即:history.history字典里每个key对应的value长度一致
df_history = pd.DataFrame(data=history.history)
print(df_history)
df_history.plot(figsize=(8, 5))
plt.grid(True)
# x就是DataFrame的索引
plt.ylim(0, 1)
plt.show()
# plot_learning_curve(history)
from sklearn.metrics import mean_squared_error
print(best_sklearn_model.score(x_test_scaled, y_test))
print(mean_squared_error(y_test, best_sklearn_model.predict(x_test_scaled)))
best_keras_model = best_sklearn_model.model
# # Returns the loss value & metrics values for the model in test mode.
print(best_keras_model.evaluate(x_test_scaled, y_test))
复制代码
使用sklearn(封装keras模型)中GridSearchCV示例:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
# print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)
from sklearn.model_selection import train_test_split
x_train_all, x_test, y_train_all, y_test = train_test_split(
housing.data, housing.target, random_state=7)
x_train, x_valid, y_train, y_valid = train_test_split(
x_train_all, y_train_all, random_state=11)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)
# 使用sklearn中RandomizedSearchCV, GridSearchCV
# 1、sklearn封装keras模型
# 2、定义超参集合
# 3、搜索超参
def build_keras_model(hidden_layer=2, layer_size=30, learning_rate=3e-3):
model = keras.models.Sequential(name='model_for_sklearn')
model.add(keras.layers.Dense(layer_size, activation='relu', input_shape=(x_train.shape[1], )))
for _ in range(hidden_layer-1):
model.add(keras.layers.Dense(layer_size, activation='relu'))
model.add(keras.layers.Dense(1))
optimizer = keras.optimizers.Adam(learning_rate)
model.compile(optimizer, loss='mse')
model.summary()
return model
# Implementation of the scikit-learn regressor API for Keras
# 注意 build_fn= 函数名
sklearn_model = keras.wrappers.scikit_learn.KerasRegressor(build_fn=build_keras_model)
# sklearn_model.summary()
from sklearn.model_selection import GridSearchCV
# 参数网格
param_grid = [
{
'hidden_layer': [3, 4, 5, 6],
'layer_size': [30, 35, 40],
'learning_rate': [1e-4, 3e-4, 1e-3, 3e-3, 1e-2]
}
]
# Fitting 3 folds for each of 60 candidates, totalling 180 fits
grid_search = GridSearchCV(estimator=sklearn_model, param_grid=param_grid, n_jobs=1, cv=3, verbose=1)
# 报错:PicklingError: Could not pickle the task to send it to the workers. 原因:无法并行化 设置n_jobs=1
# grid_search.fit(x_train_scaled, y_train)
callbacks = [
keras.callbacks.EarlyStopping(patience=3, min_delta=1e-2)
]
grid_search.fit(x_train_scaled, y_train, validation_data=(x_valid_scaled, y_valid), epochs=50,
callbacks=callbacks)
print(grid_search.best_params_)
# {'hidden_layer': 6, 'layer_size': 40, 'learning_rate': 0.001}
print(grid_search.best_score_)
print(grid_search.best_estimator_)
best_sklearn_model = grid_search.best_estimator_
def plot_learning_curve(history):
# ValueError: arrays must all be same length
# 表格型数据 要求每一列的len一致 这里即:history.history字典里每个key对应的value长度一致
df_history = pd.DataFrame(data=history.history)
print(df_history)
df_history.plot(figsize=(8, 5))
plt.grid(True)
# x就是DataFrame的索引
plt.ylim(0, 1)
plt.show()
history = best_sklearn_model.fit(x_train_scaled, y_train, validation_data=(x_valid_scaled, y_valid), epochs=50,
callbacks=callbacks)
plot_learning_curve(history)
from sklearn.metrics import mean_squared_error
print(best_sklearn_model.score(x_test_scaled, y_test))
print(mean_squared_error(y_test, best_sklearn_model.predict(x_test_scaled)))
best_keras_model = best_sklearn_model.model
# # Returns the loss value & metrics values for the model in test mode.
print(best_keras_model.evaluate(x_test_scaled, y_test))
复制代码
东方老师AI官网:
http://www.ai111.vip
有任何问题可联系东方老师微信:dfy_88888
【微信二维码图片】
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4