Python 云ML模型预测
我在cloud ML中部署了tensorflow保存的模型,用于文本分类,如下所示:Python 云ML模型预测,python,tensorflow,google-cloud-platform,google-cloud-ml,Python,Tensorflow,Google Cloud Platform,Google Cloud Ml,我在cloud 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,
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文件--
仅获取错误。
如何为我训练的模型将其转换为json,以便在cloud ML中批量预测
已保存的\u模型\u 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_x
和sequence_length
符合此要求,但batch_size
和keep_prob
不符合此要求
该服务动态构建批处理,这就是为什么它需要可变长度。因此,拥有一个名为batch_size
的输入是有问题的,因为服务不知道应该设置该输入。相反,它构建一个批并将其发送给TensorFlow。TensorFlow已经知道批量大小,因为它是输入的外部维度值,例如input\ux
与其使用batch\u size
作为输入,不如执行以下操作:
batch_size = tf.shape(input_x)[0]
尽管我会注意到,在实践中,即使是这样的需求通常也是相当少的。事情通常是“正常的”,因为input_x
用于某种操作,比如矩阵乘法或卷积,它可以在不明确知道批量大小的情况下很好地处理事情
最后,还有
keep_prob
,它通常表示模型中有一个退出层。尽管您可以将其硬编码为1.0,但通常建议您只需删除退出层即可提供服务。基本上,导出模型时,实际上构建的图形与用于培训的图形不同。例如。此模型似乎需要一些更改才能直接维护。服务的一个要求是,每个输入都有一个未指定的外部维度,该维度被解释为“批处理”维度。输入input_x
和sequence_length
符合此要求,但batch_size
和keep_prob
不符合此要求
该服务动态构建批处理,这就是为什么它需要可变长度。因此,拥有一个名为batch_size
的输入是有问题的,因为服务不知道应该设置该输入。相反,它构建一个批并将其发送给TensorFlow。TensorFlow已经知道批量大小,因为它是输入的外部维度值,例如input\ux
与其使用batch\u size
作为输入,不如执行以下操作:
batch_size = tf.shape(input_x)[0]
尽管我会注意到,在实践中,即使是这样的需求通常也是相当少的。事情通常是“正常的”,因为input_x
用于某种操作,比如矩阵乘法或卷积,它可以在不明确知道批量大小的情况下很好地处理事情
最后,还有
keep_prob
,它通常表示模型中有一个退出层。尽管您可以将其硬编码为1.0,但通常建议您只需删除退出层即可提供服务。基本上,导出模型时,实际上构建的图形与用于培训的图形不同。例如。能否运行saved_model_cli show--all--dir/path/to/model
并提供输出?@rhaertel80添加了保存的_model_cli,检查更新问题。能否运行saved_model_cli show--all--dir path/to/model
并提供输出?@rhaertel80添加了保存的_model_cli,检查更新问题。