首页 > 解决方案 > 在另一个环境中加载 Keras Wrapper Object sklearn

问题描述

我已经训练了一个 sklearn keras 分类器模型,并希望将其保存并加载以部署到另一个环境。

目前,如果我将以下代码保存并加载到相同的环境中,则它可以工作。但是,如果我尝试加载管道和模型(参见步骤 4.1 和 4.2)而不执行 create_network 函数并定义管道(参见步骤 1.1 到 1.6),则无法加载模型。我收到一条错误消息,提示“无法启动 create_network 对象”

想知道是否有某种解决方法,我可以在另一个环境中加载管道和模型,而不需要步骤 1.1 到 1.6。感谢任何形式的帮助,谢谢。

#import libraries
from tensorflow import keras
from tensorflow.keras.models import Sequential
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.pipeline import Pipeline
from keras.models import load_model
from sklearn.model_selection import RandomizedSearchCV, StratifiedKFold

import joblib


#Step 1 Creating Classifier and Pipeline
#Step 1.1 Define the function to create a keras classifier
def create_network(unit,learn_rate=0.001):
    nn_model_best = keras.Sequential()
    nn_model_best.add(keras.layers.Dense(units=unit, activation='relu'))
    nn_model_best.add(keras.layers.Dense(units=1, activation='sigmoid'))

    opt = keras.optimizers.Adam(learning_rate= learn_rate)
    lossfunc = keras.losses.binary_crossentropy
    
    nn_model_best.compile(loss=lossfunc,optimizer=opt,metrics=['accuracy'])
    
    return nn_model_best

#Step 1.2 Define classifier
clf = KerasClassifier(build_fn=create_network,verbose=1)

#Step 1.3 Define the pipeline - chain the preprocessor step and the classifier
pipe = Pipeline([('preprocessor',preprocessor), ('clf',clf)])

#Step 1.4 Randomised search cv with hyper parameters
skf = StratifiedKFold(n_splits=5, random_state=45, shuffle=True)

#Step 1.5 Input the parameters for search space
param_grid = {
    'clf__unit': [5, 10, 15, 20, 25, 30],
    'clf__learn_rate': [0.001, 0.01, 0.1, 0.2, 0.3],
    'clf__epochs': [5, 10, 15, 20, 25, 30],
    'clf__batch_size': [5, 10, 50, 100]
}

# Step 1.6 Create Randomized Search CV
rscv_nn = RandomizedSearchCV(estimator=pipe, param_distributions=param_grid, cv=skf, random_state=22, verbose=1)

#Step 2 Fit the nn model with X-train and y_train data
nn_model = rscv_nn.fit(X_train, y_train)


#Step 3 Saving the model
#Step 3.1 Save the Keras model first:
rscv_nn.best_estimator_.named_steps['clf'].model.save('nn_model.h5')

#Step 3.2 This hack allows us to save the sklearn pipeline:
rscv_nn.best_estimator_.named_steps['clf'].model = None

#Step 3.3 Finally, save the pipeline:
joblib.dump(rscv_nn.best_estimator_, 'nn_model_pipeline.pkl')


#Step 4 Loading the pipeline and model 
#Step 4.1 Load the pipeline first:
rscv_nn.best_estimator_ = joblib.load('nn_model_pipeline.pkl')

# Step 4.2 Then, load the Keras model:
rscv_nn.best_estimator_.named_steps['clf'].model = load_model('nn_model.h5')

#Step 5 Prediction on test data
y_pred_nn = rscv_nn.best_estimator_.predict(X_test)

标签: tensorflowmachine-learningkerasscikit-learnpipeline

解决方案


推荐阅读