Home > Blockchain >  After loading KerasClassifier, on running predict() get error 'KerasClassifier' object has
After loading KerasClassifier, on running predict() get error 'KerasClassifier' object has

Time:10-16

I am trying to save and re-load a KerasClassifier model using the below code:

def baseline_model():
    model = Sequential()
    model.add(Dense(8, input_dim=80, activation='relu'))
    model.add(Dense(4, activation='softmax')) # output layer, with number of classes
    model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(lr=0.01), metrics=['accuracy'])
    return model

model.model.save('model')

#re-load model
model2 = KerasClassifier(build_fn=baseline_model, epochs=100)
model2.model = load_model('model')

preds = model2.predict(testX)

The model seems to save ok but when I re-load it and try to generate predictions, I get the following error: 'KerasClassifier' object has no attribute 'classes_'

I'm following the steps in the documentation so I'm not sure why this isn't working. I did fit the model and use it successfully for prediction prior to saving it.

** Update: I also tried leaving out the line:

model2 = KerasClassifier(build_fn=baseline_model, epochs=100)

and just running

model2.model = load_model('model')

but then I get

name 'model2' is not defined

CodePudding user response:

If you need to just reload the model and generate predictions from it, then you don't need the KerasClassifier as it justs converts your Keras model to a scikit-learn model. So you can remove the lines:

model2 = KerasClassifier(build_fn=baseline_model, epochs=100)
model2.model = load_model('model')

and instead just write this:

model2 = load_model('model')

You can then run your model2.predict(testX) as normal.

  • Related