Saving and loading the model
To save the weights of the Keras model, simply call the save
function, and the model is serialized into .hdf5
format:
model.save('bi_lstm_sentiment.h5')
To load the model, use the load_model
function provided by Keras as follows:
from keras.models import load_model loaded_model = load_model('bi_lstm_sentiment.h5')
It is now ready for evaluation and does not need to be compiled. For example, on the same test set we must obtain the same results:
test_loss, test_acc = loaded_model.evaluate(X_test, y_test)
print("Testing loss: {:.5}; Testing Accuracy: {:.2%}" .format(test_loss, test_acc))