Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: Track the values a tensor takes

Tags:

tensorflow

Through a forward pass in my network I wanted to log the values a particular tf.variable takes. Is there a simple way to do this?

like image 967
fragman Avatar asked Jul 02 '26 08:07

fragman


1 Answers

There are a few ways to log or debug data in TensorFlow.

The simplest is to run it in a session, or 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()

This often isn't possible, so another approach is to put tf.Print ops in your graph. Here's how you would print the variable whenever it's used.

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

The tf.Print op will only print the first few entries if you have a large tensor, so check the docs for the summarize and first_n parameters to control how much is logged.

You can use TensorBoard to log summaries of variables during graph execution too. If you're not already using it, you should check it out, many of the higher-level APIs within TensorFlow already log a lot of information about the model variables during execution to TensorBoard. To perform your own logging in TensorBoard, use something like tf.summary.scalar or tf.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)

Check out the docs for details on how to save these summaries to disk during execution: https://www.tensorflow.org/get_started/summaries_and_tensorboard.

And lastly, there's a debugger available for TensorFlow, tfdbg, you can use to step through graph execution and dump the contents of tensors.

like image 179
Mark McDonald Avatar answered Jul 04 '26 15:07

Mark McDonald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!