【问题标题】:tensorflow/serving - Input to reshape is a tensor with 100 values, but the requested shape has 10000tensorflow/serving - reshape 的输入是一个有 100 个值的张量,但请求的形状有 10000
【发布时间】:2019-09-13 15:55:41
【问题描述】:

当我训练我的 tf.keras 功能 API 模型并使用 tensorflow/serving docker 映像为其提供服务时,我在调用 API 时遇到了形状错误。

我如何构建模型:

from tensorflow.keras.layers import Dense, DenseFeatures, Input
from tensorflow.keras.models import Model
from tensorflow import feature_column, string as tf_string
import os

feature_layer_inputs = {}
feature_columns = []
for header in ['categorical_one', 'categorical_two', 'categorical_three']:
    feature_columns.append(feature_column.indicator_column(
        feature_column.categorical_column_with_hash_bucket(header, hash_bucket_size=100)))
    feature_layer_inputs[header] = Input(shape=(1,), name=header, dtype=tf_string)


fc_layer = DenseFeatures(feature_columns)
fc = fc_layer(feature_layer_inputs)

''' Feature Columns to Dense and merge with attention output '''

fc = Dense(300, activation='relu')(fc)
fc = Dense(324, activation='relu')(fc)

pred = Dense(num_classes, activation='softmax')(fc)

inputs = [v for v in feature_layer_inputs.values()]

model = Model(inputs=inputs, outputs=pred)

model.compile(...)

model.fit(...)

saved_model_path = "C:/Temp/saved_models/{}".format(int(time.time()))
os.mkdir(saved_model_path)

model.save(saved_model_path)

我的服务签名定义如下所示:

The given SavedModel SignatureDef contains the following input(s):
inputs['categorical_one'] tensor_info:
dtype: DT_STRING
shape: (-1, 1)
name: serving_default_categorical_one:0
inputs['categorical_two'] tensor_info:
dtype: DT_STRING
shape: (-1, 1)
name: serving_default_categorical_two:0
inputs['categorical_three'] tensor_info:
dtype: DT_STRING
shape: (-1, 1)
name: serving_default_categorical_three:0
The given SavedModel SignatureDef contains the following output(s):
outputs['dense'] tensor_info:
dtype: DT_FLOAT
shape: (-1, num_classes)
name: StatefulPartitionedCall:0
Method name is: tensorflow/serving/predict

我如何调用 Serving API:

curl -d '{"instances": [ {"categorical_one": "ABC", "categorical_two": "DEF", "categorical_three": "GHI"} ] }' -X POST http://localhost:8501/v1/models/my-model-name/versions/1:predict

我收到的错误信息:

{ "error": "Input to reshape is a tensor with 100 values, but the requested shape has 10000\n\t [[{{node StatefulPartitionedCall/StatefulPartitionedCall/model/dense_features/categorical_one_indicator/Reshape}}]]" }

请注意,我只是发布了代码的关键部分,而不是每一行。

欢迎提出任何想法!

【问题讨论】:

    标签: python tensorflow keras tensorflow-serving tf.keras


    【解决方案1】:

    看起来,tensorflow 不喜欢 DenseFeatures 的输入层中明确定义的形状。 我更改了以下代码行并且它起作用了。

    feature_layer_inputs[header] = Input(shape=(), name=header, dtype=tf_string)
    

    我会用一个真实的训练模型进一步检查结果..

    【讨论】:

      【解决方案2】:

      我也遇到了类似的问题,我发现调试 tensorflow 服务有点困难。这是我遵循的。

      注意错误发生的确切位置。在你的情况下,它说:

      [[{{node StatefulPartitionedCall/StatefulPartitionedCall/model/dense_features/categorical_one_indicator/Reshape}}]]
      

      通过这些信息,您将准确地找到它期望的输入类型以及它在计算图的层次结构中的位置。

      为此,您必须使用 Tensorboard 生成图表。如果您的模型是基于 tensorflow 的,您可以通过 article 轻松找到 Keras 模型的类似文章。

      例如-就我而言,它位于[[{{node chars/Reshape_1}}]]。于是我找了chars节点

      然后在chars 节点内Reshape_1

      现在您可以看到输入和输出以及其他有用的属性,例如形状。它帮助我准确地理解了问题所在。

      【讨论】:

        猜你喜欢
        • 2018-06-21
        • 1970-01-01
        • 2019-12-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多