Tried to convert 'indices' to a tensor and failed. Error: None values not supported

GitHub Link of the code - https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/text_classification.py

The above code is an example of text classification using RNN or bag of words.

I trained the model using Ubuntu's conversation corpus. After training i modified the code to predict on the trained model ( changes are descirbed in another page.).

Briefly, my change was this,

  if train == True:
   print("Training the model")
   classifier.fit(x_train, y_train, steps=10000)

  if predict == True:
   print("Prediction is Enabled")
   p = classifier.predict(x_test,as_iterable=True)
   #y_predicted = [ p['class'] for p in classifier.predict(x_test, as_iterable=True) ]
   print(p)
   if evaluate_accuracy == True:
    print("Evaluation is Enabled")
    score = metrics.accuracy_score(y_test, y_predicted)
    print('Accuracy: {0:f}'.format(score))

With flag set as

train=False
predict=True
evaluate_accuracy=False

I will be able to call the predict method on classifier object directly instead calling classifier.fit before. I am already loading the pretrained model using Estimator API.

However, this generated error as below,

Reading data from directory
Data read completed
Creating vocabulary
Total words: 154652
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': None, '_save_checkpoints_secs': 600, '_num_ps_replicas': 0, '_keep_checkpoint_max': 5, '_tf_random_seed': None, '_task_type': None, '_environment': 'local', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7f9e0cc6aed0>, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1.0
}
, '_num_worker_replicas': 0, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': '', '_session_config': None}
Prediction is Enabled
/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py:254: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  equality = a == b
WARNING:tensorflow:From text_classification.py:184: calling predict (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
Traceback (most recent call last):
  File "text_classification.py", line 206, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "text_classification.py", line 184, in main
    p = classifier.predict(x_test,as_iterable=True)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 289, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 583, in predict
    as_iterable=as_iterable)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 877, in _infer_model
    infer_ops = self._get_predict_ops(features)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1209, in _get_predict_ops
    return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.INFER)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1124, in _call_model_fn
    model_fn_results = self._model_fn(features, labels, **kwargs)
  File "text_classification.py", line 108, in rnn_model
    target = tf.one_hot(target, 71, 1, 0)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 2170, in one_hot
    name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1853, in _one_hot
    axis=axis, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 508, in apply_op
    (input_name, err))
ValueError: Tried to convert 'indices' to a tensor and failed. Error: None values not supported.

This error appears because the method or api call to classifier.fit was skipped and may be it was initializing some things ( may be creating the graph? .. not sure).

I changed above code to following and it works now,

  if train == True:
   print("Training the model")
   classifier.fit(x_train, y_train, steps=10000)

  if predict == True:
   classifier.evaluate(x_train[:5],y_train[:5],batch_size=5)   # This is added to initialize what ever was missing
   print("Prediction is Enabled")
   p = classifier.predict(x_test,as_iterable=True)
   #y_predicted = [ p['class'] for p in classifier.predict(x_test, as_iterable=True) ]
   print(p)
   if evaluate_accuracy == True:
    print("Evaluation is Enabled")
    score = metrics.accuracy_score(y_test, y_predicted)
    print('Accuracy: {0:f}'.format(score))

Adding classifier.evaluate solved the problem. Note, this is only a workaround.

Reference:

https://github.com/tensorflow/tensorflow/issues/3208

This error can also be skipped by following code,

<TODO: test this code>

  # Ugly hack, seems to be a bug in Tensorflow
  # estimator.predict doesn't work without this line
  estimator._targets_info = tf.contrib.learn.estimators.tensor_signature.TensorSignature(tf.constant(0, shape=[1,1]))

results for ""

    No results matching ""