InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor

本文探讨了使用IPython构建TensorFlow模型时遇到的一个常见陷阱:即tf.merge_all_summaries()会记住当前会话中创建的所有摘要,包括那些因错误而失败的单元格。文章提供了两种解决方案:一种是显式地合并所需的摘要;另一种是在构建模型前设置一个明确的默认图。
以下两种方式都不好使用:目前只是找到原因,等我找到方案再来完善。

From your error message, it looks like you are using IPython. One pitfall when using IPython to build a TensorFlow model is that functions like tf.merge_all_summaries() will remember every summary created in the current session, including cells that failed with an error. This is a result of TensorFlow using a default graph to collect all of the operations, summaries, etc. that are created in a process, unless you specify the graph explicitly. I suspect that your call to tf.merge_all_summaries() is returning more than the three histogram summaries that you created in your code, and the older ones will have a dependency on a previously created placeholder.

There are two main ways that you could fix that. The simplest is to explicitly merge the summaries, rather than using tf.merge_all_summaries():

  1. weights_summary = tf . histogram_summary ( "weights" , W )
  2. biases_summary = tf . histogram_summary ( "biases" , b )
  3. y_summary = tf . histogram_summary ( "y" , y )
  4. merged = tf . merge_summary ([ weights_summary , biases_summary , y_summary ])

An alternative would be to set an explicit default graph before constructing your model. This is awkward if you want to split your model across multiple IPython cells, but should also work:

  1. # Sets a new default graph, and stores it in `g`.
  2. with tf . Graph (). as_default () as g :
  3. x = tf . placeholder ( tf . float32 , [ None , 784 ])
  4. # ...
  5. # Summaries are added to `g`.
  6. _ = tf . histogram_summary ( "weights" , W )
  7. _ = tf . histogram_summary ( "biases" , b )
  8. _ = tf . histogram_summary ( "y" , y )
  9. # ...
  10. # `merged` contains only summaries from `g`.
  11. merged = tf . merge_all_summaries ()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值