ValueError: The two structures don‘t have the same sequence length.

时间:2025-05-06 08:41:30

问题描述

环境:

windows10
cuda 10.0
tensorflow 2.0

操作步骤

  1. 根据tf2.0模型导出导入学习,把训练好的模型导出来。

  2. 把刚刚导出的模型导入,进行测试

测试导入模型的时候,报错如下

报错摘要:ValueError: The two structures don’t have the same sequence length. Input structure has length 1, while shallow structure has length 2.

Traceback (most recent call last):
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\eager\", line 1204, in _convert_inputs_to_signature
    expand_composites=True)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\util\", line 833, in flatten_up_to
    check_subtrees_length=check_subtrees_length)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\util\", line 718, in assert_shallow_structure
    input_length=len(input_tree), shallow_length=len(shallow_tree)))
ValueError: The two structures don't have the same sequence length. Input structure has length 1, while shallow structure has length 2.

报错摘要:ValueError: Structure of Python function inputs does not match input_signature. Inputs ((array([[[[0.],[0.]]]], dtype=float32),)),input_signature((TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name=‘flatten_input’), TensorSpec(shape=(), dtype=, name=’’))).

Traceback (most recent call last):
  File "G:/tensorflow_learn/model_import_savemodel.py", line 14, in <module>
    y_pred = model(test_data)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\saved_model\", line 327, in _call_attribute
    return instance.__call__(*args, **kwargs)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\eager\def_function.py", line 432, in __call__
    *args, **kwds)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\eager\", line 1169, in canonicalize_function_inputs
    self._flat_input_signature)
  File "F:\Anaconda\install\lib\site-packages\tensorflow\python\eager\", line 1208, in _convert_inputs_to_signature
    (str(inputs), str(input_signature)))
ValueError: Structure of Python function inputs does not match input_signature. Inputs ((array([[[[0.],
         [0.],
         [0.],
         ...,
         [0.],
         [0.],
         [0.]],
		 ...
		 ...
		 ...
		 [0.],
         [0.],
         [0.]]]], dtype=float32),)), input_signature((TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='flatten_input'), TensorSpec(shape=(), dtype=, name=''))).

问题解决

其实网上这类的问题很少!难道是因为tensorflow2.0刚出来没多久的吗?
最后在Google上发现了一个同样问题的,就试了一下他的方法。成功了!
具体方法:
tensorflow 2.0 的模型导入有问题(bug),于是我换了tensorflow 2.1,就好啦!