python - 如何将 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'>
实际上,我只需要知道我的方法是否正确,如果不是,那么任何改进建议都将不胜感激。
提前致谢!
解决方案
推荐阅读
- json - 是否可以使用 fetch 返回 JSON 对象?
- android - 在 OpenCV 中查找带有文本行的轮廓
- swift - Firestore - UITableViewCell 内的 UISlider
- javascript - 变量不更新 Javascript
- c++ - 在 C 中对 2 Dim 数组进行排序
- android - onScrolled 在没有用户滚动的情况下被调用(Recycler View 无限滚动)
- tensorflow - ND张量矩阵乘法与张量流
- yii2 - Yii2 使用 UrlManager::createUrl() 将语言添加到 URL
- sql - where 子句中的星期几和时间
- php - 如何使用 PHP curl 从 ZOHO 图书 API 获取 JSON 数据?