- Notifications
You must be signed in to change notification settings - Fork19.7k
Closed
Description
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_modelmodel = 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 ifmodel andmodel2 are run under same session (same notebook session). If I serializelstmweights 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?
Metadata
Metadata
Assignees
Labels
No labels