1
votes

The question:

With a keras model (partly) specified such as this:

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10)

Is it in any way possible to save all details in the model for later use?


The details:

I've been following an example from machinelearningmastery.com and trying to modify and add traits / arguments of the model such as

  • activation='relu'
  • activation='sigmoid'
  • metrics=['accuracy']

And as the question suggests, I'd like to store model settings for later use. I understand that these arguments are parts of different functions, but shouldn't it be possible all the same?

What I've tried:

1. model.save() and model.load()

Only returns

AttributeError: 'Sequential' object has no attribute 'load'

2. model.get_config()

Here I've been able to find some of the settings such as:

[{'class_name': 'Dense', 'config': {'activation': 'relu',

But I haven't found a way to load that config as a standalone model, and more often than not, I can't seem to find all settings.

3. I've also checked other posts such as Keras - Reuse weights from a previous layer - converting to keras tensor, but all aspects of the model don't seem to be covered.

Any suggestions?

2
quick google gave me the following result: machinelearningmastery.com/save-load-keras-deep-learning-models - here, you store the model config as JSON and retrieve it later on!Cut7er

2 Answers

3
votes

Instead of trying model.load() try using load_model() provided by keras to load the model you saved using model.save()

from keras.models import load_model   
load_model(filepath)

Also you can save the model as json using model.to_json() and load from json using model_from_json()

You can see more ways to save and load a model in Keras Documentation here

2
votes

model.save() will do the trick to save the model, to load it use from keras.models import load_model and use model=load_model(model_name) to load the model