TensorFlow ExportOutputs、PredictOuput 和指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY [英] TensorFlow ExportOutputs, PredictOuput, and specifying signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY

查看:22
本文介绍了TensorFlow ExportOutputs、PredictOuput 和指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 colab 和一个非常简单的演示 Estimator 用于学习/理解 Estimator API 旨在为即插即用模型,具有有用的交易技巧(例如,如果验证集停止改进,则提前停止,导出模型等).

I have a colab with a very simple demo Estimator for the purpose of learning / understanding the Estimator API with the goal of making a convention for a plug-and-play model with useful bells and whistles of the trade in tack (e.g. early stopping if the validation set stops improving, exporting the model, etc).

三个 Estimator 模式(TRAINEVALPREDICT)返回一个 EstimatorSpec.

Each of the three Estimator modes (TRAIN, EVAL, and PREDICT) return an EstimatorSpec.

根据文档:

__new__(
    cls,
    mode,
    predictions=None,          # required by PREDICT
    loss=None,                 # required by TRAIN and EVAL
    train_op=None,             # required by TRAIN
    eval_metric_ops=None,
    export_outputs=None,
    training_chief_hooks=None,
    training_hooks=None,
    scaffold=None,
    evaluation_hooks=None,
    prediction_hooks=None.     
)

在这些命名参数中,我想提请注意 predictionsexport_outputs,它们在 docs 为:

Of these named arguments I would like to bring attention to predictions and export_outputs, which are described in the docs as:

  • predictions:预测张量或张量字典.
  • export_outputs:描述要导出到 SavedModel 并在服务期间使用的输出签名.一个 dict {name: output} 其中:
    • name:此输出的任意名称.
    • output:一个ExportOutput 对象,例如 ClassificationOutputRegressionOutputPredictOutput.单头模型只需要在这个字典中指定一个条目.多头模型应为每个头指定一个条目,其中之一必须使用 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY 命名.如果未提供条目,则将创建一个默认的 PredictOutput 映射到预测.
    • predictions: Predictions Tensor or dict of Tensor.
    • export_outputs: Describes the output signatures to be exported to SavedModel and used during serving. A dict {name: output} where:
      • name: An arbitrary name for this output.
      • output: an ExportOutput object such as ClassificationOutput, RegressionOutput, or PredictOutput. Single-headed models only need to specify one entry in this dictionary. Multi-headed models should specify one entry for each head, one of which must be named using signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY. If no entry is provided, a default PredictOutput mapping to predictions will be created.

    因此应该很清楚为什么我提出了export_outputs;也就是说,因为人们最有可能在未来使用他们训练的模型(通过从 SavedModel 加载它).

    Thus it should be clear why I bring up export_outputs; namely, as one would most likely like to use the model they trained in the future (by loading it from a SavedModel).

    为了让这个问题更容易理解/增加一些清晰度:

    To make this question a bit more accessible / add some clarity:

    • 单头"模型是人们遇到的最常见的模型,其中 input_fn features 被转换为单个(批处理)output

    • "single-headed" models are the most common model one encounters where the input_fn features are transformed to a singular (batched) output

    多头"模型是具有多个输出的模型

    "multi-headed" models are models where there is more than one output

    例如这个多头模型的input_fn(根据Estimator api) 返回一个元组 (features, labels) 即这个模型有两个头.

    e.g. this multi-headed model's input_fn (in accordance with the Estimator api) returns a tuple (features, labels) i.e. this model has two heads).

    def input_fn():
      features = ...
      labels1 = ...
      labels2 = ...
      return features, {'head1': labels1, 'head2': labels2}
    

    如何指定signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY是这个问题的核心.即,如何指定它?(例如,它应该是一个 dict {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: head})

    How one specifies the signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY is the core of this question. Namely, how does one specify it? (e.g. should it be a dict {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: head})

    是的,所以在 colab 中,您会看到我们模型的 export_outputs 实际上是以多头方式定义的(虽然不应该如此):

    Right, so in the colab you see that our model's export_outputs is actually defined in a multi-head manner (although it shouldn't be):

    来自 estimator 函数 >colab 的模型_fn:

    From estimator functions > model_fn of the colab:

    def model_fn(...):
    
        # ...
    
        # send the features through the graph
        MODEL = build_fn(MODEL)
    
        # prediction
        MODEL['predictions'] = {'labels': MODEL['net_logits']} # <--- net_logits added in the build_fn
    
        MODEL['export_outputs'] = {
            k: tf.estimator.export.PredictOutput(v) for k, v in MODEL['predictions'].items()
        }
    
        # ...
    

    在这个特定的例子中,如果我们扩展字典理解,我们的功能相当于:

    in this particular instance, if we expand the dictionary comprehension, we have the functional equivalent of:

    MODEL['export_outputs'] = {
        'labels': tf.estimator.export.PredictOutput(MODEL['net_logits'])
    }
    

    它在这种情况下有效,因为我们的字典有一个键,因此有一个 PredictOutput,其中在 colab我们的 model_fn 只有一个头部,格式更合适:

    It works in this instance as our dictionary has one key and hence one PredictOutput, where in the colab our model_fn has only a single head and would be more properly formatted as:

    MODEL['export_outputs'] = {
        'predictions': tf.estimator.export.PredictOutput(MODEL['predictions'])
    }
    

    正如 PredictOutput:

    __init__(outputs)
    

    哪里

    • outputs:一个 Tensor 或一个表示预测的 Tensor 字符串的字典.
    • outputs: A Tensor or a dict of string to Tensor representing the predictions.

    因此我的问题如下:

    1. 如果 PredictOutput 可以是一本字典,当/为什么要多个 PredictOutputs 作为 export_outputs="nofollow noreferrer">EstimatorSpec?

    1. if PredictOutput can be a dictionary, when / why would one want multiple PredictOutputs as their export_outputs for the EstimatorSpec?

    如果有一个多头模型,(比如有多个 PredictOutputs) 如何实际指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY

    If one has a multi-headed model, (say with multiple PredictOutputs) how does one actually specify the signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY

    EstimatorSpec 当它在 export_outputs 中也是必需的"(对于关心使用 SavedModels 的任何人)时?

    what is the point of predictions in the EstimatorSpec when it is also "required" (for anyone who cares about using SavedModels) in export_outputs?

    推荐答案

    感谢您的详细提问;你显然在这里挖得很深.

    Thanks for your detailed question; you have clearly dug deep here.

    1. 还有一些不能作为字典的 RegressionOutput 和 ClassificationOutput 类.使用 export_outputs dict 允许对这些用例进行泛化.

    1. There are also classes for RegressionOutput and ClassificationOutput which cannot be dictionaries. The use of an export_outputs dict allows for generalizations over those use cases.

    您希望从保存的模型中默认使用的头部应采用默认签名密钥.例如:

    The head you want to be served by default from the saved model should take the default signature key. For example:

    export_outputs = {
      signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
        PredictOutput(outputs={'some_output_1': output_1}),
      'head-2': PredictOutput(outputs={'some_output_2': output_2}),
      'head-3': PredictOutput(outputs={'some_output_3': output_3})
    }
    

    1. 原因 1:很多人使用默认的 export_outputs(这又是预测的值),或者不导出到保存的模型.原因2:历史.预测是第一位的,随着时间的推移,越来越多的功能被添加进来.这些功能需要灵活性和额外信息,因此被独立打包到 EstimatorSpec 中.

    这篇关于TensorFlow ExportOutputs、PredictOuput 和指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆