soulless
soulless

Reputation: 393

KerasClassifier - Sci-Kit learn pipeline [Attribute Error]

I am using Sci-kit learn's pipeline with a KerasClassfier at the end. The classifier would load in the trained model for prediction. But after adding the classifier to the pipeline (3 components in total), I received an error AttributeError: 'KerasClassifier' object has no attribute 'model' after calling pipeline.predict_proba. I think it expects me to fit it but I am importing a trained model. I can't find anything relevant online. Your kind help is greatly appreciated. The following is relevant part of my code:

def buildEngModelByLoading():
  # load json and create model
  json_file = open('saved_model/cnnModel.json', 'r')
  loaded_model_json = json_file.read()
  json_file.close()
  loaded_model = model_from_json(loaded_model_json)
  # load weights into new model
  loaded_model.load_weights("saved_model/cnnModel.h5")
  print("Loaded classifier model")
  return loaded_model

engSklearnCnn = KerasClassifier(build_fn=buildEngModelByLoading, epochs=20, batch_size=batchSize, verbose=1)
#Append classfier to one pipeline
pipeline.steps.insert(2,['classifier',engSklearnCnn])`

Upvotes: 2

Views: 1325

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

That's because you forgot to use compile method first before predict function.

buildEngModelByLoading().compile(optimizer = 'classifier_optimizer', loss = 'loss_function', metrics = 'metrics')

Then just replace classifier_optimizer, loss_function, metrics with your parameters used.

Upvotes: 1

Related Questions