通过CloudML获取TFrecords的批量预测

我按照这个优秀的教程成功地在CloudML上训练了一个模型。我的代码也可以离线进行预测,但现在我试图使用Cloud ML进行预测时遇到了一些问题。

为了部署我的模型,我遵循了这个教程。现在我有一段代码通过apache_beam.io.WriteToTFRecord生成TFRecords,我想为这些TFRecords进行预测。为此,我正在按照这篇文章进行操作,我的命令如下所示:

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格式的规格在这里,但没有找到如何用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/predictsignature_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

回答:

在导出您的模型时,您需要确保它是“可批处理的”,即输入占位符的外部维度应设置为shape=[None],例如:

input = tf.Placeholder(dtype=tf.string, shape=[None])...

这可能需要对图进行一些修改。例如,我看到您的输出的形状被硬编码为[1,1]。最外层的维度应该是None,这可能在您修复占位符时自动发生,或者可能需要其他更改。

考虑到输出的名称是probabilities,我还期望最内层的维度大于1,即预测的类别数量,因此应为[None, NUM_CLASSES]的形式。

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注