model.save and load giving different result
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:127
- Comments:315 (16 by maintainers)
Top 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 >
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I crosschecked all these functions - they seem to be working properly. model.save(), load_model(), model.save_weights() and model.load_weights()
Use model.model.save() instead of model.save()