2016-06-23 1 views
5

나는 그것에 kerasRegressor와 scikit 배우기 pipline 있습니다keras regressor가있는 scikit-learn pipline을 디스크에 저장하는 방법은 무엇입니까?

estimators = [ 
    ('standardize', StandardScaler()), 
    ('mlp', KerasRegressor(build_fn=baseline_model, nb_epoch=5, batch_size=1000, verbose=1)) 
    ] 
pipeline = Pipeline(estimators) 

의 pipline 훈련, 나는 ... JOBLIB를 사용하여 디스크에 저장

joblib.dump(pipeline, filename , compress=9) 

을하려하지만 무엇입니까 후 오류 :

RuntimeError: maximum recursion depth exceeded

어떻게 파이프 라인을 디스크에 저장 하시겠습니까?

+0

당신은 볼 수 있었다 딜에서. 어쩌면 작동합니다 https://pypi.python.org/pypi/dill – Moritz

+0

최대 재귀 수준의 값을 단순히 늘려야합니다. http://stackoverflow.com/questions/3323001/maximum-recursion-depth – user1808924

답변

4

나는 직접적인 방법이 없기 때문에 같은 문제로 고생했다. 여기 나를 위해 일한 해킹이 있습니다. 내 파이프 라인을 두 개의 파일로 저장했습니다. 모델이 다시로드 할 수있는 방법을 여기

... 
from keras.models import load_model 
from sklearn.externals import joblib 

... 

pipeline = Pipeline([ 
    ('scaler', StandardScaler()), 
    ('estimator', KerasRegressor(build_model)) 
]) 

pipeline.fit(X_train, y_train) 

# Save the Keras model first: 
pipeline.named_steps['estimator'].model.save('keras_model.h5') 

# This hack allows us to save the sklearn pipeline: 
pipeline.named_steps['estimator'].model = None 

# Finally, save the pipeline: 
joblib.dump(pipeline, 'sklearn_pipeline.pkl') 

del pipeline 

그리고 : 다음 sklearn 파이프 라인과 두 번째의 절인 객체를 저장 첫 번째 파일은 Keras 모델을 저장하는 데 사용 된

# Load the pipeline first: 
pipeline = joblib.load('sklearn_pipeline.pkl') 

# Then, load the Keras model: 
pipeline.named_steps['estimator'].model = load_model('keras_model.h5') 

y_pred = pipeline.predict(X_test) 
+0

이 방법을 시도했습니다. KerasClassifier와 함께 오류가 발생했습니다 : 'KerasClassifier'개체에 '저장'속성이 없습니다. 실제로 파이프 라인을 수행하지 않으셨습니까? named_steps [ 'estimator']. model.model.save ('keras_model.h5')? 그러나이 경우에는로드 된 모델 주위에 KerasClassifier 객체를 다시 포장해야합니다. – JohnnyQ

+1

네, 정말 확신합니다. 그냥 다시 한 번 확인, 그것은 매력처럼 작동 :) :) (python 3.5.2, keras 2.0.8, sklearn 0.19.1) – constt

관련 문제