【发布时间】: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