我在使用自定义层TrajGRU时,先根据ConvLSTMCell重写了TrajGRUCell,然后TrajGRU继承自ConvRNN。写了get_config from_config方法结果还是出现下面报错。
Unable to restore custom object of type _tf_keras_rnn_layer currently.
Please make sure that the layer implements `get_config`and `from_config` when saving. In addition,
please use the `custom_objects` arg when calling `load_model()`.
随便调了下custom_objects居然过了。
据我所知没有指定出所有的自定义元素的报错因该是这个:
ValueError: Unknown layer: SincConv1D
现在变成这样一整块报错了,有点迷惑。既然是custom_objects的问题就说一下吧。
custom_objects需要指定你的自定义层来自哪,如果是外部包,例如tfa就像这样指定:
custom_objects={
'hamming_loss': tfa.metrics.HammingLoss(mode="multilabel", name="hamming_loss"),
'attention': attention(return_sequences=True)
}
前面是类在你代码中的名字像tf,np这样的缩写,后面是真实的类名地址。
下面是我的例子:
new_model = tf.keras.models.load_model(".\\保存模型\\Test_Traj",compile=False,
custom_objects={"TrajGRUCell":TrajGRUCell,"TrajGRU":TrajGRU}, )
因为模型和类都是在这个文件中定义的,直接名字就行。
参考:
https://stackoverflow.com/questions/66408995/unable-to-restore-custom-object-of-type-tf-keras-metric-currently-using-the-ham
https://stackoverflow.com/questions/61767723/get-config-missing-while-loading-previously-saved-model-without-custom-layers
https://blog.csdn.net/m0_38140207/article/details/106194312
https://blog.csdn.net/weixin_38145317/article/details/99541950