How to use TensorBoard with Jupyter Notebook (kaggle and colab)?

Published on Aug. 22, 2023, 12:19 p.m.

To use TensorBoard with Jupyter Notebook, you can use the tensorboard magic command in a notebook cell, which will start a TensorBoard instance and display it inline in the notebook. Here’s how:

  1. Install the jupyter-tensorboard extension by running !pip install jupyter-tensorboard in your Jupyter Notebook.
  2. Load the tensorboard extension by running %load_ext tensorboard in your notebook.
  3. Start a TensorBoard instance and specify the log directory for your data by running %tensorboard --logdir=<path_to_your_logs> in a notebook cell. This will start a TensorBoard instance that you can view in your browser.
  4. To see the TensorBoard visualizations in your notebook, you can use the tensorboard magic command with the --host=localhost and --port= specified. For example, running %tensorboard --logdir=<path_to_your_logs> --host=localhost --port=6006 will display TensorBoard in your notebook at localhost:6006.
  5. You can also use the tensorboard.notebook module to display TensorBoard visualizations directly in a cell. For example, running the following code will display the TensorBoard scalar summary for your data in the notebook:
import tensorboard.notebook as tb
tb.notebook.start('--logdir <path_to_your_logs>')

To use TensorBoard with Google Colab

To use TensorBoard with Google Colab, you can use the tensorboard magic command to start a TensorBoard instance and expose it to the internet using a tool like ngrok. Here’s how:

  1. Load the TensorBoard extension by running %load_ext tensorboard in your Colab notebook.
  2. Start a TensorBoard instance by running %tensorboard --logdir=<path_to_your_logs> in a notebook cell.
  3. Install ngrok by running !pip install ngrok in a notebook cell.
  4. In a new cell, run ngrok http 6006. This will create a public URL that you can use to access the TensorBoard instance running on port 6006.
  5. In your browser, navigate to the URL generated by ngrok. This will display the TensorBoard instance, which you can use to visualize your machine learning experiments.

Note that ngrok creates a temporary public URL that expires after a certain period of time. If you want a more permanent solution, you can consider deploying your own server or using a cloud service like Google Cloud Platform or Amazon Web Services.

To use TensorBoard with Kaggle

To use TensorBoard with Kaggle, you can follow these steps:

  1. In your Kaggle notebook, train your model and save the logs to a directory using TensorBoard’s FileWriter API. For example, you can use the following code to save the logs to a directory called logs:
from tensorflow.keras.callbacks import TensorBoard
import datetime

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
model.fit(x, y, epochs=10, callbacks=[tensorboard_callback])
  1. Load the TensorBoard extension by running %load_ext tensorboard in your notebook.
  2. Start a TensorBoard instance by running %tensorboard --logdir=<path_to_your_logs> in a notebook cell, where <path_to_your_logs> is the path to the directory containing your logs.
  3. Create a public URL for your TensorBoard instance using ngrok. You can install and run ngrok within the notebook using the following code:
!pip install ngrok
get_ipython().system_raw('ngrok http 6006 &')
  1. In your browser, navigate to the URL generated by ngrok. This will display the TensorBoard instance, which you can use to visualize your machine learning experiments.

Note that ngrok creates a temporary public URL that expires after a certain period of time. If you want a more permanent solution, you can consider deploying your own server or using a cloud service like Google Cloud Platform or Amazon Web Services.