Intro
In this article, I would like to briefly give a tutorial of Tensorboard using Keras.
Environment
OS: Ubuntu: 16.04 LTS
Python: 3.6.2
What is Tensorboard?
Simply saying, Tensorboard is a web-application for inspecting and understanding the result of the training using Tensorflow. And currently it does support five different visualisations: scalars, images, audio, histograms, and computational graphs. As you may know, all computations happen in Tensorflow are represented by the computational nodes. So, utilising visualisation tool, we can efficiently improve the model.
For more information regarding Tensorboard UI, please refer to this.
This is what a TensorBoard graph looks like:
The basic script
import tensorflow as tf
with tf.name_scope("MyOperationGroup"):
with tf.name_scope("Scope_A"):
a = tf.add(1, 2, name="Add_these_numbers")
b = tf.multiply(a, 3)
with tf.name_scope("Scope_B"):
c = tf.add(4, 5, name="And_These_ones")
d = tf.multiply(c, 6, name="Multiply_these_numbers")
with tf.name_scope("Scope_A"):
e = tf.multiply(4, 5, name="B_add")
f = tf.div(c, 6, name="B_mul")
g = tf.add(b, d)
h = tf.multiply(g, f)
with tf.Session() as sess:
writer = tf.summary.FileWriter("output", sess.graph)
print(sess.run(h))
writer.close()
Collecting run data from your Keras program
from time import time
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.callbacks import TensorBoard
import numpy as np0
x_train, y_train = np.random.rand(5,5), np.random.randint(2, size=5)
x_test, y_test = np.random.rand(3,5), np.random.randint(2, size=3)
model = Sequential()
model.add(Dense(3, input_dim=5, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='sgd', loss='mse')
tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
model.fit(x_train, y_train, epochs=100, verbose=1, callbacks=[tensorboard])
score = model.evaluate(x_test, y_test)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
Once you finish the training, you can move on to check the result of the tensorboard by executing the command below.
$ tensorboard --logdir=logs/
Thank you for reading.