# KerasRegressor serialize/save a model as a .h5df

I am been using a script from machinelearningmastery on Keras regression and I would like to save model as a .h5 file.

Machinelearningmastery also has another tutorial for saving models/pickles but the scripts are written in a model.fit() method in Keras… But the script I am using I am defining the model thru calling a function.

Can someone give me a tip on how I can save this model as a .h5df?

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt
import math
from sklearn.preprocessing import MinMaxScaler

print(dataset.shape)
print(dataset.dtypes)
print(dataset.columns)

# shuffle dataset
df = dataset.sample(frac=1.0)

# split into input (X) and output (Y) variables
X = np.array(df.drop(['kWh'],1))
Y = np.array(df['kWh'])

def wider_model():
# create model
model = Sequential()
# Compile model
return model

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=wider_model, epochs=200, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(pipeline, X, Y, cv=kfold)
print("Wider: %.2f (%.2f) MSE" % (results.mean(), results.std()))
print("RMSE", math.sqrt(results.std()))


After your edit, it sounds like the real issue is that you're using the sklearn wrapper for keras and an sklearn pipeline.

To access the actual NN from the pipeline, use the steps or named_steps attribute:
https://scikit-learn.org/stable/modules/compose.html#pipeline

Then, to save the wrapped KerasRegressor model, use model_name.model.save():
https://stackoverflow.com/questions/40396042/how-to-save-scikit-learn-keras-model-into-a-persistence-file-pickle-hd5-json-ya#40397312
https://github.com/keras-team/keras/issues/4274

• Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN? – HenryHub Mar 2 '19 at 20:57
• It’s still deep learning right? I get better results with this method Vs using a keras ‘model.fit()’ regression method with the same architecture as far as same number of model hidden layers and nuerons – HenryHub Mar 2 '19 at 20:59
• KerasRegressor is a wrapper on the keras package, meant to look like and interact cleanly with sklearn (hence your pipeline). There shouldn't be any difference, if you manually do the standardization scaling and k-fold cross-validation. (If there is, maybe another question is warranted.) – Ben Reiniger Mar 3 '19 at 1:22
• @BenReiniger Can it be saved as .pb graph? – Jodh Singh Aug 7 '19 at 8:14
• @JodhSingh, if nothing else, saving first to h5 and then converting to pb as usual should work. You should be able to do something more direct, using KerasRegressor.model to recover the Keras model object, but I don't know how that interacts with the keras.session involved in the common pb conversion script. Maybe post that as a standalone question? – Ben Reiniger Aug 7 '19 at 21:37

Citing Keras' official page:

It is not recommended to use pickle or cPickle to save a Keras model.

You can use model.save(filepath) to save a Keras model into a single HDF5 file which will contain:

• the architecture of the model, allowing to re-create the model
• the weights of the model
• the training configuration (loss, optimizer)
• the state of the optimizer, allowing to resume training exactly where you left off.
• Hello, could you give me a tip in my code how I can incorporate a ‘model.save()’ to save an hd5 file. I am defining the model thru calling a function from the Machinelearningmastery post. Thank you – HenryHub Mar 2 '19 at 15:49
• I edited the title and post to replace the word pickle with serialize/save model as .hd5 file. Searching online for the KerasRegressor, nothing comes up to save the model... only keras using the ‘model.fit()’ method. Thanks for anytime in responding – HenryHub Mar 2 '19 at 19:36