How can I code feed_dict

爷,独闯天下 提交于 2020-01-04 06:34:51

问题


Codes which produces AE

x = tf.placeholder(tf.float32, [None, 784])
keep_prob = tf.placeholder("float")

for step in range(2000):
    batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE)
    sess.run(train_step, feed_dict={x: batch_xs, keep_prob: (1 - DROP_OUT_RATE) }) # feed_dict 
    if step % 10 == 0:
        summary_op = tf.merge_all_summaries()
        summary_str = sess.run(summary_op, feed_dict={x: batch_xs, keep_prob: 1.0})
        summary_writer.add_summary(summary_str, step)
    if step % 100 == 0:
        print(loss,eval(session=sess, feed_dict={x: batch_xs, keep_prob: 1.0}))

What I got as an error message

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

in log

 File "<ipython-input-9-c70541b6146b>", line 18, in <module>
    x = tf.placeholder(tf.float32, [None, 784])

I don't know what I need to do. It seems to me that the codes are valid.


回答1:


The problem here is almost certainly the call to tf.merge_all_summaries(), which doesn't play well with interactive (e.g. IPython) usage. Perhaps unintuitively, tf.merge_all_summaries() adds a new operation to the graph that depends on all summaries that have been created in the same graph. When you are using IPython, the graph will remember all of the summaries that you created, even in previous executions of the same cell. This can lead to tf.merge_all_summaries() creating a tensor that depends on a tf.placeholder() created in a previous execution of the cell.

There are two main workarounds for this, as I described in another answer:

  1. Explicitly merge the summaries that you want to compute, using tf.merge_summary([summary_1, summary_2, summary_3]).

  2. Create your graph in an explicit with tf.Graph().as_default(): block, which will ensure that only the summaries created within that block are included in the merge.

Note also that tf.merge_all_summaries() adds a new op to the graph each time you run it. This means that your training loop has a subtle memory leak, and will add 200 copies of the same op to the graph. To avoid this, you should call tf.merge_all_summaries() outside your training loop and cache the resulting tensor. You can then use that tensor within the training loop to get the same result.



来源:https://stackoverflow.com/questions/35116566/how-can-i-code-feed-dict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!