TensorBoard is a great way to visualize what's happening behind the code.
In this example, we'll loop through some numbers to improve our guess of the average value. Then we can visualize the results on TensorBoard.
Let's just set ourselves up with some data to work with:
In [1]:
import tensorflow as tf
import numpy as np
raw_data = np.random.normal(10, 1, 100)
The moving average is defined as follows:
In [2]:
alpha = tf.constant(0.05)
curr_value = tf.placeholder(tf.float32)
prev_avg = tf.Variable(0.)
update_avg = alpha * curr_value + (1 - alpha) * prev_avg
Here's what we care to visualize:
In [3]:
avg_hist = tf.summary.scalar("running_average", update_avg)
value_hist = tf.summary.scalar("incoming_values", curr_value)
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs")
Time to compute the moving averages. We'll also run the merged
op to track how the values change:
In [4]:
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(len(raw_data)):
summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]})
sess.run(tf.assign(prev_avg, curr_avg))
print(raw_data[i], curr_avg)
writer.add_summary(summary_str, i)
Check out the visualization by running TensorBoard from the terminal:
$ tensorboard --logdir=path/to/logs
In [5]:
#made the logs be written successfully
writer.close()
In [ ]: