属性错误:无法获取<模块"__main__"上的属性"create_model>



我创建了一个神经网络模型,并创建了一个集成学习模型,即投票模型。我将神经网络与随机森林和xgboost结合起来。现在我保存了模型并尝试将其加载到另一个Jupiter笔记本上但我得到了这个错误AttributeError:不能在main上获得属性'create_model'"在

这是创建模型的代码,它在第一个笔记本

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy

# Function to create model, required for KerasClassifier
def create_model(input_shape=66):
#x_shape= data_x.shape
#input_dim=x_shape[1]
# create model
model = Sequential()
model.add(Dense(12, input_dim=66, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1,activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model

seed = 7
numpy.random.seed(seed)
Kc_model = KerasClassifier(
create_model, # Pass in function
input_shape=66, # Pass in the dimensions to above function
epochs=100,
batch_size=32,
verbose=False)
Kc_model._estimator_type = "classifier"

Kc_model.fit(x_train, y_train, epochs=100,batch_size=10)

rf = RandomForestClassifier(max_depth=15, random_state=0)
rf.fit(x_train,y_train)
rf_y_pred = rf.predict(x_test)
#Model Score
print("The accuracy score for Random Forest Classifier is")
print("Accuracy:{}%".format(round(metrics.accuracy_score(y_test, rf_y_pred)*100)))
print("Training:{}%".format(round(rf.score(x_train, y_train)*100)))
print("Test set: {}%".format(round(rf.score(x_test, y_test)*100)))

xgboost_model = XGBClassifier()
xgboost_model.fit(x_train, y_train)
xgboost_y_pred = xgboost_model.predict(x_test)
print("The accuracy score for Voting XGB Classifier is")
print("Accuracy:{}%".format(round(metrics.accuracy_score(y_test, xgboost_y_pred)*100)))
print("Training:{}%".format(round(xgboost_model.score(x_train, y_train)*100)))
print("Test set: {}%".format(round(xgboost_model.score(x_test, y_test)*100)))

from keras.wrappers.scikit_learn import KerasClassifier
import scikeras
from tensorflow import keras
voting = VotingClassifier(
estimators = [('rf',rf),('xgboost_model',xgboost_model),('Kc_model',Kc_model) ],
voting='soft')
#reshaping=y_test.reshape(2712,1)
voting_model =voting.fit(x_train, y_train)
voting_pred = voting_model.predict(x_test)
#Model Score
print("The accuracy score for Voting Classifier is")
print("Training:{}%".format(round(voting_model.score(x_train, y_train)*100)))
print("Test set: {}%".format(round(voting_model.score(x_test, y_test)*100)))

import pickle
# save
with open('voting_model.pkl','wb') as f:
pickle.dump(Kc_model,f)

在我尝试加载模型的第二个笔记本中,我得到了一个错误,如下所示


import pickle
import pandas as pd
with open('voting_model.pkl', 'rb') as f:
Kc_model = pickle.load(f)

发生这种情况的原因是无法picklekeras.wrappers.scikit_learn.KerasClassifier包装器。没有保存模型构建功能。相反,您应该pickle拟合的模型:

import pickle
# save
with open('voting_model.pkl','wb') as f:
pickle.dump(Kc_model.model, f)

现在,您可以加载您的模型并按照您的意愿使用它。

with open('voting_model.pkl', 'rb') as f:
model = pickle.load(f)
# Predict something.
model.predict(X_test)

但是,如果在加载后需要KerasClassifier实例,则应该重新包装它。然后保存"classes_"属性。最后,现在构建函数将返回加载的pickle:

# Save this as well.
with open('voting_model_classes.pkl', 'wb') as f:
pickle.dump(Kc_model.classes_, f)
import pickle
from keras.wrappers.scikit_learn import KerasClassifier
def load_model():
with open('voting_model.pkl', 'rb') as f:
return pickle.load(f)

def load_classes():
with open('voting_model_classes.pkl', 'rb') as f:
return pickle.load(f)

Kc_model = KerasClassifier(
load_model,
epochs=100,
batch_size=32,
verbose=False)
Kc_model._estimator_type = "classifier"
# We need to manually call it because it will only be called once the classifier is re-fitted.
Kc_model.model = load_model()
Kc_model.classes_ = load_classes()
# Now you can use Kc_model as KerasClassifier.

错误是预料之中的:模型构建函数按名称进行pickle,而该名称在您的新笔记本中不存在。

你可以试试SciKeras,它有一个initialize方法(docs),你可以调用它来恢复像classes_这样的东西,如果你选择直接使用SavedModel序列化你的Keras模型(SciKeras的KerasClassifier将很乐意接受一个模型实例)。

相关内容

  • 没有找到相关文章

最新更新