使用tensorflow.summary时遇到错误

时间:2017-10-20 14:15:45

标签: python tensorflow tensorboard

我通过试用示例mnist_softmax.py(this is the orignal file)来学习 TensorFlow ,我为它添加了一些修改以便使用 TensorBoard

   tf.global_variables_initializer().run()
  merged_summary_op = tf.summary.merge_all()
  summary_writer = tf.summary.FileWriter('mnist_logs', sess.graph)
  # Train
  for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    if i%25 == 0:
        summary_str , _  =  sess.run(merged_summary_op)
        summary_writer.add_summary(summary_str,i)

  # Test trained model
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  print(sess.run(accuracy, feed_dict={x: mnist.test.images,
                                      y_: mnist.test.labels}))

但运行此文件时出现错误:

 File "mnist_softmax.py", line 87, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "mnist_softmax.py", line 73, in main
    summary_str , _  =  sess.run(merged_summary_op)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1109, in _run
    self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 413, in __init__
    self._fetch_mapper = _FetchMapper.for_fetch(fetches)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 230, in for_fetch
    (fetch, type(fetch)))
TypeError: Fetch argument None has invalid type <class 'NoneType'>

如果有人能找到我写错的地方,我将非常感激:)。

1 个答案:

答案 0 :(得分:0)

我相信这个问题是一样的:Tensorflow TypeError: Fetch argument None has invalid type <type 'NoneType'>?

虽然代码中未显示,但变量train_step的值可能为None。打印出来并检查。那是你的&#34; fetch参数&#34;,它是你要求张量计算并返回给你的OP或张量。

您的代码中的某个地方可能有:

train_step = tf.train.AdamOptimizer(...).minimize(...)

Tensorflow需要您将此OP(操作)发送到sess.run(...)。这指示张量流计算train_step&#34; OP&#34;,并且这样做会更新权重。在某个地方train_step可能被重新分配到None