如何保存 GridSearchCV 对象?

2022-01-24 00:00:00 python scikit-learn grid-search keras save

问题描述

最近,我一直致力于在带有 Tensorflow 后端的 Keras 中应用网格搜索交叉验证 (sklearn GridSearchCV) 进行超参数调整.我的模型调整好后我正在尝试保存 GridSearchCV 对象以供以后使用,但没有成功.

Lately, I have been working on applying grid search cross validation (sklearn GridSearchCV) for hyper-parameter tuning in Keras with Tensorflow backend. An soon as my model is tuned I am trying to save the GridSearchCV object for later use without success.

超参数调优如下:

x_train, x_val, y_train, y_val = train_test_split(NN_input, NN_target, train_size = 0.85, random_state = 4)

history = History() 
kfold = 10


regressor = KerasRegressor(build_fn = create_keras_model, epochs = 100, batch_size=1000, verbose=1)

neurons = np.arange(10,101,10) 
hidden_layers = [1,2]
optimizer = ['adam','sgd']
activation = ['relu'] 
dropout = [0.1] 

parameters = dict(neurons = neurons,
                  hidden_layers = hidden_layers,
                  optimizer = optimizer,
                  activation = activation,
                  dropout = dropout)

gs = GridSearchCV(estimator = regressor,
                  param_grid = parameters,
                  scoring='mean_squared_error',
                  n_jobs = 1,
                  cv = kfold,
                  verbose = 3,
                  return_train_score=True))

grid_result = gs.fit(NN_input,
                    NN_target,
                    callbacks=[history],
                    verbose=1,
                    validation_data=(x_val, y_val))

备注:create_keras_model 函数初始化并编译一个 Keras Sequential 模型.

Remark: create_keras_model function initializes and compiles a Keras Sequential model.

执行交叉验证后,我尝试使用以下代码保存网格搜索对象 (gs):

After the cross validation is performed I am trying to save the grid search object (gs) with the following code:

from sklearn.externals import joblib

joblib.dump(gs, 'GS_obj.pkl')

我得到的错误如下:

TypeError: can't pickle _thread.RLock objects

能否请您告诉我此错误的原因可能是什么?

Could you please let me know what might be the reason for this error?

谢谢!

P.S.:joblib.dump 方法适用于保存使用的 GridSearchCV 对象用于训练来自 sklearn 的 MLPRegressors.

P.S.: joblib.dump method works well for saving GridSearchCV objects that are used for the training MLPRegressors from sklearn.


解决方案

使用

直接导入joblib

而不是

从 sklearn.externals 导入作业库

保存对象或结果:

joblib.dump(gs, 'model_file_name.pkl')

并使用以下方法加载您的结果:

and load your results using:

joblib.load("model_file_name.pkl")

这是一个简单的工作示例:

Here is a simple working example:


import joblib

#save your model or results
joblib.dump(gs, 'model_file_name.pkl')

#load your model for further usage
joblib.load("model_file_name.pkl")

相关文章