目录
B. 没用到tf.feature_column接口,需要自己定义feature_spec
版本:tensorflow 1.14
保存形式:svaedModel
1. Estimator形式
模型保存后,预测数据以example格式输入
1.1 模型导出
A. 用到了tf.feature_column接口
# feature_columns
feature_columns = [
tf.feature_column.numeric_column('dense_features', (10,)),
...
]
serving_feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)
serving_input_receiver_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(serving_feature_spec)
model_estimator.export_saved_model(export_dir_base=save_model_dir,
serving_input_receiver_fn=serving_input_receiver_fn)
- 第4行 make_parse_example_spec() 会根据创建的feature column列表,构建出解析tf.Example所需要的信息,比如:
{
'feature_1': VarLenFeature(dtype=tf.int64),
'feature_2': VarLenFeature(dtype=tf.int64),
'dense_features': FixedLenFeature(shape=(10,), dtype=tf.float32, default_value=None)
}
- 第5行 build_parsing_serving_input_receiver_fn函数 注册接收key为examples的string tensor作为input,依据feature_spec解析后给到模型,源码如下:
- 本质是:将来模型的输入是一个example形式的序列化string,对其按照feature_spec解析成一个个字段,形成features
def build_parsing_serving_input_receiver_fn(feature_spec, default_batch_size=None):
def serving_input_receiver_fn():
"""An input_fn that expects a serialized tf.Example."""
serialized_tf_example = array_ops.placeholder(dtype=dtypes.string,
shape=[default_batch_size],
name='input_example_tensor')
receiver_tensors = {'examples': serialized_tf_example}
features = parsing_ops.parse_example(serialized_tf_example, feature_spec)
return ServingInputReceiver(feature