Google 具有自定义响应结构的 Cloud ML 预测 API
Google Cloud ML Prediction API with Custom Response Structure
我目前正在探索使用 Google Cloud ML 来托管模型并使用该模型托管预测端点。我仍然不明白的一件事是预测 API 本身的响应格式:
从 documentation here 中写道,来自 API 的响应将采用 predictions[]
的形式,其中包含具有 label
和 scores
的对象场地。我的问题:
是否可以自定义 predictions[]
中对象的结构?例如,如果对于给定的 instance/data,我希望将 API 预测为 return 数字列表或其他可能的结构怎么办?
如果可能,我应该怎么做(例如,更改我的 TensorFlow 代码?配置文件?)?
到现在为止,我还没有清楚地了解预测 API 会如何根据我的 TensorFlow 模型得到它会给出的响应形式。
谢谢。
绝对支持定义自己的输出。典型的 TensorFlow 培训计划将:
- 构建训练图
- 使用该图训练模型
- 构建预测图
- 导出保存的模型
这是例证,例如,在sample code。
构建预测图时,您将为输入创建占位符,例如:
with tf.Graph() as prediction_graph:
# dtypes can be anything
# First dimension of shape is "batch size" which must be None
# so the system can send variable-length batches. Beyond that,
# there are no other restrictions on shape.
x = tf.placeholder(dtype=tf.int32, shape=(None,))
y = tf.placeholder(dtype=tf.float32, shape=(None,))
z = build_prediction_graph(x, y)
saver = tf.train.Saver()
导出 SavedModel 时,您在所谓的 "Signature" 中声明输入和输出;在此过程中,您给它们起友好的名称(字典中的键),因为 TensorFlow 会进行名称修饰。这些键是您在发送数据时在 JSON 中使用的键,它们是您在预测中返回的 JSON 中的键。
例如:
# Define the inputs and the outputs.
inputs = {"x": tf.saved_model.utils.build_tensor_info(x),
"y": tf.saved_model.utils.build_tensor_info(y)}
outputs = {"z": tf.saved_model.utils.build_tensor_info(z)}
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)
对使用该签名的服务的假设请求可能如下所示:
{"instances": [{"x": 6, "y": 3.14}, {"x": 3, "y": 1.0}]}
假设响应如下:
{"predictions": [{"z": [1, 2, 3]}, {"z": [4, 5, 6]}]}
最后,您需要实际保存 SavedModel
:
with tf.Session(graph=prediction_graph) as session:
# Restore the most recently checkpointed variables from training
saver.restore(session, tf.train.latest_checkpoint(job_dir))
# Save the SavedModel
b = builder.SavedModelBuilder('/path/to/export')
b.add_meta_graph_and_variables(session, ['serving_default'], signature)
b.save()
我目前正在探索使用 Google Cloud ML 来托管模型并使用该模型托管预测端点。我仍然不明白的一件事是预测 API 本身的响应格式:
从 documentation here 中写道,来自 API 的响应将采用 predictions[]
的形式,其中包含具有 label
和 scores
的对象场地。我的问题:
是否可以自定义
predictions[]
中对象的结构?例如,如果对于给定的 instance/data,我希望将 API 预测为 return 数字列表或其他可能的结构怎么办?如果可能,我应该怎么做(例如,更改我的 TensorFlow 代码?配置文件?)?
到现在为止,我还没有清楚地了解预测 API 会如何根据我的 TensorFlow 模型得到它会给出的响应形式。
谢谢。
绝对支持定义自己的输出。典型的 TensorFlow 培训计划将:
- 构建训练图
- 使用该图训练模型
- 构建预测图
- 导出保存的模型
这是例证,例如,在sample code。
构建预测图时,您将为输入创建占位符,例如:
with tf.Graph() as prediction_graph:
# dtypes can be anything
# First dimension of shape is "batch size" which must be None
# so the system can send variable-length batches. Beyond that,
# there are no other restrictions on shape.
x = tf.placeholder(dtype=tf.int32, shape=(None,))
y = tf.placeholder(dtype=tf.float32, shape=(None,))
z = build_prediction_graph(x, y)
saver = tf.train.Saver()
导出 SavedModel 时,您在所谓的 "Signature" 中声明输入和输出;在此过程中,您给它们起友好的名称(字典中的键),因为 TensorFlow 会进行名称修饰。这些键是您在发送数据时在 JSON 中使用的键,它们是您在预测中返回的 JSON 中的键。
例如:
# Define the inputs and the outputs.
inputs = {"x": tf.saved_model.utils.build_tensor_info(x),
"y": tf.saved_model.utils.build_tensor_info(y)}
outputs = {"z": tf.saved_model.utils.build_tensor_info(z)}
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)
对使用该签名的服务的假设请求可能如下所示:
{"instances": [{"x": 6, "y": 3.14}, {"x": 3, "y": 1.0}]}
假设响应如下:
{"predictions": [{"z": [1, 2, 3]}, {"z": [4, 5, 6]}]}
最后,您需要实际保存 SavedModel
:
with tf.Session(graph=prediction_graph) as session:
# Restore the most recently checkpointed variables from training
saver.restore(session, tf.train.latest_checkpoint(job_dir))
# Save the SavedModel
b = builder.SavedModelBuilder('/path/to/export')
b.add_meta_graph_and_variables(session, ['serving_default'], signature)
b.save()