占位符张量需要 ml 引擎预测中的值,但不需要局部预测



我一直在开发一个模型,用于云ML引擎的在线预测服务。我的模型包含一个placeholder_with_default张量,我用它来保持预测显著性的阈值。

threshold = tf.placeholder_with_default(0.01, shape=(), name="threshold")

我注意到在使用本地预测时:

gcloud ml-engine local predict --json-instances=data.json --model-dir=/my/model/dir

我不需要为这个张量提供值,例如,这是一个有效的输入:

{"features": ["a", "b"], "values": [10, 5]}

但是,使用在线预测时:

gcloud ml-engine predict --model my_model --version v1 --json-instances data.json

如果我使用上面的 JSON,我会收到一个错误:

{
    "error": "Prediction failed: Exception during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input size does not match signature")"
}

但是,如果我包括阈值,那么我不会。 例如:

{"features": ["a", "b"], "values": [10, 5], "threshold": 0.01}

有没有办法让"阈值"成为可选输入?

谢谢

马修

看起来目前在 CloudML 中是不可能的。如果要从 JSON 文件获取预测,则需要显式添加默认值(就像对 "threshold": 0.01 所做的那样(。

在 Python 中,我只是在执行 API 请求之前动态添加所需的属性:

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字典。对于具有许多可选占位符的模型,这比为每个实例手动设置缺少的占位符值要好一些。

相关内容

最新更新