NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or

当与保存的模型中含有类(调用了类),使用model.save()保存为.h5格式的模型时会报错:NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.

解决方法一(也是更推荐的方法):根据说明,显式设置model.save("模型名称", save_format='tf')或简单地使用model.save("模型名称"),调用时则采用tf.keras.models.load_model("模型名称路径")即可。

保存为save_format='tf'格式的模型是一个文件夹,文件夹内包含三个文件,调用时直接写模型文件夹的路径即可。

解决方法二:   将模型改为Sequential的形式,大模型或者稍微复杂的模型(如加入了残差网络的模型)改成这种方式比较麻烦,因此不推荐。

解决方法三:使用ModelCheckpoint()保存ckpt格式的模型,我还没试过这种方法,哈哈。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值