擅长:python、mysql、java
<p>在tensorflow中编程时,了解定义操作和执行操作之间的区别是至关重要的。在python中运行时,大多数以<code>tf.</code>开头的函数都会向计算图添加操作。在</p>
<p>例如,当您执行以下操作时:</p>
<pre><code>tf.scatter_update(S,inds_new,updates)
</code></pre>
<p>以及:</p>
^{pr2}$
<p>很多次,你的计算图增长超出了需要的范围,填满了所有的内存,并极大地减慢了速度。在</p>
<p>您应该做的是在循环之前定义一次计算:</p>
<pre><code>init = tf.initialize_all_variables()
inds_new = sampled_ind + line*K
update_op = tf.scatter_update(S, inds_new, updates)
sess = tf.Session()
sess.run(init)
for line in range(N):
sess.run(update_op, feed_dict={tfx: X[line:line+1]})
</code></pre>
<p>这样,计算图只包含<code>inds_new</code>和<code>update_op</code>的一个副本。请注意,当您执行<code>update_op</code>时,<code>inds_new</code>也将被隐式执行,因为它在计算图中是其父级。在</p>
<p>{cd3>你应该知道,每一次运行的结果都是不同的。在</p>
<p>另外,调试这类问题的一个很好的方法是使用张量板可视化计算图。在代码中添加:</p>
<pre><code>summary_writer = tf.train.SummaryWriter('some_logdir', sess.graph_def)
</code></pre>
<p>然后在控制台中运行:</p>
<pre><code>tensorboard logdir=some_logdir
</code></pre>
<p>在服务的html页面上会有一个计算图的图片,在这里你可以检查你的张量。在</p>