【问题标题】:ValueError: Cannot feed value of shape () for Tensor 'input_example_tensor:0', which has shape '(?,)'ValueError:无法为具有形状“(?,)”的张量“input_example_tensor:0”提供形状()的值
【发布时间】:2018-08-03 22:54:16
【问题描述】:

为什么会出现这个错误?

我找不到与我的案例类似的答案并解释为什么会发生这种情况。此类错误有很多信息,但似乎取决于很多原因,并且对于不同的情况,它可能有不同的解决方案。

所以我尝试加载TensorFlow 训练模型,然后将其转换为TensorFlow Serving 模型格式并进行预测。

iris_data.py 来自下面的导入文件。

这就是我导出模型的方式(火车已经这样做了,我只是从磁盘加载它):

import tensorflow as tf
import iris_data
from tensorflow.contrib import predictor

# Fetch the data
    (train_x, train_y), (test_x, test_y) = iris_data.load_data()

    # Feature columns describe how to use the input.
    my_feature_columns = []
    for key in train_x.keys():
        my_feature_columns.append(tf.feature_column.numeric_column(key=key))

    # Build 2 hidden layer DNN with 10, 10 units respectively.
    classifier = tf.estimator.DNNClassifier(
        feature_columns=my_feature_columns,
        # Two hidden layers of 10 nodes each.
        hidden_units=[10, 10],
        # The model must choose between 3 classes.
        n_classes=3,
        model_dir='G:\AI models')

    #converting into TensorFlow Serving model format 
    path = 'G:\AI models\serve'
    feature_spec = tf.feature_column.make_parse_example_spec(my_feature_columns)
    export_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
    servable_model_path = classifier.export_savedmodel(path, export_input_fn, as_text=True)

这是我尝试使用TensorFlow Serving 模型进行预测的方法:

 expected = ['Setosa', 'Versicolor', 'Virginica']
    predict_x = {
        'SepalLength': [5.1, 5.9, 6.9],
        'SepalWidth': [3.3, 3.0, 3.1],
        'PetalLength': [1.7, 4.2, 5.4],
        'PetalWidth': [0.5, 1.5, 2.1],
    }
path = 'G:\AI models\serve\\1519413839'
    predict_fn = predictor.from_saved_model(path)
    predictions = predict_fn(
        {"inputs": predict_x})

最后报错:

ValueError: Cannot feed value of shape () for Tensor 'input_example_tensor:0', which has shape '(?,)'

【问题讨论】:

  • 失败在哪一行?
  • @Yserbius predictions = predict_fn({"inputs": predict_x})

标签: python tensorflow machine-learning tensorflow-serving


【解决方案1】:

您必须使用示例 API 传递您的示例:

feature = {
    'SepalLength': tf.train.Feature(float_list=tf.train.FloatList(value=[5.1])),
    'SepalWidth': tf.train.Feature(float_list=tf.train.FloatList(value=[3.3])),
    'PetalLength': tf.train.Feature(float_list=tf.train.FloatList(value=[1.7])),
    'PetalWidth': tf.train.Feature(float_list=tf.train.FloatList(value=[0.5]))
}
example = tf.train.Example(
    features=tf.train.Features(
        feature=feature
    )
)
serialized_example = example.SerializeToString()

predictions = predict_fn({"inputs": [serialized_example]})

当然,如果你愿意,你可以传递很多例子。

有用的链接:http://shzhangji.com/blog/2018/05/14/serve-tensorflow-estimator-with-savedmodel/

【讨论】:

    【解决方案2】:

    () 是零阶张量(即标量)的维数,(?,) 是未知维数的一阶张量(向量)。

    如果没有完整的堆栈跟踪,我只能告诉你。

    【讨论】:

    • 我可以分享什么来帮助你?可能我没有以正确的方式转换模型...
    • 堆栈跟踪是打印在错误上方的行,它应该在您的代码中开始,并在 tensorflow 中结束。
    【解决方案3】:
    'G:\AI models\serve\\1519413839' 
    

    应该是

    'G:\\AI models\\serve\\1519413839'
    

    反斜杠需要转义。可能没有读取文件,从而使预测函数具有零维张量形状作为其数据。

    【讨论】:

    • 不幸的是它没有帮助我:(
    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 2019-07-23
    • 2023-03-19
    • 2018-04-29
    • 2018-04-05
    • 2017-12-06
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多