问题
用tensorflow2 自定义了一个模型,用checkpoint选择最优模型保存,但是在用 load_model()装载模型的时候遇到 No model found in config file.
原因
把模型给换成顺序模型,如下所示是没有问题的。
原因在于,要保存h5文件,要求模型是 a Functional model 或者 a Sequential model,而对于subclassed models,我自己定义的模型应该属于这类,就不起作用,因为 isn’t safely serializable,所以建议是要换成 save_weights
而不是 load_model()
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`.
解决方案
将checkpoint的函数的filepath参数改变,增加 save_weights_only参数;预测时用 load_weights 函数去装载