【问题标题】:TensorFlow Estimator ServingInputReceiver features vs receiver_tensors: when and why?TensorFlow Estimator ServingInputReceiver 特性与receiver_tensors:何时以及为什么?
【发布时间】:2019-04-23 22:16:40
【问题描述】:

previous question 中探讨了serving_input_receiver_fn 的用途和结构,在answer 中:

def serving_input_receiver_fn():
  """For the sake of the example, let's assume your input to the network will be a 28x28 grayscale image that you'll then preprocess as needed"""
  input_images = tf.placeholder(dtype=tf.uint8,
                                         shape=[None, 28, 28, 1],
                                         name='input_images')
  # here you do all the operations you need on the images before they can be fed to the net (e.g., normalizing, reshaping, etc). Let's assume "images" is the resulting tensor.

  features = {'input_data' : images} # this is the dict that is then passed as "features" parameter to your model_fn
  receiver_tensors = {'input_data': input_images} # As far as I understand this is needed to map the input to a name you can retrieve later
  return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

answer 的作者声明(关于receiver_tensors):

据我了解,这需要将输入映射到您以后可以检索的名称

我不清楚这种区别。在实践中,(参见colab),同一个字典可以同时传递给featuresreceiver_tensors

来自@estimator_export('estimator.export.ServingInputReceiver')source code(或ServingInputReceiver docs

  • 功能TensorSparseTensor 或字符串字典到 TensorSparseTensor,指定要传递给模型的特征。笔记: 如果features 传递的不是字典,它将被包裹在字典中 单个条目,使用“功能”作为键。因此,模型必须 接受 {'feature': tensor} 形式的特征字典。您可以使用 TensorServingInputReceiver 如果您希望张量按原样传递。
  • receiver_tensorsTensorSparseTensorTensor 的字符串字典 或SparseTensor,指定此接收器期望的输入节点 默认喂食。通常,这是一个预期的占位符 序列化tf.Example protos。

阅读后,我很清楚features 的目的是什么。 features 是一个输入字典,然后我通过图表发送。许多常见模型只有一个输入,但您可以或当然有更多。

那么关于receiver_tensors 的声明“通常,这是一个期望序列化tf.Example protos 的单个占位符。”对我来说,这表明receiver_tensors 需要一个从TF 解析的(Sequence)Examples 的单个批处理占位符Records。

为什么?如果 TF Records 被完全预处理,那么这是多余的吗?如果它没有完全预处理,为什么会通过呢? featuresreceiver_tensors 字典中的键是否应该相同?

谁能提供一个更具体的例子来说明差异以及现在的情况

input_tensors = tf.placeholder(tf.float32, <shape>, name="input_tensors")
features = receiver_tensors =  {'input_tensors': input_tensors}

有效...(即使它不应该...)

【问题讨论】:

    标签: python tensorflow tensorflow-estimator


    【解决方案1】:

    据我了解,SWAPNIL 的回答是正确的。 我会分享一个我的例子。

    假设graph的输入是一个形状为[None, 64]的占位符

    inputs = tf.placeholder(dtype=tf.float32, shape=[None, 64])
    prediction = ... # do some prediction
    

    但是我们从上游得到的是 32 个浮点数的数组,我们需要将它们处理成形状 [None, 64],例如,简单地重复它们。

    def serving_fn():
        inputs = tf.placeholder(dtype=tf.float32, shape=[None, 32])  # this is raw input
        features = tf.concat([inputs, inputs], axis=1)  # this is how we get model input from raw input
        return tf.estimator.export.TensorServingInputReceiver(features, inputs)
    

    当然,我们可以在外部执行此过程,并像定义图的输入一样提供估计器数据。在这种情况下,我们将上游流程中的输入连接起来,原始输入的形状为 [None, 64] 所以函数是

    def serving_fn():
        inputs = tf.placeholder(dtype=tf.float32, shape=[None, 64])  # this is raw input
        features = inputs  # we simply feed the raw input to estimator
        return tf.estimator.export.TensorServingInputReceiver(features, inputs)
    

    【讨论】:

      【解决方案2】:

      服务输入函数的工作是将接收到的原始特征转换为模型函数接受的处理后的特征

      receiver_tensors :这些是输入占位符。这将在您的图表中打开,您将在其中收到原始输入特征。

      定义此占位符后,您对这些接收器张量执行转换,以将它们转换为模型可接受的特征。其中一些转换将包括:

      • 预处理收到的数据。
      • 从 tfrecord 解析示例。 (如果您提供 tfrecord 作为服务函数的输入)

      features :一旦你转换接收张量,就会获得在预测期间直接馈送到模型函数的特征。

      在您的情况下,您提供给服务输入函数的数据不需要进行预处理。因此features = receiver_tensors 正在工作。

      【讨论】:

        【解决方案3】:

        如果您在 TensorServingInputReceiver 中进行预处理,而不是在 receiver_tensors 中进行预处理,则功能会有所不同。在 TensorServingInputReceiver 内部进行预处理后,特征将被传递给模型。 receiver_tensors 是 TensorServingInputReceiver 的输入,它们可以是 tf.Example 格式

        【讨论】:

          猜你喜欢
          • 2018-09-13
          • 2023-03-28
          • 2018-03-12
          • 2012-09-27
          • 1970-01-01
          • 2020-02-10
          • 1970-01-01
          • 2019-12-30
          • 1970-01-01
          相关资源
          最近更新 更多