我跟着this great tutorial并成功训练了一个模型(在CloudML上)。我的代码也使预测脱机,但现在我正在尝试使用Cloud ML进行预测并遇到一些问题。
为了部署我的模型,我跟着this tutorial。现在我有一个代码,通过TFRecords
生成apache_beam.io.WriteToTFRecord
,我想对那些TFRecords
做出预测。为此,我关注this article,我的命令如下:
gcloud ml-engine jobs submit prediction $JOB_ID --model $MODEL --input-paths gs://"$FILE_INPUT".gz --output-path gs://"$OUTPUT"/predictions --region us-west1 --data-format TF_RECORD_GZIP
但我只得到错误:'Exception during running the graph: Expected serialized to be a scalar, got shape: [64]
它似乎期望数据采用不同的格式。我找到了JSON here的格式规范,但无法找到如何使用TFrecords。
更新:这是saved_model_cli show --all --dir
的输出
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['prediction']:
The given SavedModel SignatureDef contains the following input(s):
inputs['example_proto'] tensor_info:
dtype: DT_STRING
shape: unknown_rank
name: input:0
The given SavedModel SignatureDef contains the following output(s):
outputs['probability'] tensor_info:
dtype: DT_FLOAT
shape: (1, 1)
name: probability:0
Method name is: tensorflow/serving/predict
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['example_proto'] tensor_info:
dtype: DT_STRING
shape: unknown_rank
name: input:0
The given SavedModel SignatureDef contains the following output(s):
outputs['probability'] tensor_info:
dtype: DT_FLOAT
shape: (1, 1)
name: probability:0
Method name is: tensorflow/serving/predict
导出模型时,需要确保它是“batchable”,即输入占位符的外部维度具有shape=[None]
,例如,
input = tf.Placeholder(dtype=tf.string, shape=[None])
...
这可能需要稍微重新处理图形。例如,我看到输出的形状被硬编码为[1,1]。最外层的维度应该是None
,这可能会在您修复占位符时自动发生,或者可能需要进行其他更改。
鉴于输出的名称是probabilities
,我还希望最里面的维度> 1,即预测的类的数量,所以像[None, NUM_CLASSES]
。