Python Tensorflow NoneType不可迭代

Xan*_*May 5 python deep-learning tensorflow

完整代码在这里

错误:

    ---------------------------------------------------------------------------
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
Run Code Online (Sandbox Code Playgroud)

在执行该调用之前运行以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

产生输出:

<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],
         ...,
Run Code Online (Sandbox Code Playgroud)

因此,应该可迭代的对象显然是可迭代的,因为当我尝试从中创建迭代器时它不会引发异常,并且两个对象都具有明确定义的类型.此错误不在会话的TensorFlow文件中,我不知道它被引发的位置,因为没有跟踪.

谢谢你的帮助

Dav*_*rks 14

看起来您的问题在这里:

_ , loss_val = sess.run(tr_op, feed_dict=feed_dict)
Run Code Online (Sandbox Code Playgroud)

你问tensorflow tr_op为你计算.这是一个操作.例如,sess.run将产生一个返回值.您正尝试从结果中提取2个值.None在这种情况下,它试图将返回值视为元组.

试试看:

result = sess.run(tr_op, feed_dict=feed_dict)
print(result)
Run Code Online (Sandbox Code Playgroud)

您将看到结果是None,这是优化器(tr_op)的正确返回值.如果你进一步尝试:

_, loss_val = result
Run Code Online (Sandbox Code Playgroud)

你会再次得到你的错误TypeError: 'NoneType' object is not iterable.

你打算做什么可能是这样的:

_ , loss_val = sess.run([tr_op, loss_op], feed_dict=feed_dict)
Run Code Online (Sandbox Code Playgroud)