首页 > 解决方案 > 如何将 tf.estimator.predict 用于单个数据项

问题描述

我是 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'>

实际上,我只需要知道我的方法是否正确,如果不是,那么任何改进建议都将不胜感激。

提前致谢!

标签: pythontensorflowprediction

解决方案


推荐阅读