python - TensorFlow ExportOutputs、PredictOuput 并指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY

标签 python tensorflow tensorflow-serving tensorflow-estimator

上下文

我有一个 colab用一个非常简单的演示 Estimator为了学习/理解 Estimator API 的目标是为即插即用模型制定一个约定,其中包含有用的交易技巧(例如,如果验证集停止改进、导出模型等,则提前停止)。

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

根据docs :

__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 中进行了描述作为:

  • 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 加载它)。

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

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

  • “多头”模型是指有多个输出的模型

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

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

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

对,所以在 colab你看到我们的模型的 export_outputs 实际上是以多头的方式定义的(虽然它不应该是):

来自 colabestimator functions > model_fn :

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()
    }

    # ...

在这个特定的例子中,如果我们扩展字典理解,我们有等价的功能:

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

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

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

PredictOutput 中所述:

__init__(outputs)

在哪里

  • outputs:表示预测的 Tensor 或字符串字典。

问题

因此我的问题如下:

  1. 如果 PredictOutput可以是一本字典,什么时候/为什么需要多个 PredictOutput s 作为 EstimatorSpecexport_outputs ?

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

  3. predictionsEstimatorSpec 中有什么意义?当它在 export_outputs 中也是“必需的”(对于任何关心使用 SavedModel 的人)时?

最佳答案

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

  1. 还有 RegressionOutput 和 ClassificationOutput 类不能是字典。使用 export_outputs 字典可以对这些用例进行概括。

  2. 您希望从保存的模型中默认提供服务的头部应该采用默认签名 key 。例如:

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:历史。预测是第一位的,随着时间的推移,越来越多的功能被添加进来。这些功能需要灵 active 和额外信息,因此被独立打包到 EstimatorSpec 中。

关于python - TensorFlow ExportOutputs、PredictOuput 并指定 signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53414168/

相关文章:

java - 中级Java/Python开发人员的项目构想

python - 如何在 Tensorflow 神经网络中使用自定义矩阵乘法处理无值错误

tensorflow - 如何转换在 Edge TPU 板的自定义数据集上训练的模型?

tensorflow - 如何使用cuda8.0安装TensorFlow-gpu?

database - 如何从 png 图像创建 TFRecords 文件

python - SPA (Ember) 身份验证与后端

python - tf.Variable 具有来自输入占位符的动态形状

python - "match anything until a specific character, then work your way backwards"怎么说?

tensorflow - 关于 tf.stack() 轴的问题

docker - gRPC 服务器响应操作系统错误,grpc_status : 14