【发布时间】: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