在tensorflow中,如何从生成器中读取我的预测?

问题描述 投票:3回答:3

我一直在调整以下张量流教程中最具卷积性的网络:https://www.tensorflow.org/tutorials/layers

除输入的形状外,我使用相同的代码。当我训练和评估时,我得到了很好的结果。但我希望看到一个预测,以便我可以知道错误分类的内容。但是在跑步时

y=SN_classifier.predict(input_fn=my_data_to_predict)

其中my_data_to_predict是一个正确形状的numpy数组,我得到以下输出:

<generator object Estimator.predict at 0x7fb1ecefeaf0>

我在论坛上看过我应该能够读到它:因为我在y:print(i)

但它引发'numpy.ndarray'对象不可调用

如果我尝试同样的事情:

print('Predictions: {}'.format(list(y))

我在其他论坛上看过..

你能不知道它为什么不输出我的预测?

以下是我定义预测的代码部分:

predictions = {
      # Generate predictions (for PREDICT and EVAL mode)
      "classes": tf.argmax(input=logits, axis=1),
      # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
      # `logging_hook`.
      "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
    }
    if mode == tf.estimator.ModeKeys.PREDICT:
        return(tf.estimator.EstimatorSpec(mode=mode, predictions=predictions))

我称之为:

y=SN_classifier.predict(input_fn=my_data_to_predict)

非常感谢你的帮助,我会接受任何建议,想法:)

python tensorflow machine-learning prediction tensorflow-estimator
3个回答
3
投票

input_fn应该是一个产生张量的函数。将它包装在numpy_input_fn应该是你所需要的。

input_fn = tf.estimator.inputs.numpy_input_fn(my_data_to_predict)
for single_prediction in SN_classifier.predict(input_fn):
    predicted_class = single_prediction['class']
    probability = single_prediction['probability']
    do_something_with(predicted_class, probability)

1
投票

predict函数返回一个生成器,因此您可以一次获取包含所有预测的整个字典。

predictor = SN_classifier.predict(input_fn=my_data_to_predict)

# this is how to get your results:

predictions_dict = next(predictor)

0
投票

有一种方法可以通过简单地将生成器包装在'list'中来转换classifier.predict函数返回的生成器:

predictor = SN_classifier.predict(input_fn=my_data_to_predict);
results   = list(predictor);
tf.logging.info(results);
© www.soinside.com 2019 - 2024. All rights reserved.