Tensorflow2 ValueError: Could not find matching function to call loaded from the SavedModel

Tensorflow2 ValueError: Could not find matching function to call loaded from the SavedModel

我正在尝试从 json 字符串中获取数据,将其转换为 tensorflow 可以使用的格式并进行预测。

import tensorflow as tf
import pandas as pd
import json
import matplotlib.pyplot as ply
%matplotlib inline
from sklearn.model_selection import train_test_split
import numpy as np

new_model = tf.keras.models.load_model('demo_2020_05/demo_1')
json_string = '[{"S8":"String Data 8" , "S9":"String Data 9" , "S4":"String Data 4" , "S3":"String Data 3" , "S7":"String Data 7" , "S5":"String Data 5" , "S2":"String Data 2" , "S6":"String Data 6" , "I2": 400 , "I3": 0 , "I1": 0  , "S1":"String Data 1"  ,"S10":"String Data 10"}]'
a_json = json.loads(json_string)


predict = {}
for i in a_json: 
    for key, val in i.items():      
         predict[key] = []

for i in a_json:
    for key, val in i.items():
        temp = []
        temp = predict[key]
        temp.append(val)
        predict[key] = temp

for i in a_json:
    for key, val in i.items():
        temp = []
        temp = predict[key]
        temp_np = np.array(temp)
        predict[key] = temp_np

print(predict)
    {'S1': array(['String Data 1'], dtype='<U42'), 'S2': array(['String Data 2'], dtype='<U23'), 'S3': array([String Data 3'], dtype='<U6'), 'S4': array(['String Data 4'], dtype='<U5'), 'S5': array(['String Data 5'], dtype='<U5'), 'S6': array(['String Data 6'], dtype='<U6'), 'S7': array(['String Data 7'], dtype='<U2'), 'S8': array(['String Data 8'], dtype='<U12'), 'I1': array([400]), 'I2': array([0]), 'I3': array([0]), 'S9': array(['String Data 9'], dtype='<U11'), 'S10': array(['String Data 10'], dtype='<U7')}






new_model.predict(predict)

这是我在构建模型时传递给模型的确切结构和数据。我有 copy/pasted json、for 循环和预测调用,它可以工作并在创建模型的 jupyter notebook 中进行预测。

这是我收到以下错误的地方:

ValueError: Could not find matching function to call loaded from the SavedModel. Got:

* {
      'S1': <tf.Tensor 'features:0' shape=(None, 1) dtype=string>
    , 'S2': <tf.Tensor 'features_1:0' shape=(None, 1) dtype=string>
    , 'S3': <tf.Tensor 'features_2:0' shape=(None, 1) dtype=string>
    , 'S4': <tf.Tensor 'features_3:0' shape=(None, 1) dtype=string>
    , 'S5': <tf.Tensor 'features_4:0' shape=(None, 1) dtype=string>
    , 'S6': <tf.Tensor 'features_5:0' shape=(None, 1) dtype=string>
    , 'S7': <tf.Tensor 'features_6:0' shape=(None, 1) dtype=string>
    , 'I1': <tf.Tensor 'features_7:0' shape=(None, 1) dtype=int64>
    , 'S8': <tf.Tensor 'features_8:0' shape=(None, 1) dtype=string>
    , 'S9': <tf.Tensor 'features_9:0' shape=(None, 1) dtype=string>
    , 'S10': <tf.Tensor 'features_10:0' shape=(None, 1) dtype=string>
    , 'I2': <tf.Tensor 'features_11:0' shape=(None, 1) dtype=int64>
    , 'I3': <tf.Tensor 'features_12:0' shape=(None, 1) dtype=int64>
    }
* None
  Keyword arguments: {}

Expected these arguments to match one of the following 1 option(s):

Option 1:
Positional arguments (2 total):
 * {
  'I3': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I3')
, 'S7': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S7')
, 'S8': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S8')
, 'I2': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I2')
, 'S3': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S3')
, 'S6': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S6')
, 'S9': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S9')
, 'S2': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S2')
, 'I1': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I1')
, 'S1': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S1')
, 'S4': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S4')
, 'S5': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S5')
, 'S10': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S10')
}
* None
  Keyword arguments: {}

predict() 似乎在寻找不同的 datatype/structure。但是,我已尝试将我认为它要求执行以下操作的内容发送给它。

ta_S4 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S4 = ta_S4.write(0,'String Data 4')

ta_S3 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S3 = ta_S3.write(0,'String Data 3')

ta_S7 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S7 = ta_S7.write(0,'String Data 7')

ta_S5 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S5 = ta_S5.write(0,'String Data 5')

ta_S2 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S2 = ta_S2.write(0,'String Data 2')

ta_S6 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S6 = ta_S6.write(0,'String Data 6')

ta_I2= tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I2  = ta_I2.write(0, 400)

ta_I3 = tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I3 = ta_I3.write(0, 0)

ta_I1 = tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I1 = ta_I1.write(0, 0)

ta_S1 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S1 = ta_S1.write(0,'String Data 1')

ta_S10 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S10 = ta_S10 .write(0,'String Data 10')




predict = {'S8': ta_S8,
 'S9': ta_S9,
 'S4': ta_S4,
 'S3': ta_S3,
 'S7': ta_S7,
 'S5': ta_S5,
 'S2': ta_S2,
 'S6': ta_S6,
 'I2': ta_I2,
 'I3': ta_I3,
 'I1': ta_I1,
 'S1': ta_S1,
 'S10': ta_S10
          }

new_model.predict(predict)

此错误为:

ValueError: Failed to find data adapter that can handle input: (<class 'dict'> containing {"<class 'str'>"} keys and {"<class 'tensorflow.python.ops.tensor_array_ops.TensorArray'>"} values), <class 'NoneType'>

我不确定要传递给 new_model.predict() 的内容。

编辑 1:

把问题问的简单一点。我如何创建一个类似于此(如下所示)的数据结构以传递给 new_model.predict() 以获得预测?

{
      'I3': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I3')
    , 'S7': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S7')
    , 'S8': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S8')
    , 'I2': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I2')
    , 'S3': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S3')
    , 'S6': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S6')
    , 'S9': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S9')
    , 'S2': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S2')
    , 'I1': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I1')
    , 'S1': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S1')
    , 'S4': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S4')
    , 'S5': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S5')
    , 'S10': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S10')
    }
    * None
      Keyword arguments: {} 

这已通过

解决
pip uninstall tensorflow
pip uninstall tensorboard
pip install -q tf-nightly
pip install --ignore-installed tf-nightly