keras模型的保存与重新加载

 1 # 模型保存JSON文件
 2 model_json = model.to_json()
 3 with open('model.json', 'w') as file:
 4     file.write(model_json)
 5 
 6 # 保存模型权重值
 7 model.save_weights('model.json.h5')
 8 
 9 # 从JSON文件中加载模型
10 with open('model.json', 'r') as file:
11     model_json1 = file.read()
12 
13 # 加载模型
14 new_model = model_from_json(model_json1)
15 new_model.load_weights('model.json.h5')
16 
17 # 编译模型
18 # new_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
19 new_model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])

json文件保存模型的结构,h5文件保存模型的参数,加载模型后加载参数,然后需要编译模型;之后就可以进行评估和预测。

猜你喜欢

转载自www.cnblogs.com/ywheunji/p/10459866.html