Skip to content

Instantly share code, notes, and snippets.

@JefferyW
Created January 8, 2020 22:12
Show Gist options
  • Save JefferyW/874f9fe315fc25596fdbb6393d681e3c to your computer and use it in GitHub Desktop.
Save JefferyW/874f9fe315fc25596fdbb6393d681e3c to your computer and use it in GitHub Desktop.
Keras save and load model
from keras.models import model_from_yaml
def saveModel(model, savename):
# serialize model to YAML
model_yaml = model.to_yaml()
with open(savename+".yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
print("Yaml Model ",savename,".yaml saved to disk")
# serialize weights to HDF5
model.save_weights(savename+".h5")
print("Weights ",savename,".h5 saved to disk")
def loadModel(savename):
with open(savename+".yaml", "r") as yaml_file:
model = model_from_yaml(yaml_file.read())
print "Yaml Model ",savename,".yaml loaded "
model.load_weights(savename+".h5")
print "Weights ",savename,".h5 loaded "
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment