TensorFlow Estimator ServingInputReceiver功能与receiver_tensors:何时以及为何?

问题描述 投票:7回答:1

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

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

  • 特征:TensorSparseTensor,或TensorSparseTensor的字符串,指定要传递给模型的特征。注意:如果features传递的不是dict,它将被包含在带有单个条目的dict中,使用“feature”作为键。因此,模型必须接受{'feature':tensor}形式的特征字典。如果你想让张量按原样传递,你可以使用TensorServingInputReceiver
  • receiver_tensors:TensorSparseTensor或字符串到TensorSparseTensor的字典,指定此接收器预期默认馈送的输入节点。通常,这是一个单独占位符,期望序列化的tf.Example原型。

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

所以关于receiver_tensors的声明“通常,这是一个单独的占位符,期望序列化的tf.Example原型。”对我来说,表明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个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.