Can I use TensorBoard with Google Colab?

TensorBoard for TensorFlow running on Google Colab using tensorboardcolab. This uses ngrok internally for tunnelling.

  1. Install TensorBoardColab

!pip install tensorboardcolab

  1. Create a tensorboardcolab object

tbc = TensorBoardColab()

This automatically creates a TensorBoard link that can be used. This Tensorboard is reading the data at './Graph'

  1. Create a FileWriter pointing to this location

summary_writer = tbc.get_writer()

tensorboardcolab library has the method that returns FileWriter object pointing to above './Graph' location.

  1. Start adding summary information to Event files at './Graph' location using summary_writer object

You can add scalar info or graph or histogram data.

Reference: https://github.com/taomanwai/tensorboardcolab


EDIT: You probably want to give the official %tensorboard magic a go, available from TensorFlow 1.13 onward.


Prior to the existence of the %tensorboard magic, the standard way to achieve this was to proxy network traffic to the Colab VM using ngrok. A Colab example can be found here.

These are the steps (the code snippets represent cells of type "code" in colab):

  1. Get TensorBoard running in the background.
    Inspired by this answer.

    LOG_DIR = '/tmp/log'
    get_ipython().system_raw(
        'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
        .format(LOG_DIR)
    )
    
  2. Download and unzip ngrok.
    Replace the link passed to wget with the correct download link for your OS.

    ! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
    ! unzip ngrok-stable-linux-amd64.zip
    
  3. Launch ngrok background process...

    get_ipython().system_raw('./ngrok http 6006 &')
    

    ...and retrieve public url. Source

    ! curl -s http://localhost:4040/api/tunnels | python3 -c \
        "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
    

Many of the answers here are now obsolete. So will be mine I'm sure in a few weeks. But at the time of this writing all I had to do is run these lines of code from colab. And tensorboard opened up just fine.

%load_ext tensorboard
%tensorboard --logdir logs

Here's an easier way to do the same ngrok tunneling method on Google Colab.

!pip install tensorboardcolab

then,

from tensorboardcolab import TensorBoardColab, TensorBoardColabCallback

tbc=TensorBoardColab()

Assuming you are using Keras:

model.fit(......,callbacks=[TensorBoardColabCallback(tbc)])

You can read the original post here.