2
$\begingroup$

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


# load dataset
dataset = pd.read_csv("joinedRuntime2.csv", index_col='Date', parse_dates=True)

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()
    model.add(Dense(20, input_dim=7, kernel_initializer='normal', activation='relu'))
    #model.add(Dense(28, kernel_initializer='normal', activation='relu'))
    #model.add(Dense(21, kernel_initializer='normal', activation='relu'))
    #model.add(Dense(14, kernel_initializer='normal', activation='relu'))
    model.add(Dense(10, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    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()))
$\endgroup$
0
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN? $\endgroup$
    – HenryHub
    Mar 2 '19 at 20:57
  • $\begingroup$ 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 $\endgroup$
    – HenryHub
    Mar 2 '19 at 20:59
  • $\begingroup$ 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.) $\endgroup$ Mar 3 '19 at 1:22
  • $\begingroup$ @BenReiniger Can it be saved as .pb graph? $\endgroup$
    – Jodh Singh
    Aug 7 '19 at 8:14
  • $\begingroup$ @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? $\endgroup$ Aug 7 '19 at 21:37
1
$\begingroup$

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.
$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – HenryHub
    Mar 2 '19 at 15:49
  • $\begingroup$ 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 $\endgroup$
    – HenryHub
    Mar 2 '19 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.