首先安装scikeras:
pip install scikeras
使用KerasClassifier类对模型进行封装
# coding = utf-8
from scikeras.wrappers import KerasClassifier
from sklearn.model_selection import GridSearchCV
from tensorflow import keras
# Prepare data X_train: ndarray,(60000, 28, 28) y_train: ndarray, (60000,)
(X_train, y_train), (X_valid, y_valid) = keras.datasets.fashion_mnist.load_data()
X_train = X_train / 255.0
X_valid = X_valid / 255.0
# Build model
def create_model(hidden_num=4):
inputs = keras.Input(shape=(28, 28))
x = keras.layers.Flatten()(inputs)
x = keras.layers.Dense(hidden_num, activation='relu')(x)
outputs = keras.layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
# Grid search
param_grid = {
'epochs': [10, 20],
'batch_size': [8, 16, 32],
'hidden_num': [4, 8]
}
model = KerasClassifier(model=create_model, hidden_num=4)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=5, verbose=3)
grid_result = grid.fit(X_train, y_train)
# Print grid search result
print(f'Best: {grid_result.best_score_} using {grid_result.best_params_}')
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, std, param in zip(means, stds, params):
print(f"{mean} {std} with: {param}")
1、对于model.fit()函数原有的参数,比如epchos、batch_size,可以直接放进字典里
2、自定义的参数,比如上面的hidden_num,在创建KerasClassifier时需要当做参数传入。