In tensorflow, how can I read my predictions from a generator?

7k views Asked by At

I have been adapting the most convolutional network from the following tensorflow tutorial : https://www.tensorflow.org/tutorials/layers

I use the same code except for the shape of the inputs. when I train and evaluate, I get good results. But I would like to see a prediction so that I can know what was misclassified. However when running

y=SN_classifier.predict(input_fn=my_data_to_predict)

where my_data_to_predict is a numpy array of right shape, I get the following output :

<generator object Estimator.predict at 0x7fb1ecefeaf0>

I've read on forums that I should be able to read it my doing : for i in y: print(i)

But it raises 'numpy.ndarray' object is not callable

Same happens if I try :

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

which I read on other forums..

Would you have any idea of why it doesn't output my predictions ?

Here is the part of the code where I define the predict :

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))

And where I call it :

y=SN_classifier.predict(input_fn=my_data_to_predict)

Thank you very much for your help, I'll take any advice, idea :)

3

There are 3 answers

0
DomJack On

input_fn should be a function that generates a tensor. Wrapping it in a numpy_input_fn should be all you need.

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)
0
Dan D. On

There's a way to turn the generator returned by classifier.predict function, by simply wrapping the generator in 'list':

predictor = SN_classifier.predict(input_fn=my_data_to_predict);
results   = list(predictor);
tf.logging.info(results);
0
Philli On

The predict function returns a generator, so you can get the whole dictionary containing all predictions at once.

predictor = SN_classifier.predict(input_fn=my_data_to_predict)

# this is how to get your results:

predictions_dict = next(predictor)