保存Keras训练的模型 不推荐使用pickle或cPickle。 (1) 如果只保存模型结构,代码如下: [python] view plain copy # save as JSON json_string = model.to_json() # save as YAML yaml_string = model.to_yaml() # model reconstruction from JSON: from keras.modelsimport model_from_json model = model_from_json(json_string) # model reconstruction from YAML model =model_from_yaml(yaml_string) (2) 如果需要保存数据: [python] view plain copy model.save_weights('my_model_weights.h5') model.load_weights('my_model_weights.h5') (3) 综合运用: [python] view plain copy json_string = model.to_json() open('my_model_architecture.json','w').write(json_string) model.save_weights('my_model_weights.h5') model = model_from_json(open('my_model_architecture.json').read()) model.load_weights('my_model_weights.h5')