【问题标题】:Cloud ML model predictionCloud ML 模型预测
【发布时间】:2019-01-19 05:50:48
【问题描述】:

我已经在云 ML 中部署了 tensorflow 保存的模型,用于文本分类,如下所示,

    input_x = graph.get_tensor_by_name('input_x:0')
    keep_prob = graph.get_tensor_by_name('keep_prob:0')
    predictions = graph.get_tensor_by_name('softmax/predictions:0')

feed_dict = {input_x: x_test, batch_size: 8, sequence_length: x_lengths,  keep_prob: 1.0}

它的部署没有错误。我有一个 csv 文件要预测。 --csv 文件--

"the test is completed"
"the test2 is done"

只得到错误。 如何将其转换为我训练的模型的 json,以便在云 ML 中进行批量预测?

saved_model_cli - 信息

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['batch_size'] tensor_info:
        dtype: DT_INT32
        shape: ()
        name: batch_size:0
    inputs['input_x'] tensor_info:
        dtype: DT_INT32
        shape: (-1, 25)
        name: input_x:0
    inputs['keep_prob'] tensor_info:
        dtype: DT_FLOAT
        shape: ()
        name: keep_prob:0
    inputs['sequence_length'] tensor_info:
        dtype: DT_INT32
        shape: (-1)
        name: sequence_length:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['predictions'] tensor_info:
        dtype: DT_INT64
        shape: (-1)
        name: softmax/predictions:0
  Method name is: tensorflow/serving/predict

目前我将csv转换为Json,用于预测:

{"sequence_length": 25, "batch_size": 1, "keep_prob": 1.0, "input_x": [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 10, 11, 12, 13, 14, 15, 1, 16, 12, 13, 14, 17, 18, 19, 20]}

例外:

 Exception during running the graph: Cannot feed value of shape (1,) for Tensor u\'keep_prob:0\', which has shape \'()\' (Error code: 2)\n'

【问题讨论】:

  • 你能运行saved_model_cli show --all --dir /path/to/model并提供输出吗?
  • @rhaertel80 添加了 saved_model_cli,查看更新问题。

标签: python tensorflow google-cloud-platform google-cloud-ml


【解决方案1】:

此模型似乎需要进行一些更改才能直接提供服务。该服务的一个要求是每个输入都有一个未指定的外部维度,该维度被解释为“批量”维度。输入 input_xsequence_length 满足此要求,但 batch_sizekeep_prob 不满足。

服务动态构建批次,这就是它需要可变长度的原因。因此,有一个名为batch_size 的输入将是有问题的,因为服务不知道它应该设置该输入。相反,它会构建一个批次并将其发送到 TensorFlow。 TensorFlow 已经知道批量大小,因为它是输入的外部维度的值,例如 input_x

与其使用batch_size 作为输入,不如执行以下操作:

batch_size = tf.shape(input_x)[0]

尽管我会注意到,即使在实践中通常也很少需要这样做。事情通常“正常工作”,因为input_x 用于某种运算,例如矩阵乘法或卷积,它可以在不明确知道批量大小的情况下很好地处理事情。

最后还有keep_prob,通常表示模型中有dropout层。即使您可以将其硬编码为 1.0,但通常建议您完全删除 dropout 层以进行服务。基本上,当您导出模型时,您实际上构建了一个不同的图表而不是用于训练。这在this sample中举例说明。

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2019-04-20
    • 2018-01-30
    • 2018-10-03
    • 2018-06-23
    • 2019-01-09
    • 2020-01-17
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多