Thursday, December 7, 2017

How to use TensorBoard through arithmetic calculation on TensorFlow

Overview

Through basic arithmetic operations, let’s check how those are expressed on TensorBoard.

It has two main points.
One, check the main calculation function on TensorFlow.
Two, check how it is expressed on TensorBoard.

TensorFlow deals with Tensor, leading us to use TensorFlow’s methods for mathematical operations. Simply, here, I’ll use some of them. And, TensorBoard is the tool to check the graph and other information graphically. As a simple check, I’ll show how those operations are expressed visually on that.




Environment

pip freeze | grep tensor
tensorboard==1.0.0a6
tensorflow==1.2.0

If tensorflow and tensorboard are not on list, you can install those by pip.

Mathematical Operation


TensorFlow deals with Tensor and for that, we need to use the method of TensorFlow. Here, I just show scalar calculations.

import tensorflow as tf

const1 = tf.constant(2, name='CONST1')
const2 = tf.constant(3, name='CONST2')

# add
plus = tf.add(const1, const2, name='PLUS')

# subtract
subtract = tf.subtract(const1, const2, name='MINUS')

# multiple
multiple = tf.scalar_mul(const1, const2)

# divide
divide = tf.divide(const1, const2, name='DIVIDE')

Literally, the code above is for addition, subtraction, multiplication and division. About multiplication and division, there are many other methods responding to your needs. I simply use one of them.

Execution


Here, let’s print the outcome of the calculations and save the graph information for visualization on TensorBoard.

sess = tf.InteractiveSession()
writer = tf.summary.FileWriter('./logdir', sess.graph)
tf.global_variables_initializer().run()
print(sess.run(plus))
print(sess.run(subtract))
print(sess.run(multiple))
print(sess.run(divide))

By executing this, we can get following output.

5
-1
6
0.666666666667

TensorBoard


On the Execution phase, I specified the log place. To visualize on TensorBoard, on terminal we can execute tensorboard command with specifying the directory of the log.

tensorboard --logdir='./logdir'

As a default, port 6006 is used. On browser, http://localhost:6006 shows the information.
We can get the plot like this.

enter image description here

As you can see, about addition, subtraction and multiplication, the graphs are same except for the name of those. tf.scalar_mul() method doesn’t have the option of name. So only multiplication has the name ‘mul’ as default. When we want to get the detail of the operation. By clicking the operation points, the detail will be shown. On the case of the image below, I clicked the ‘PLUS’ point and operation detail came on the right side.

enter image description here

Also, the way of expressing division is interesting. Different from other operations, It seems bit confusing way.