I'm using a Scikit-Learn custom pipeline (sklearn.pipeline.Pipeline
) in conjunction with RandomizedSearchCV
for hyper-parameter optimization. This works great.
Now I would like to insert a Keras model as a first step into the pipeline. Parameters of the model should be optimized. The computed (fitted) Keras model should then be used later on in the pipeline by other steps, so I think I have to store the model as a global variable so that the other pipeline steps can use it. Is this right?
I know that Keras offers some wrappers for the Scikit-Learn API but the problem is that these wrappers already do classification / regression but I only want to compute the Keras model and nothing else.
How can this be done?
For example I have a method which returns the model:
def create_model(file_path, argument2,...):
...
return model
The method needs some fixed parameters like a file path etc. but X and y is not needed (or can be ignored). The parameters of the model should be optimized (number of layers etc.).
Best Answer
You need to wrap your Keras model as a Scikit learn model first, and then just proceed as normal.
Here's a quick example (I've omitted the imports for brevity)