擅长:python、mysql、java
<p>虽然其他的答案是正确的,即在对图形求值之前不能打印该值,但它们并没有讨论一种简单的方法,即在对图形求值之后,在图形中实际打印一个值。</p>
<p>每当对图求值(使用<code>run</code>或<code>eval</code>)时,查看张量值的最简单方法是使用<a href="https://www.tensorflow.org/versions/master/api_docs/python/control_flow_ops.html#Print" rel="noreferrer">^{<cd3>}</a>操作,如下例所示:</p>
<pre><code># Initialize session
import tensorflow as tf
sess = tf.InteractiveSession()
# Some tensor we want to print the value of
a = tf.constant([1.0, 3.0])
# Add print operation
a = tf.Print(a, [a], message="This is a: ")
# Add more elements of the graph using a
b = tf.add(a, a)
</code></pre>
<p>现在,每当我们评估整个图表时,例如使用<code>b.eval()</code>,我们得到:</p>
<pre><code>I tensorflow/core/kernels/logging_ops.cc:79] This is a: [1 3]
</code></pre>