【问题标题】:Use tf.saved_model to predict multiple input vectors (tensorflow 2.0)使用 tf.saved_model 预测多个输入向量(tensorflow 2.0)
【发布时间】:2020-01-21 05:16:51
【问题描述】:

我训练了一个用于预测的估计器对象。但是你可能知道,estimator.predict 每次运行都会恢复参数,确实很慢。所以我跟着this guide 加快了速度。由于我使用的是 tensorflow 2.0,本指南中推荐的 tf.contrib.predictor API 不再可用,因此我求助于 saved_model API,即加载模型的 official way

以下是将估算器保存到 saved_model 的代码。 (我现在只有 5 个功能)

serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
  tf.feature_column.make_parse_example_spec([tf.feature_column.numeric_column(str(x)) for x in range(1,6)]))
my_estimator.export_saved_model('saved_model',serving_input_fn)

输出:

INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Signatures INCLUDED in export for Classify: None
INFO:tensorflow:Signatures INCLUDED in export for Regress: ['serving_default', 'regression']
INFO:tensorflow:Signatures INCLUDED in export for Predict: ['predict']
INFO:tensorflow:Signatures INCLUDED in export for Train: None
INFO:tensorflow:Signatures INCLUDED in export for Eval: None
INFO:tensorflow:Restoring parameters from ./output\model.ckpt-100000
INFO:tensorflow:Assets added to graph.
INFO:tensorflow:No assets to write.
INFO:tensorflow:SavedModel written to: saved_model\temp-b'1579582279'\saved_model.pb

official guide for predicting 之后,我调用了tf.Example 上使用我的输入数据构建的predict 签名:

example = tf.train.Example()
example.features.feature["1"].float_list.value.append(1) #note here the float_list.value can take multiple values
example.features.feature["2"].float_list.value.append(1)
example.features.feature["3"].float_list.value.append(1)
example.features.feature["4"].float_list.value.append(1)
example.features.feature["5"].float_list.value.append(1)

并使用

进行预测
my_model=tf.saved_model.load('saved_model/1579582279')
my_prediction=my_model.signatures["predict"](examples=tf.constant([example.SerializeToString()]))

虽然这很好用。当我使用每个功能的值列表构造tf.example 时。并尝试使用相同的代码进行预测

example = tf.train.Example()
example.features.feature["1"].float_list.value.extend([1,2])
example.features.feature["2"].float_list.value.extend([1,2])
example.features.feature["3"].float_list.value.extend([1,2])
example.features.feature["4"].float_list.value.extend([1,2])
example.features.feature["5"].float_list.value.extend([1,2])
my_prediction=my_model.signatures["predict"](examples=tf.constant([example.SerializeToString()]))

它给了我错误:

InvalidArgumentError:  Name: <unknown>, Key: 2, Index: 0.  Number of float values != expected.  Values size: 2 but output shape: [1]
     [[node ParseExample/ParseExample (defined at c:\users\i354164\appdata\local\programs\python\python36\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_pruned_2040]

Function call stack:
pruned

我的问题是:如何导出/加载 saved_model 以便它可以采用多个输入的tf.Example

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    加载模型后,您似乎需要调整位置参数的数量。

    how to input multi features for tensorflow model inference

    infer._num_positional_args = 2
    infer(tf.constant(x1), tf.constant(x2))
    

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 2020-03-27
      • 2016-12-07
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多