问题描述
我一直在开发用于Cloud ML引擎的在线预测服务的模型.我的模型包含一个placeholder_with_default
张量,用于保持预测显着性的阈值.
I've been developing a model for use with the cloud ML engine's online prediction service. My model contains a placeholder_with_default
tensor that I use to hold a threshold for prediction significance.
threshold = tf.placeholder_with_default(0.01, shape=(), name="threshold")
我注意到在使用局部预测时:
I've noticed that when using local predict:
gcloud ml-engine local predict --json-instances=data.json --model-dir=/my/model/dir
我不需要为此张量提供值.例如这是有效的输入:
I don't need to supply values for this tensor. e.g. this is a valid input:
{"features": ["a", "b"], "values": [10, 5]}
但是,当使用在线预测时:
However when using online predict:
gcloud ml-engine predict --model my_model --version v1 --json-instances data.json
如果我使用上述JSON,则会收到错误消息:
If I use the above JSON I get an error:
{
"error": "Prediction failed: Exception during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"input size does not match signature\")"
}
但是,如果我包含阈值,则不会.例如:
However if I include threshold, then I don't. e.g:
{"features": ["a", "b"], "values": [10, 5], "threshold": 0.01}
是否可以将阈值"作为可选输入?
Is there a way to have "threshold" be an optional input?
谢谢
马修
推荐答案
看起来目前在CloudML中还无法实现.如果要从JSON文件中获取预测,则需要显式添加默认值(就像使用"threshold": 0.01
一样).
Looks like currently it's not possible in CloudML. If you're getting predictions from a JSON file, you need to add the default values explicitly (like you did with "threshold": 0.01
).
在Python中,我只是在执行API请求之前动态添加所需的属性:
In Python I'm just dynamically adding the required attributes before doing the API request:
def add_empty_fields(instance):
placeholder_defaults = {"str_placeholder": "", "float_placeholder": -1.0}
for ph, default_val in placeholder_defaults.items():
if ph not in instance:
instance[ph] = default_val
会更改将占位符名称映射到占位符值的instance
字典.对于具有许多可选占位符的模型,这比为每个实例手动设置缺少的占位符值要好一些.
which would mutate the instance
dict that maps placeholder names to placeholder values. For a model with many optional placeholders, this is a bit nicer than manually setting missing placeholder values for each instance.
这篇关于占位符张量需要ml-engine预测中的值,而不是局部预测中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!