无法使用“model.save()”保存自定义模型
Posted
技术标签:
【中文标题】无法使用“model.save()”保存自定义模型【英文标题】:Saving Custom Model cannot be done with `model.save()` 【发布时间】:2020-12-23 00:16:53 【问题描述】:我的 python 版本 3.6.5 张量流 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'
我从here引用了这个简单的模型,
我看到它已被处理here,
但我仍然可以在我的电脑和other's as well看到这个
【问题讨论】:
【参考方案1】:这似乎是张量流中的一个错误。只需为您创建的权重命名,问题就解决了:
self.add_weight(name='name')
【讨论】:
以上是关于无法使用“model.save()”保存自定义模型的主要内容,如果未能解决你的问题,请参考以下文章