Tensorflow:跟踪张量所需的值

时间:2017-03-23 02:41:06

标签: tensorflow

通过我网络中的正向传递,我想记录特定tf.variable所需的值。有一种简单的方法可以做到这一点吗?

2 个答案:

答案 0 :(得分:1)

有几种方法可以在TensorFlow中记录或调试数据。

最简单的方法是在会话或eval中运行它。 e.g。

import tensorflow as tf
sess = tf.InteractiveSession()

v = tf.Variable([0.0])
# you can do other graph things here.

print sess.run(v)
# alternatively
print v.eval()

这通常是不可能的,因此另一种方法是将tf.Print操作放在图表中。以下是在使用变量时如何打印变量。

import tensorflow as tf

v = tf.Variable([0.0], name="the_var")
v = tf.Print(v, [v], "the_var = ")

# ... do things with 'v' as if it was the variable op

tf.Print操作只会打印前几个条目(如果您有一个较大的张量),因此请检查文档中的summarizefirst_n参数以控制记录的数量。< / p>

您也可以使用TensorBoard在图形执行期间记录变量摘要。如果你还没有使用它,你应该check it out,TensorFlow中的许多更高级别的API已经在执行到TensorBoard期间记录了很多关于模型变量的信息。要在TensorBoard中执行自己的日志记录,请使用tf.summary.scalartf.summary.histogram之类的内容。

v = tf.Variable([0.0])
# this will log to the 'distributions' tab in tensorboard too
tf.summary.scalar(v)
tf.summary.histogram(v)

查看文档,了解有关如何在执行期间将这些摘要保存到磁盘的详细信息:https://www.tensorflow.org/get_started/summaries_and_tensorboard

最后,有一个可用于TensorFlow的调试器tfdbg,您可以使用它来逐步执行图形执行并转储张量的内容。

答案 1 :(得分:0)

您可以在tensorflow变量的每一步调试值。