question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

model.save and load giving different result

See original GitHub issue

I am trying to save a simple LSTM model for text classification. The input of the model is padded vectorized sentences.

model = Sequential()
model.add(LSTM(40, input_shape=(16, 32)))
model.add(Dense(20))
model.add(Dense(8, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

For saving I’m using the following snippet:

for i in range(50):
    from sklearn.cross_validation import train_test_split

    data_train, data_test, labels_train, labels_test = train_test_split(feature_set, dummy_y, test_size=0.1, random_state=i)
    accuracy = 0.0
    try:
        with open('/app/accuracy', 'r') as file:
            for line in file:
                accuracy = float(line)
    except Exception:
        print("error")
    model.fit(data_train, labels_train, nb_epoch=50)
    loss, acc = model.evaluate(feature_set, dummy_y)
    if acc > accuracy:
        model.save_weights("model.h5", overwrite=True)
        model.save('my_model.h5', overwrite=True)
        print("Saved model to disk.\nAccuracy:")
        print(acc)
        with open('/app/accuracy', 'w') as file:
            file.write('%f' % acc)

But whenever I’m trying to load the same model

from keras.models import load_model
model = load_model('my_model.h5')

I’m getting random accuracy like an untrained model. same result even when trying to load weights separately. If I set the weights

lstmweights=model.get_weights()
model2.set_weights(lstmweights)

like above. It is working if model and model2 are run under same session (same notebook session). If I serialize lstmweights and try to load it from different place, again I’m getting result like untrained model. It seems saving only the weights are not enough. So why model.save is not working. Any known point?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:127
  • Comments:315 (16 by maintainers)

github_iconTop GitHub Comments

130reactions
HarshaVardhanPcommented, Apr 22, 2017

I crosschecked all these functions - they seem to be working properly. model.save(), load_model(), model.save_weights() and model.load_weights()

90reactions
pras135commented, May 7, 2017

Use model.model.save() instead of model.save()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Loading a Previously Saved Keras Model Gives Different ...
My preferred way of doing this is by saving the model's weights, which are randomized at the beginning of the model creation, and...
Read more >
Trained and Loaded Keras Sequential Model is giving ...
It is most likely because you only save the model structure and the model weights. You do not save the state of your...
Read more >
Tensorflow 2 + Keras - Loading Saved Model Giving Different ...
Check your data is getting the same pre-processing as when you trained the model. The same order and all.
Read more >
Load model in TensorFlow gives a different result than the ...
I'm using the TensorFlow library in Python. After creating a model and saving it, if I load the entire model, I get inconsistent...
Read more >
How to Save and Load Your Keras Deep Learning Model
Keras provides the ability to describe any model using JSON format with a to_json() function. This can be saved to a file and...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found