【keras自定义层踩坑】keras保存带自定义层模型时出现AttributeError: ‘NoneType‘ object has no attribute ‘replace‘

项目场景:

我的 python 版本 3.6.5
tensorflow 版本 2.3.0


简单的自定义模型

import tensorflow as tf
import tensorflow.keras as keras
class x(keras.layers.Layer):
    def build(self, input_shape):
        self.add_weight()

inputs = keras.layers.Input(1)
outputs = x()(inputs)
model = keras.models.Model(inputs, outputs)
model.save("temp_model")

可以运行,但是在保存模型时报错

AttributeError: 'NoneType' object has no attribute 'replace'

详细信息

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-160-d7ce2ee17e65> in <module>
      6 outputs = x()(inputs)
      7 model = keras.models.Model(inputs, outputs)
----> 8 model.save("temp_model")

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options)
   1977     """
   1978     save.save_model(self, filepath, overwrite, include_optimizer, save_format,
-> 1979                     signatures, options)
   1980 
   1981   def save_weights(self,

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
    132   else:
    133     saved_model_save.save(model, filepath, overwrite, include_optimizer,
--> 134                           signatures, options)
    135 
    136 

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\saved_model\save.py in save(model, filepath, overwrite, include_optimizer, signatures, options)
     78     # we use the default replica context here.
     79     with distribution_strategy_context._get_default_replica_context():  # pylint: disable=protected-access
---> 80       save_lib.save(model, filepath, signatures, options)
     81 
     82   if not include_optimizer:

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\saved_model\save.py in save(obj, export_dir, signatures, options)
    974 
    975   _, exported_graph, object_saver, asset_info = _build_meta_graph(
--> 976       obj, export_dir, signatures, options, meta_graph_def)
    977   saved_model.saved_model_schema_version = constants.SAVED_MODEL_SCHEMA_VERSION
    978 

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\saved_model\save.py in _build_meta_graph(obj, export_dir, signatures, options, meta_graph_def)
   1059   # Note we run this twice since, while constructing the view the first time
   1060   # there can be side effects of creating variables.
-> 1061   _ = _SaveableView(checkpoint_graph_view)
   1062   saveable_view = _SaveableView(checkpoint_graph_view, wrapped_functions)
   1063   object_saver = util.TrackableSaver(checkpoint_graph_view)

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\saved_model\save.py in __init__(self, checkpoint_view, wrapped_functions)
    176     self.checkpoint_view = checkpoint_view
    177     trackable_objects, node_ids, slot_variables = (
--> 178         self.checkpoint_view.objects_ids_and_slot_variables())
    179     self.nodes = trackable_objects
    180     self.node_ids = node_ids

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\tracking\graph_view.py in objects_ids_and_slot_variables(self)
    424     object_names = object_identity.ObjectIdentityDictionary()
    425     for obj, path in path_to_root.items():
--> 426       object_names[obj] = _object_prefix_from_path(path)
    427     node_ids = object_identity.ObjectIdentityDictionary()
    428     for node_id, node in enumerate(trackable_objects):

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\tracking\graph_view.py in _object_prefix_from_path(path_to_root)
     62   return "/".join(
     63       (_escape_local_name(trackable.name)
---> 64        for trackable in path_to_root))
     65 
     66 

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\tracking\graph_view.py in <genexpr>(.0)
     62   return "/".join(
     63       (_escape_local_name(trackable.name)
---> 64        for trackable in path_to_root))
     65 
     66 

~\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\tracking\graph_view.py in _escape_local_name(name)
     55   # edges traversed to reach the variable, so we escape forward slashes in
     56   # names.
---> 57   return (name.replace(_ESCAPE_CHAR, _ESCAPE_CHAR + _ESCAPE_CHAR)
     58           .replace(r"/", _ESCAPE_CHAR + "S"))
     59 

AttributeError: 'NoneType' object has no attribute 'replace' 

解决方案:

tensor未命名错误

self.add_weight(name='name') #只需要在build中定义训练变量时,加上名字即可
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值