jjzjj

python - TensorFlow Estimator ServingInputReceiver 功能与 receiver_tensors : when and why?

coder 2023-08-15 原文

previous questionserving_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):

As far as I understand this is needed to map the input to a name you can retrieve later

我不清楚这种区别。实际上,(参见 colab),可以将同一个字典传递给 featuresreceiver_tensors

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

  • features: A Tensor, SparseTensor, or dict of string to Tensor or SparseTensor, specifying the features to be passed to the model. Note: if features passed is not a dict, it will be wrapped in a dict with a single entry, using 'feature' as the key. Consequently, the model must accept a feature dict of the form {'feature': tensor}. You may use TensorServingInputReceiver if you want the tensor to be passed as is.
  • receiver_tensors: A Tensor, SparseTensor, or dict of string to Tensor or SparseTensor, specifying input nodes where this receiver expects to be fed by default. Typically, this is a single placeholder expecting serialized tf.Example protos.

看完之后,我清楚了features的用途是什么。 features 是我随后通过图形发送的输入字典。许多常见模型只有一个输入,但您可以或当然有更多。

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

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

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

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

有效...(即使可能不应该...)

最佳答案

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

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

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

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

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

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

关于python - TensorFlow Estimator ServingInputReceiver 功能与 receiver_tensors : when and why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53410469/

有关python - TensorFlow Estimator ServingInputReceiver 功能与 receiver_tensors : when and why?的更多相关文章

随机推荐