解决tf2 NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional mod

解决tf2 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_model.h5')
new_model = keras.models.load_model('save_model.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_weights('model_weight')
new_model = MyNet()
new_model.load_weights('model_weight')

自定义模型只保存参数(model_weight模型),官方模型保存参数和权重(.h5模型)

欢迎大家交流学习,任何问题都可以留言


版权声明:本文为weixin_41194171原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。