【问题标题】:How to create Tensorflow serving_input_receiver_fn with multiple features?如何创建具有多个功能的 Tensorflow serving_input_receiver_fn?
【发布时间】:2020-02-02 12:38:30
【问题描述】:

TF guide on saving models 中有一段关于 serving_input_receiver_fn 的段落讨论了预处理逻辑的实现功能。我正在尝试对 DNNRegressor 的输入数据进行一些规范化。他们的函数代码如下所示:

feature_spec = {'foo': tf.FixedLenFeature(...),
                'bar': tf.VarLenFeature(...)}

def serving_input_receiver_fn():
  """An input receiver that expects a serialized tf.Example."""
  serialized_tf_example = tf.placeholder(dtype=tf.string,
                                         shape=[default_batch_size],
                                         name='input_example_tensor')
  receiver_tensors = {'examples': serialized_tf_example}
  features = tf.parse_example(serialized_tf_example, feature_spec)
  return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

我的代码如下所示:

feat_cols = [
    tf.feature_column.numeric_column(key="FEATURE1"),
    tf.feature_column.numeric_column(key="FEATURE2")
]

def serving_input_receiver_fn():
    feature_spec = tf.feature_column.make_parse_example_spec(feat_cols)

    default_batch_size = 1

    serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[default_batch_size], name='tf_example')

    receiver_tensors = { 'examples': serialized_tf_example}

    features = tf.parse_example(serialized_tf_example, feature_spec)

    fn_norm1 = lamba FEATURE1: normalize_input_data('FEATURE1', FEATURE1)
    fn_norm2 = lamba FEATURE2: normalize_input_data('FEATURE2', FEATURE2)
    features['FEATURE1'] = tf.map_fn(fn_norm1, features['FEATURE1'], dtype=tf.float32)
    features['FEATURE2'] = tf.map_fn(fn_norm2, features['FEATURE2'], dtype=tf.float32)

    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

毕竟,保存的模型在图表中没有我的特征。如果您有多个要通过的功能,我正在尝试弄清楚这是如何工作的。

我使用 keras MPG 数据创建了一个示例。 It is located here:

【问题讨论】:

  • receiver_tensors 定义在哪里?
  • 抱歉,忘记那行了。 receiver_tensors = { 'examples': serialized_tf_example} 我会更新帖子
  • 曾几何时,我将receiver_tensors 用作定义了功能的字典。代码没有出错,但是当我去服务它时抛出一个错误,说我需要为 tf_example 提供一个值。
  • 您仍然没有提供太多上下文。发布MVCE 代码。
  • @SiyuanRen 我创建了一个基于 keras MPG 代码的示例,但它有同样的问题。在使用 TF Serving 的 Web API 时,我需要知道如何创建一个功能性 serving_input_receiver_fn 来规范化数据。

标签: tensorflow tensorflow-serving


【解决方案1】:

ServingInputReceiver 中的features 直接传递给您的模型函数。你要的是receive_tensors或者receive_tensor_alternatives,也就是ServingInputReceiver构造函数的第二个和第三个参数。

例如,你可以这样做

serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[default_batch_size], name='tf_example')

receiver_tensors = { 'examples': serialized_tf_example}

raw_features = tf.parse_example(serialized_tf_example, feature_spec)

fn_norm1 = lamba FEATURE1: normalize_input_data('FEATURE1', FEATURE1)
fn_norm2 = lamba FEATURE2: normalize_input_data('FEATURE2', FEATURE2)
features['FEATURE1'] = tf.map_fn(fn_norm1, raw_features['FEATURE1'], dtype=tf.float32)
features['FEATURE2'] = tf.map_fn(fn_norm2, raw_features['FEATURE2'], dtype=tf.float32)
return tf.estimator.export.ServingInputReceiver(
      features=features,
      receiver_tensors=receiver_tensors,
      receiver_tensors_alternatives={'SOME_KEY': raw_features})

如果您不需要向网络提供 Example 原型,您可以完全跳过它。

raw_features = {'FEATURE1': tf.placeholder(...), 'FEATURE2': tf.placeholder(...)}
features = preprepocess(raw_features)
return tf.estimator.export.ServingInputReceiver(features, {'SOME_OTHER_KEY': raw_features})

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多