如何保存GridSearchCV对象? [英] How to save GridSearchCV object?

查看:793
本文介绍了如何保存GridSearchCV对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在研究使用网格搜索交叉验证(sklearn GridSearchCV)在带有Tensorflow后端的Keras中进行超参数调整.我的模型一经调整 我试图保存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顺序模型.

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.

推荐答案

尝试一下:

from sklearn.externals import joblib
joblib.dump(gs.best_estimator_, 'filename.pkl')

如果要将对象转储到一个文件中,请使用:

If you want to dump your object into one file - use:

joblib.dump(gs.best_estimator_, 'filename.pkl', compress = 1)


简单示例:

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
from sklearn.externals import joblib

iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svc = svm.SVC()
gs = GridSearchCV(svc, parameters)
gs.fit(iris.data, iris.target)

joblib.dump(gs.best_estimator_, 'filename.pkl')

#['filename.pkl']

您还可以保存整个对象:

you can also save the whole object:

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

这篇关于如何保存GridSearchCV对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆