简体   繁体   English

谷歌云引擎:输入实例不是JSON格式

[英]google cloud engine: Input instances are not in JSON format

I am using Google Cloud ML Engine to do online prediction. 我正在使用Google Cloud ML Engine进行在线预测。 I wrote Tensorflow Estimator API Code which I referred from the tf-estimator-tutorials repository. 我写了我从tf-estimator-tutorials存储库引用的Tensorflow Estimator API代码。 To do online prediction we need to export the model into proto buffer file ( .pb ) file. 为了进行在线预测,我们需要将模型导出到原型缓冲区文件( .pb )文件中。 To serve the input function to the model I wrote the following code in serve_input_fn() function. 为了将输入函数提供给模型,我在serve_input_fn()函数中编写了以下代码。

SERVING_HEADER = ['renancy','freq','monetary']
SERVING_HEADER_DEFAULTS = [[0.0],[0.0],[0.0]]

#shape=(?,), dtype=string
rows_string_tensor = tf.placeholder(dtype=tf.string,
                                    shape=[None],
                                    name="csv_rows")

#feeding rows_string_tensor value in the dictionary
receive_tensor = {'csv_rows':rows_string_tensor}

#shape=(?,1), dtype=string
row_columns = tf.expand_dims(rows_string_tensor, -1)

#<tf.Tensor 'DecodeCSV:0' shape=(?,1) dtype=float32>,<tf.Tensor 'DecodeCSV:1' shape=(?,1) dtype=float32>
#<tf.Tensor 'DecodeCSV:2' shape=(?,1) dtype=float32>
columns = tf.decode_csv(row_columns, record_defaults=SERVING_HEADER_DEFAULTS)

#<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32>
#<tf.Tensor 'Expand_dims_3:0' shape=(?,1,1) dtype=float32>
columns = [tf.expand_dims(tensor, -1) for tensor in columns]

#{"renancy":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,
#"freq":<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32> 
#"monetary":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>}
features = dict(zip(SERVING_HEADER, columns))


#InputFnOps(features=None, labels=None, default_inputs={'csv_rows':<tf.Tensor 'csv_rows:0' shape=(?,) dtype=string>})
return tf.contrib.learn.InputFnOps(
    process_features(features),
    None,
    receive_tensor
)

I have deployed the model in the cloud ML. 我已经在云ML中部署了该模型。 Now I have to do online prediction.To do so gcloud ml-engine predict --model-dir=<model_name> --version <version> --json-instances=test.json --project <project_name> 现在我必须进行在线预测gcloud ml-engine predict --model-dir=<model_name> --version <version> --json-instances=test.json --project <project_name>

When I run the above command, It shows the following error 当我运行上面的命令时,它显示以下错误

{ "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\\"NodeDef mentions attr 'select_cols' not in Op output:; attr=OUT_TYPE:list(type),min=1,allowed=[DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_STRING]; attr=field_delim:string,default=\\",\\"; attr=use_quote_delim:bool,default=true; attr=na_value:string,default=\\"\\">; NodeDef: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,1], [?,1], [?,1]], field_delim=\\",\\", na_value=\\"\\", select_cols=[], use_quote_delim=true, _device=\\"/job:localhost/replica:0/task:0/device:CPU:0\\"](ExpandDims, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_0). (Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary.).\\n\\t [[Node: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,1], [?,1], [?,1]], field_delim=\\",\\", na_value=\\"\\", select_cols=[], use_quote_de {“错误”:“预测失败:模型执行期间出错:AbortionError(code = StatusCode.INVALID_ARGUMENT,详细信息= \\“ NodeDef在Op输出中未提及attr'select_cols':; attr = OUT_TYPE:list(type),min = 1 ,allowed = [DT_FLOAT,DT_DOUBLE,DT_INT32,DT_INT64,DT_STRING]; attr = field_delim:string,default = \\“,\\”; attr = use_quote_delim:bool,default = true; attr = na_value:string,default = \\“ \\ “>; NodeDef:DecodeCSV = DecodeCSV [OUT_TYPE = [DT_FLOAT,DT_FLOAT,DT_FLOAT],_ output_shapes = [[?, 1],[?, 1],[?, 1]],field_delim = \\”,\\“,na_value = \\“ \\”,select_cols = [],use_quote_delim = true,_device = \\“ / job:localhost /副本:0 / task:0 / device:CPU:0 \\”]](ExpandDims,DecodeCSV / record_defaults_0,DecodeCSV / record_defaults_0 ,DecodeCSV / record_defaults_0)。(检查您的GraphDef解释二进制文件是否与生成GraphDef的二进制文件保持最新。)\\ n \\ t [[Node:DecodeCSV = DecodeCSV [OUT_TYPE = [DT_FLOAT,DT_FLOAT,DT_FLOAT],_output_shapes = [[?, 1],[?, 1],[?, 1]],field_delim = \\“,\\”,na_value = \\“ \\”,select_cols = [],use_quote_de lim=true, _device=\\"/job:localhost/replica:0/task:0/device:CPU:0\\"](ExpandDims, DecodeCSV/record_de...TRUNCATED\\")" } lim = true,_device = \\“ / job:localhost /副本:0 /任务:0 /设备:CPU:0 \\”](ExpandDims,DecodeCSV / record_de ... TRUNCATED \\“)”}

I know tf.contrib.learn.InputFnOps has been deprecated but still out of curiosity I wonder if there is any way to do prediction. 我知道tf.contrib.learn.InputFnOps已被弃用,但仍然出于好奇,我想知道是否有任何方法可以进行预测。 My test.json data look like this 我的test.json数据看起来像这样

       {"csv_rows":"7.0,8.0,7.0"}
       {"csv_rows":"5.0,6.0,4.0"}

I have trained the model using this data Train dataset . 我已经使用此数据训练数据集训练了模型。

Your test.json has to have exactly one instance per line. 您的test.json必须每行只有一个实例。 In your code, you are reading csv_rows as a string and decoding it as a CSV, so this is what your code is expecting in test.json: 在您的代码中,您将csv_rows作为字符串读取并将其解码为CSV,因此这是您的代码在test.json中的期望值:

{"csv_rows":"7.0,8.0,7.0"}
{"csv_rows":"5.0,6.0,4.0"}

If you want to be able to provide: 如果您希望能够提供:

{"renancy":"9.0","freq":"3.0","monetary":"5.0"}
{"renancy":"5.0","freq":"6.0","monetary":"4.0"}

Then, your serving code has to change to: 然后,您的服务代码必须更改为:

def serving_input_fn():
    feature_placeholders = {
        'renancy': tf.placeholder(tf.float32, [None]),
        'freq': tf.placeholder(tf.float32, [None]),
        'monetary': tf.placeholder(tf.float32, [None])
    }
    features = features_placeholders
    return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM