栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

NotImplementedError:在__init__中带有参数的图层必须覆盖get_config。

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NotImplementedError:在__init__中带有参数的图层必须覆盖get_config。

这不是一个错误,这是一个功能。

此错误使您知道TF无法保存模型,因为它无法加载模型。
具体来说,它将无法重新实例化您的自定义

Layer
类:
enprer



deprer


要解决此问题, 只需

get_config

根据您添加的新参数覆盖其方法。

层配置是包含层配置的Python字典(可序列化)。稍后可以从此配置中重新实例化同一层(没有经过训练的权重)。


例如,如果您的

enprer
班级看起来像这样:

class enprer(tf.keras.layers.Layer):    def __init__(        self,        vocab_size, num_layers, units, d_model, num_heads, dropout,        **kwargs,    ):        super().__init__(**kwargs)        self.vocab_size = vocab_size        self.num_layers = num_layers        self.units = units        self.d_model = d_model        self.num_heads = num_heads        self.dropout = dropout    # Other methods etc.

那么您只需要重写此方法:

    def get_config(self):        config = super().get_config().copy()        config.update({ 'vocab_size': self.vocab_size, 'num_layers': self.num_layers, 'units': self.units, 'd_model': self.d_model, 'num_heads': self.num_heads, 'dropout': self.dropout,        })        return config

当TF看到这一点(针对两个类)时,您将能够保存模型。

因为现在加载模型时,TF将能够从config重新实例化同一层。


Layer.from_config

的源代码可以更好地了解其工作方式:

@classmethoddef from_config(cls, config):  return cls(**config)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/610643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号