tensorflow加载模型出现Unable to restore custom object of type _tf_keras_rnn_layer currently.

我在使用自定义层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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值