如何对单个数据项使用tf.estimator.predict

时间:2019-04-15 12:37:29

标签: python tensorflow prediction

我是tf.estimator的新手,之前曾使用过旧版本的tensorflow。传递“ input_fn”使我有些困惑。我有一个DNNClassifier和一个包含单个数据项(x)的numpy数组,我希望为其预测“ y”值。

这是我到目前为止的代码。

dnn_clf = tf.estimator.DNNClassifier(feature_columns=feature_columns, n_classes=14, hidden_units=[60, 100], model_dir="/home/Ehoward14/mysite/dnn_model")

predict_fn = tf.estimator.inputs.numpy_input_fn(x=scaled_X,
                                                    y=None,
                                                    batch_size=1,
                                                    shuffle=False,
                                                    num_epochs=1)

y_pred = dnn_clf.predict(input_fn=predict_fn)

我想像这样打印单个y值:

    //This will only print one value
    for result in y_pred:
            print(result)

scaled_X包含以下内容: [[1.42857143 0. 0. 16.36363636 0. 1.81818182#012 6.66666667 0. 2.896 20. 0. 0.#012 0. 20. 16.66666667 0.]]

此代码返回错误:

for result in y_pred:
  File "/usr/lib/python3.6/site- 
packages/tensorflow/python/estimator/estimator.py", line 543, in predict
features, None, model_fn_lib.ModeKeys.PREDICT, self.config)
  File "/usr/lib/python3.6/site- 
packages/tensorflow/python/estimator/estimator.py", line 1133, in _call_model_fn
     model_fn_results = self._model_fn(features=features, **kwargs)
  File "/usr/lib/python3.6/site- 
   packages/tensorflow/python/estimator/canned/dnn.py", line 385, in _model_fn
    batch_norm=batch_norm)
  File "/usr/lib/python3.6/site- 
   packages/tensorflow/python/estimator/canned/dnn.py", line 179, in _dnn_model_fn
    'Given type: {}'.format(type(features)))
    ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'>

实际上,我只需要知道我的方法是否正确,否则我将不胜感激。

谢谢!

0 个答案:

没有答案