Xander May
Xander May

Reputation: 121

Python Tensorflow NoneType is not Iterable

Full code is here

The error:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-e6c5369957bc> in <module>()
     55 print(feed_dict)
     56 _ , loss_val = sess.run(tr_op, 
---> 57                         feed_dict=feed_dict)
     58 print('Loss of the current batch is {}'.format(loss_val))

TypeError: 'NoneType' object is not iterable

The following code is run before that call is executed:

print(type(tr_op))
print(type(feed_dict))
try:
    some_object_iterator = iter(feed_dict)
except TypeError as te:
    print (feed_dict, 'is not iterable')
print(feed_dict)

which yields the output:

<class 'tensorflow.python.framework.ops.Operation'>
<class 'dict'>
{<tf.Tensor 'img_data:0' shape=(?, 64, 64, 3) dtype=float32>: array([[[[0.3019608 , 0.23921569, 0.58431375],
         [0.30588236, 0.23921569, 0.61960787],
         [0.30980393, 0.24705882, 0.63529414],
         ...,

So the object that should be iterable is clearly iterable because it isnt raising the exception when I try to make an iterator out of it, and both objects both have clearly defined types. This error is not in the TensorFlow file for session, and I dont know where it was raised because there is no trace.

Thanks for any help

Upvotes: 6

Views: 5651

Answers (1)

David Parks
David Parks

Reputation: 32071

Looks like your issue is here:

_ , loss_val = sess.run(tr_op, feed_dict=feed_dict)

You are asking tensorflow to compute the tr_op for you. That is one operation. E.g. one return value from sess.run will be produced. You are trying to extract 2 values from the result. It's trying to treat the return value of None as a tuple in this case.

Try this to see it:

result = sess.run(tr_op, feed_dict=feed_dict)
print(result)

You'll see that result is None, which is correct return value for an optimizer (tr_op). If you further attempted:

_, loss_val = result

You would again get your error TypeError: 'NoneType' object is not iterable.

What you meant to do probably is this:

_ , loss_val = sess.run([tr_op, loss_op], feed_dict=feed_dict)

Upvotes: 16

Related Questions