Skip to content

Tensorboard

In machine learning, to improve something you often need to be able to measure it. TensorBoard is a tool for providing the measurements and visualizations needed during the machine learning workflow. It enables tracking experiment metrics like loss and accuracy, visualizing the model graph, projecting embeddings to a lower dimensional space, and much more.

More details here : https://www.tensorflow.org/tensorboard/get_started

Tensorboard is installed into the VRE and could be used by different way. We will see how to do that with the following Machine Learning example :

import tensorflow as tf
import datetime
import os

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

def create_model():
  return tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
  ])

model = create_model()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

log_dir = os.join(os.getenv("JUPYTER_TENSORBOARD_LOGDIR"), datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

model.fit(x=x_train, 
          y=y_train, 
          epochs=5, 
          validation_data=(x_test, y_test), 
          callbacks=[tensorboard_callback])

Output:

Epoch 1/5
       1/1875 [..............................] - ETA: 0s - loss: 2.3604 - accuracy: 0.0625WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/summary_ops_v2.py:1277: stop (from tensorflow.python.eager.profiler) is deprecated and will be removed after 2020-07-01.
    Instructions for updating:
    use `tf.profiler.experimental.stop` instead.
    WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0028s vs `on_train_batch_end` time: 0.0070s). Check your callbacks.
    1875/1875 [==============================] - 7s 4ms/step - loss: 0.2184 - accuracy: 0.9362 - val_loss: 0.1005 - val_accuracy: 0.9695
    Epoch 2/5
    1875/1875 [==============================] - 6s 3ms/step - loss: 0.0967 - accuracy: 0.9700 - val_loss: 0.0733 - val_accuracy: 0.9767
    Epoch 3/5
    1875/1875 [==============================] - 6s 3ms/step - loss: 0.0687 - accuracy: 0.9782 - val_loss: 0.0692 - val_accuracy: 0.9769
    Epoch 4/5
    1875/1875 [==============================] - 6s 3ms/step - loss: 0.0542 - accuracy: 0.9833 - val_loss: 0.0581 - val_accuracy: 0.9807
    Epoch 5/5
    1875/1875 [==============================] - 6s 3ms/step - loss: 0.0439 - accuracy: 0.9857 - val_loss: 0.0651 - val_accuracy: 0.9814

<tensorflow.python.keras.callbacks.History at 0x7f82afeb25c0>

Now that your ML model is train and ready you can visualize all the process by launching the tensorboard tool.

Standard use of Tensorboard

  • Open a terminal
  • Use the command : tensorboard --logdir=$HOME/tensorboard/logs --host=0.0.0.0 --port=6006
  • Open a remote desktop
  • Open a web browser and browse localhost:6006. Now you can play with it.

You could use any other port available. 6006 is the default Tensorboard port.

You can also use any other logdir folder.

Jupyter-tensorboard Integration for Jupyter Notebook

Jupyter-tensorboard extension is a jupyter server extension for better collaboration between jupyter notebook and tensorboard providing graphical user interface for tensorboard start, manage and stop in jupyter interface. It provides:

  • No need to type tensorboard and the long log path in command line.
  • No need extra port for serve tensorboard. This is helpful for remote jupyter servers.
  • Multiple tensorboard instance managing simultaneously.

The launcher is directly available in the launcher home page of the VRE with all others applications.

  • Click on the Tensorboard Icon and a Tensorboard instance will be launch.

Tensorboard extension uses $JUPYTER_TENSORBOARD_LOGDIR path. By default this variable is set to $HOME/tensorboard/logs but you can overwrite it into your ~/.bashrc file.

Jupyter Notebook magics

Tensorboard is not usable with Jupyter Notebook magics directly due to proxy security policy.