6

I'm using the code from the MNIST tutorial:

feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                            hidden_units=[10, 20, 10],
                                            n_classes=2,
                                            model_dir="/tmp/iris_model")

classifier.fit(x=np.array(train, dtype = 'float32'),
               y=np.array(y_tr, dtype = 'int64'),
               steps=2000)

accuracy_score = classifier.evaluate(x=np.array(test, dtype = 'float32'),
                                     y=y_test)["auc"]
print('AUC: {0:f}'.format(accuracy_score))

from tensorflow.contrib.learn import SKCompat
ds_test_ar = np.array(ds_test, dtype = 'float32')

ds_predict_tf = classifier.predict(input_fn = _my_predict_data)
print('Predictions: {}'.format(str(ds_predict_tf)))

but at the end I got the following result instead of the predictions:

Predictions: <generator object DNNClassifier.predict.<locals>.<genexpr> at 0x000002CE41101CA8>

What did I do wrong?

5 Answers5

14

What you received and saved to ds_predict_tf is a generator expression. To print it you can do:

for i in ds_predict_tf:
    print i

or

print(list(ds_predict_tf))

You can read more about genexpr here.

Community
  • 1
  • 1
sygi
  • 4,557
  • 2
  • 32
  • 54
9

The DNNClassifier predict function by default have as_iterable=True. Thus, it returns an generator. For getting values of predictions instead of generator, pass as_iterable=False in classifier.predict method.

For example,

classifier.predict(input_fn = _my_predict_data,as_iterable=False)



For understanding more about classifier methods and arguments. Here is a part of documentation for predict method.

From DNNClassifier documentation:

Predict

Args:

  • x: features.
  • input_fn: Input function. If set, x must be None.
  • batch_size: Override default batch size.
  • outputs: list of str, name of the output to predict. If None, returns classes.
  • as_iterable: If True, return an iterable which keeps yielding predictions for each example until inputs are exhausted. Note: The inputs must terminate if you want the iterable to terminate (e.g. be sure to pass num_epochs=1 if you are using something like read_batch_features).

Returns:

  • Numpy array of predicted classes with shape [batch_size] (or an iterable of predicted classes if as_iterable is True). Each predicted class is represented by its class index (i.e. integer from 0 to n_classes-1). If outputs is set, returns a dict of predictions.
Community
  • 1
  • 1
ML_NN
  • 101
  • 1
  • 7
2

Solution:-

pred = classifier.fit(x=training_set.data, y=training_set.target, steps=2000).predict(test_set.data)

print ("Predictions:")

print(list(pred))

That's it...

Michael Yadidya
  • 1,397
  • 1
  • 9
  • 15
1

To be as close as possible to the tutorial use:

print('Predictions: {}' .format(list(ds_predict_tf)))
simo23
  • 506
  • 4
  • 12
0

Sorry, the answer is very easy, you need to use the predictor as generator object:

g1 = ds_predict_tf

[g1.__next__() for i in range(100)]