簡體   English   中英

雲ML模型預測

[英]Cloud ML model prediction

我已經在雲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'

該模型似乎需要進行一些更改才能直接使用。 服務的一項要求是,每個輸入都具有未指定的外部尺寸,該尺寸被解釋為“批”尺寸。 輸入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 ,它通常指示模型中存在一個退出層。 即使您可以將此代碼硬編碼為1.0,通常還是建議您完全刪除掉落層以進行投放。 基本上,當您導出模型時,您實際上會構建訓練不同的圖。 此示例中對此進行了舉例說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM