tensorflow2.0保存模型和提取模型

1.保存节点

  # 实例化Checkpoint,指定保存对象为model(如果需要保存Optimizer的参数也可加入)
  checkpoint = tf.train.Checkpoint(myModel=self.model)
  # ...(模型训练代码)
  # 模型训练完毕后将参数保存到文件(也可以在模型训练过程中每隔一段时间就保存一次)
  checkpoint.save(save_sess_path+'/model.ckpt')
  print("模型已经保存到:", save_sess_path)

2.保存模型

      myModel=self.model
      myModel.save(save_sess_path + '/the_save_model.h5')

自己搭建的模型不适用上述保存模式,需要用别的保存方式
Consider saving to the Tensorflow SavedModel format (by setting save_format=“tf”) or using save_weights.

json_config = model.to_json()
with open('model_config.json', 'w') as json_file:
    json_file.write(json_config)

model.save_weights('path_to_my_weights.h5')

with open('model_config.json') as json_file:
    json_config = json_file.read()
new_model = keras.models.model_from_json(json_config)
new_model.load_weights('path_to_my_weights.h5')

new_predictions = new_model.predict(x_test)
np.testing.assert_allclose(predictions, new_predictions, atol=1e-6)
————————————————
版权声明:本文为CSDN博主「Doit_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_31456593/article/details/88605422

3. 读取模型

        new_model = tf.keras.models.load_model(save_sess_path + '/the_save_model.h5')
      	new_predictions = new_model.predict(signals)
      	y_pred = np.testing.assert_allclose(labels ,new_predictions,atol = 1e-6)

4.预测keras中model.evaluate、model.predict和model.predict_classes的区别

  1. model.evaluate 用于评估您训练的模型。它的输出是model的acc和loss,而不是对输入数据的预测。

  2. .model.predict 实际预测,输入为test sample,输出为label。

  3. 在keras中有两个预测函数model.predict_classes(test) 和model.predict(test)。如果标签经过了one-hot编码,如[1,2,3,4,5]是标签类别,经编码后为[1 0 0
    0 0],[0 1 0 0 0]…[0 0 0 0 1]。

    4.model.predict_classes(test)预测的是类别,打印出来的值就是类别号。并且只能用于序列模型来预测,不能用于函数式模型。
    而model.predict(test)输出的还是5个编码值,要经过argmax(predict_test,axis=1)转化为类别号。

两者差异
1
输入输出不同
model.evaluate输入数据(data)和金标准(label),然后将预测结果与金标准相比较,得到两者误差并输出.
model.predict输入数据(data),输出预测结果
2
是否需要真实标签(金标准)
model.evaluate需要,因为需要比较预测结果与真实标签的误差
model.predict不需要,只是单纯输出预测结果,全程不需要金标准的参与.

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值