【发布时间】:2020-03-04 11:04:15
【问题描述】:
我对 tensorflow 非常陌生,尤其是 2.0,因为没有足够的关于该 API 的示例,但它似乎比 1.x 方便得多 到目前为止,我设法使用 tf.estimator api 训练了一个线性模型,然后设法使用 tf.estimator.exporter 保存它。
之后我想使用 tf.saved_model api 加载这个模型,我想我成功了,但我对我的过程有些怀疑,所以这里快速浏览一下我的代码:
所以我有一个使用 tf.feature_column api 创建的功能数组,它看起来像这样:
feature_columns =
[NumericColumn(key='geoaccuracy', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
NumericColumn(key='longitude', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
NumericColumn(key='latitude', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
NumericColumn(key='bidfloor', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
VocabularyListCategoricalColumn(key='adid', vocabulary_list=('115', '124', '139', '122', '121', '146', '113', '103', '123', '104', '147', '114', '149', '148'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
VocabularyListCategoricalColumn(key='campaignid', vocabulary_list=('36', '31', '33', '28'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
VocabularyListCategoricalColumn(key='exchangeid', vocabulary_list=('1241', '823', '1240', '1238'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
...]
之后,我以这种方式使用我的特征列数组定义一个估计器,并对其进行训练。到这里,没问题。
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
在训练我的模型后,我想保存它,所以这里开始怀疑,这是我如何进行但不确定这是正确的方法:
serving_input_parse = tf.feature_column.make_parse_example_spec(feature_columns=feature_columns)
""" view of the variable : serving_input_parse =
{'adid': VarLenFeature(dtype=tf.string),
'at': VarLenFeature(dtype=tf.string),
'basegenres': VarLenFeature(dtype=tf.string),
'bestkw': VarLenFeature(dtype=tf.string),
'besttopic': VarLenFeature(dtype=tf.string),
'bidfloor': FixedLenFeature(shape=(1,), dtype=tf.float32, default_value=None),
'browserid': VarLenFeature(dtype=tf.string),
'browserlanguage': VarLenFeature(dtype=tf.string)
...} """
# exporting the model :
linear_est.export_saved_model(export_dir_base='./saved',
serving_input_receiver_fn=tf.estimator.export.build_parsing_serving_input_receiver_fn(serving_input_receiver_fn),
as_text=True)
现在我尝试加载它,但我不知道如何使用加载的模型来调用预测,例如使用来自 pandas 数据帧的原始数据
loaded = tf.saved_model.load('saved/1573144361/')
还有一件事,我试图查看模型的签名,但我无法真正理解我的输入形状发生了什么
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['classification']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 2)
name: head/Tile:0
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 2)
name: head/predictions/probabilities:0
Method name is: tensorflow/serving/classify
signature_def['predict']:
The given SavedModel SignatureDef contains the following input(s):
inputs['examples'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['all_class_ids'] tensor_info:
dtype: DT_INT32
shape: (-1, 2)
name: head/predictions/Tile:0
outputs['all_classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 2)
name: head/predictions/Tile_1:0
outputs['class_ids'] tensor_info:
dtype: DT_INT64
shape: (-1, 1)
name: head/predictions/ExpandDims:0
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 1)
name: head/predictions/str_classes:0
outputs['logistic'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 1)
name: head/predictions/logistic:0
outputs['logits'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 1)
name: linear/linear_model/linear/linear_model/linear/linear_model/weighted_sum:0
outputs['probabilities'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 2)
name: head/predictions/probabilities:0
Method name is: tensorflow/serving/predict
signature_def['regression']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['outputs'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 1)
name: head/predictions/logistic:0
Method name is: tensorflow/serving/regress
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 2)
name: head/Tile:0
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 2)
name: head/predictions/probabilities:0
Method name is: tensorflow/serving/classify
【问题讨论】:
-
检查此处提供的解决方案stackoverflow.com/questions/58959582/…
标签: tensorflow model save load predict