KerasRegressor serialize/save a model as a .h5df












1












$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()))









share|improve this question











$endgroup$

















    1












    $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()))









    share|improve this question











    $endgroup$















      1












      1








      1





      $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()))









      share|improve this question











      $endgroup$




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






      python deep-learning keras regression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      HenryHub

















      asked yesterday









      HenryHubHenryHub

      1596




      1596






















          2 Answers
          2






          active

          oldest

          votes


















          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





          share









          $endgroup$













          • $begingroup$
            Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
            $endgroup$
            – HenryHub
            1 min ago



















          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.







          share|improve this answer









          $endgroup$













          • $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
            5 hours ago












          • $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
            1 hour ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46488%2fkerasregressor-serialize-save-a-model-as-a-h5df%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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





          share









          $endgroup$













          • $begingroup$
            Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
            $endgroup$
            – HenryHub
            1 min ago
















          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





          share









          $endgroup$













          • $begingroup$
            Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
            $endgroup$
            – HenryHub
            1 min ago














          0












          0








          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





          share









          $endgroup$



          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






          share











          share


          share










          answered 8 mins ago









          Ben ReinigerBen Reiniger

          15218




          15218












          • $begingroup$
            Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
            $endgroup$
            – HenryHub
            1 min ago


















          • $begingroup$
            Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
            $endgroup$
            – HenryHub
            1 min ago
















          $begingroup$
          Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
          $endgroup$
          – HenryHub
          1 min ago




          $begingroup$
          Thank you so much... can you give me another tip? Is the KerasRegressor just a wrapper for the scikit-learn NN?
          $endgroup$
          – HenryHub
          1 min ago











          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.







          share|improve this answer









          $endgroup$













          • $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
            5 hours ago












          • $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
            1 hour ago
















          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.







          share|improve this answer









          $endgroup$













          • $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
            5 hours ago












          • $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
            1 hour ago














          1












          1








          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.







          share|improve this answer









          $endgroup$



          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.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          pcko1pcko1

          1,536317




          1,536317












          • $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
            5 hours ago












          • $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
            1 hour ago


















          • $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
            5 hours ago












          • $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
            1 hour ago
















          $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
          5 hours ago






          $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
          5 hours ago














          $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
          1 hour ago




          $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
          1 hour ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46488%2fkerasregressor-serialize-save-a-model-as-a-h5df%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus I

          Tabula Rosettana

          How to label and detect the document text images