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


# 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()))
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.