tensorflow/serving - 重塑的输入是一个有 100 个值的张量,但请求的形状有 10000 个

tensorflow/serving - Input to reshape is a tensor with 100 values, but the requested shape has 10000

当我训练 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

我如何称呼服务 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}}]]" }

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

欢迎任何想法!

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

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

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

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

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

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

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

为此,您必须使用 Tensorboard 生成图形。如果你的模型是基于 tensorflow 的,你可以 article 并且你会很容易找到关于 Keras 模型的类似文章。

例如-在我的例子中是 [[{{node chars/Reshape_1}}]]。所以我寻找 chars 节点

然后 Reshape_1chars 节点内。

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