Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 724
Xander May Avatar asked May 18 '18 23:05

Xander May


People also ask

How do I fix NoneType object is not iterable in Python?

The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.

What does NoneType object is not iterable Python?

The Python "TypeError: 'NoneType' object is not iterable" occurs when we try to iterate over a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before iterating.

How do I iterate NoneType in Python?

The TypeError: 'NoneType' object is not iterable error is raised when you try to iterate over an object whose value is equal to None. To solve this error, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list.

How do you fix TypeError argument of type NoneType is not iterable?

The Python "TypeError: argument of type 'NoneType' is not iterable" occurs when we use the membership test operators (in and not in) with a None value. To solve the error, correct the assignment of the variable that stores None or check if it doesn't store None .


1 Answers

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)
like image 97
David Parks Avatar answered Oct 24 '22 12:10

David Parks