加载保存的BertClassifer模型

时间:2020-06-16 16:57:34

标签: python tensorflow keras tensorflow-model-garden

我正在使用此colab笔记本中的以下示例: https://github.com/tensorflow/models/blob/master/official/colab/fine_tuning_bert.ipynb

它使用model.save()功能保存微调的模型。

我正在尝试使用tf.keras.models.load_model()加载相同的模型,但出现以下错误:

KeyError: 'name'
KeyError                                  Traceback (most recent call last)
in engine
----> 1 model = tf.keras.models.load_model('./saved_model/test')

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    183     if isinstance(filepath, six.string_types):
    184       loader_impl.parse_saved_model(filepath)
--> 185       return saved_model_load.load(filepath, compile)
    186 
    187   raise IOError(

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile)
    114   # TODO(kathywu): Add saving/loading of optimizer, compiled losses and metrics.
    115   # TODO(kathywu): Add code to load from objects that contain all endpoints
--> 116   model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
    117 
    118   # pylint: disable=protected-access

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py in load_internal(export_dir, tags, loader_cls)
    605       loader = loader_cls(object_graph_proto,
    606                           saved_model_proto,
--> 607                           export_dir)
    608       root = loader.get(0)
    609       if isinstance(loader, Loader):

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in __init__(self, *args, **kwargs)
    186     self._models_to_reconstruct = []
    187 
--> 188     super(KerasObjectLoader, self).__init__(*args, **kwargs)
    189 
    190     # Now that the node object has been fully loaded, and the checkpoint has

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir)
    121       self._concrete_functions[name] = _WrapperFunction(concrete_function)
    122 
--> 123     self._load_all()
    124     self._restore_checkpoint()
    125 

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_all(self)
    207     # loaded from config may create variables / other objects during
    208     # initialization. These are recorded in `_nodes_recreated_from_config`.
--> 209     self._layer_nodes = self._load_layers()
    210 
    211     # Load all other nodes and functions.

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_layers(self)
    307         continue
    308 
--> 309       layers[node_id] = self._load_layer(proto.user_object, node_id)
    310 
    311     for node_id, proto in metric_list:

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_layer(self, proto, node_id)
    333     # Detect whether this object can be revived from the config. If not, then
    334     # revive from the SavedModel instead.
--> 335     obj, setter = self._revive_from_config(proto.identifier, metadata, node_id)
    336     if obj is None:
    337       obj, setter = revive_custom_object(proto.identifier, metadata)

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _revive_from_config(self, identifier, metadata, node_id)
    350     else:
    351       obj = (
--> 352           self._revive_graph_network(metadata, node_id) or
    353           self._revive_layer_from_config(metadata, node_id))
    354 

/home/cdsw/.local/lib/python3.6/site-packages/tensorflow/python/keras/saving/saved_model/load.py in _revive_graph_network(self, metadata, node_id)
    386     else:
    387       model = models_lib.Functional(
--> 388           inputs=[], outputs=[], name=config['name'])
    389 
    390     # Record this model and its layers. This will later be used to reconstruct

KeyError: 'name'

有人可以建议我做错了什么,还是应该做其他事情来加载此模型?谢谢!

2 个答案:

答案 0 :(得分:0)

如前所述,name未定义。 model = tf.keras.models.load_model('./saved_model/test')指向PATH(文件夹)./saved_model/test,而不是模型的PATH。

答案 1 :(得分:0)

我在这里记录了我对KeyError(以及找到的解决方案)的体验:

https://github.com/tensorflow/models/issues/8692#issuecomment-727033061

这是由于自定义层的get_config()的实现不正确而导致的,它没有使用父级的基本配置,而该基础配置确实包含了name键,这对于重新加载keras是必需的。