tensorflow - 在另一个环境中加载 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)
解决方案
推荐阅读
- javascript - 使用 AJV 要求某些元素存在于数组中
- python - 暂停递归深度优先搜索
- javascript - 简单频率排序 - Codewars 挑战 - JavaScript
- javascript - javascript减少返回未定义
- git - http.sslVerify=false 不会在 git config 中禁用 ssl 验证
- sql - Oracle Sql groupBy 分别在 2 列上
- python - Tkinter:选中时用于背景颜色的单选按钮
- apache-spark - 在 DBT 管道中使用外部 parquet 表
- javascript - TinyMCE 无法读取视频标签
- prisma - 如何使用从 prisma1 到 prisma2 的分页,使用 Connection