在tensorflow中使用export_savedmodel的正确方法是什么?

时间:2018-06-12 13:58:15

标签: python tensorflow tensorflow-serving tensorflow-estimator

我正在使用Tensorflow 1.8。

我创建了一个自定义的tf.estimator my_estimator,在训练之后我想导出我的模型,以便在预测后使用它。为此,我尝试了(feature_placeholders是我模型的输入):

feature_placeholders = {
        "Numerical_features": tf.placeholder(tf.float32, [None, None, parameters.model_params['N_INPUT']]),
        "Categorical_features": tf.placeholder(tf.int32,
                                               [None, None, len(parameters.model_params['vocabulary_sizes'].keys())]),
        "Fixed_features": tf.placeholder(tf.float32, [None]),
        "Lengths_features": tf.placeholder(tf.int32, [None]),
        "labels": tf.placeholder(tf.float32, [None]),
        "Predictions": tf.placeholder(tf.float32, [None])
                                }

my_estimator.export_savedmodel('my_directory',
        serving_input_receiver_fn=tf.estimator.export.build_raw_serving_input_receiver_fn(
        features=feature_placeholders)

我收到以下错误:

 File "/home/train_eval_predict.py", line 650, in train_rnn
    features=feature_placeholders)
  File "/home/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 613, in export_savedmodel
    config=self.config)
  File "/home/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 831, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/home/train_eval_predict.py", line 418, in model_fn
    logits=prediction))
  File "/home/tensorflow/python/ops/nn_ops.py", line 1829, in softmax_cross_entropy_with_logits_v2
    logits)
  File "/home/tensorflow/python/ops/nn_ops.py", line 1777, in _ensure_xent_args
    raise ValueError("Both labels and logits must be provided.")
ValueError: Both labels and logits must be provided.

我该如何解决这个问题?

顺便说一下,我不确定feature_placeholders是否已根据tf.estimator.build_raw_serving_input_receiver_fn()正确定义。

0 个答案:

没有答案