gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:45 25 4
gpt4 key购买 nike

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

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

关于python - tensorflow/serving - reshape 的输入是一个具有 100 个值的张量,但请求的形状有 10000 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com