2016-06-23 4 views
5

私はそれにkerasRegressorとpiplineを学ぶscikitを持っている:どのようにディスク上にケラス回帰を持つ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

あなたが見ることができる:

... 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 

そして、ここでは、モデルが戻ってロードすることができる方法でありますディルでおそらく、https://pypi.python.org/pypi/dill – Moritz

+0

を動作させることができます。最大再帰深度の値を単純に増やす必要があります。http://stackoverflow.com/questions/3323001/maximum-recursion-depth – user1808924

答えて

4

これを行う直接的な方法はないので、私は同じ問題で苦労しました。ここで私のために働くハックです。パイプラインを2つのファイルに保存しました。最初のファイルは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'オブジェクトには 'save'属性がありません。 実際にpipeline.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

関連する問題