【问题标题】:Can't save and load a model无法保存和加载模型
【发布时间】:2021-01-02 15:10:44
【问题描述】:

我只是跟着Keras.iohttps://keras.io/examples/nlp/semantic_similarity_with_bert/的这个教程

我可以运行它并且模型可以工作。 当我想以 h5 格式保存模型时,出现此错误:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-29-549810e352cb> in <module>()
----> 1 model.save('my_model.h5')

9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in get_config(self)
   2252 
   2253   def get_config(self):
-> 2254     raise NotImplementedError
   2255 
   2256   @classmethod

NotImplementedError:
-----------------------------------------------------------------------------

当我想用“SavedModel”格式做同样的事情时 我可以保存模型,但是当我尝试加载它时,出现此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites)
    403     _pywrap_utils.AssertSameStructure(nest1, nest2, check_types,
--> 404                                       expand_composites)
    405   except (ValueError, TypeError) as e:

ValueError: The two structures don't have the same nested structure.

First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}

Second structure: type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')

More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')" is not

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
7 frames
<ipython-input-33-ae06d36f12a1> in <module>()
----> 1 new_model = tf.keras.models.load_model('saved_model/my_model2', compile=False)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
    210       if isinstance(filepath, six.string_types):
    211         loader_impl.parse_saved_model(filepath)
--> 212         return saved_model_load.load(filepath, compile, options)
    213 
    214   raise IOError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile, options)
    145 
    146   # Finalize the loaded layers and remove the extra tracked dependencies.
--> 147   keras_loader.finalize_objects()
    148   keras_loader.del_tracking()
    149 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in finalize_objects(self)
    594         layers_revived_from_config.append(node)
    595 
--> 596     _finalize_saved_model_layers(layers_revived_from_saved_model)
    597     _finalize_config_layers(layers_revived_from_config)
    598 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_saved_model_layers(layers)
    783         call_fn = _get_keras_attr(layer).call_and_return_conditional_losses
    784         if call_fn.input_signature is None:
--> 785           inputs = infer_inputs_from_restored_call_function(call_fn)
    786         else:
    787           inputs = call_fn.input_signature[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in infer_inputs_from_restored_call_function(fn)
   1068   for concrete in fn.concrete_functions[1:]:
   1069     spec2 = concrete.structured_input_signature[0][0]
-> 1070     spec = nest.map_structure(common_spec, spec, spec2)
   1071   return spec
   1072 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
    651   for other in structure[1:]:
    652     assert_same_structure(structure[0], other, check_types=check_types,
--> 653                           expand_composites=expand_composites)
    654 
    655   flat_structure = (flatten(s, expand_composites) for s in structure)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites)
    409                   "Entire first structure:\n%s\n"
    410                   "Entire second structure:\n%s"
--> 411                   % (str(e), str1, str2))
    412 
    413 

ValueError: The two structures don't have the same nested structure.

First structure: type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}

Second structure: type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')

More specifically: Substructure "type=dict str={'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}" is a sequence, while substructure "type=TensorSpec str=TensorSpec(shape=(None, 128), dtype=tf.int32, name='inputs')" is not
Entire first structure:
{'input_ids': .}
Entire second structure:
.

------------------------------------------------------------------------------

有什么想法吗?

【问题讨论】:

  • 为了更有可能得到响应,您能否将完整的错误堆栈跟踪复制粘贴为您的问题中的代码?
  • 此外,如果您详细说明您为尝试解决问题所采取的步骤,您更有可能得到回复。例如,您在哪里在线查找资源。
  • 我刚刚添加了完整的错误
  • 如果你复制并粘贴代码而不是截图,它会更容易阅读:)
  • 有人知道吗?

标签: keras model


【解决方案1】:

我认为您正在使用不同版本的 TensorFlow 进行训练和保存。然后,您会遇到错误。请关注这些主题(here 和 here),那里讨论了加载 Keras 模型的一些常见问题。

【讨论】:

  • 我无法应用您的建议,因为我无法保存
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 2022-10-13
  • 2021-12-15
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多