本文介绍了TensorFlow Estimator ServingInputReceiver 功能与receiver_tensors:何时以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前问题探索了serving_input_receiver_fn的目的和结构,并在答案中:

In a previous question the purpose and structure of the serving_input_receiver_fn is explored and in the 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)

答案的作者声明(关于receiver_tensors):

据我所知,这是将输入映射到稍后可以检索的名称所必需的

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

This distinction is unclear to me. In practice, (see this colab), the same dictionary can be passed to both features and receiver_tensors.

来自源代码@estimator_export('estimator.export.ServingInputReceiver')(或 ServingInputReceiver 文档:

From the source code of @estimator_export('estimator.export.ServingInputReceiver') (or the ServingInputReceiver docs:

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

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

After reading, it is clear to me what the purposes of features is. features is a dictionary of inputs that I then send through the graph. Many common models have just a single input, but you can or course have more.

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

So then the statement regarding receiver_tensors which "Typically, this is a single placeholder expecting serialized tf.Example protos.", to me, suggests that receiver_tensors want a singular batched placeholder for (Sequence)Examples parsed from TF Records.

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

Why? If the TF Records is fully preprocessed, then this is redundant? if it is not fully pre-processed, why would one pass it? Should the keys in the features and receiver_tensors dictionaries be the same?

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

Can someone please provide me with a more concrete example of the difference and what goes where, as right now

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

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

works... (even if maybe it shouldn't...)

推荐答案

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

As far as I understand, SWAPNIL's answer is correct.I would share a example of mine.

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

Suppose the input of graph is a placeholder of shape [None, 64]

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

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

But what we get from upstream are arrays of 32 float numbers and we will need to process them into shape [None, 64], for example, simple repeat them.

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]所以函数是

Of course we can do this process outside, and feed the estimator data just as we define the inputs of graph. In this case, we concatenate the inputs in the upstream process, and the raw input would be of shape [None, 64]So the function would be

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)

这篇关于TensorFlow Estimator ServingInputReceiver 功能与receiver_tensors:何时以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:02