无法使用`model.save()`来保存自定义模型
我的 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()
q = keras.layers.Input(1)
o = x()(q)
model = keras.models.Model(q, o)
model.save("temp_model")
它失败了 AttributeError: 'NoneType' object has no attribute 'replace'
怎么了?
欲知详情,
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-160-d7ce2ee17e65> in <module>
6 o = x()(q)
7 qwer = keras.models.Model(q, o)
----> 8 qwer.save("qwer")
~AppDataRoamingPythonPython36site-packagestensorflowpythonkerasenginetraining.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,
~AppDataRoamingPythonPython36site-packagestensorflowpythonkerassavingsave.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
~AppDataRoamingPythonPython36site-packagestensorflowpythonkerassavingsaved_modelsave.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:
~AppDataRoamingPythonPython36site-packagestensorflowpythonsaved_modelsave.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
~AppDataRoamingPythonPython36site-packagestensorflowpythonsaved_modelsave.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)
~AppDataRoamingPythonPython36site-packagestensorflowpythonsaved_modelsave.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
~AppDataRoamingPythonPython36site-packagestensorflowpythontrainingtrackinggraph_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):
~AppDataRoamingPythonPython36site-packagestensorflowpythontrainingtrackinggraph_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
~AppDataRoamingPythonPython36site-packagestensorflowpythontrainingtrackinggraph_view.py in <genexpr>(.0)
62 return "/".join(
63 (_escape_local_name(trackable.name)
---> 64 for trackable in path_to_root))
65
66
~AppDataRoamingPythonPython36site-packagestensorflowpythontrainingtrackinggraph_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'
我从这里引用了这个简单的模型,
我看到这里已经处理了,
但我仍然可以在我的电脑和其他人的电脑上看到这个
回答
这似乎是 tensorflow 中的一个错误。只需为您创建的权重命名,问题就消失了:
self.add_weight(name='name')