Tensorflow 2.0

How to use tensorboard in Jupyter Notebook?

Tensorflow 2.0

Tensorflow is out with the new Tensorflow2.0.0-alpha and it also brings some new features. And We will talk about what’s new in tensorboard. The main attraction is that user now can run tensorboard live in Jupyter Notebook and Google Colab without opening a new window in their web browser.

You can look at some new features in new and updated tensorboard:

Let’s look at how we can run tensorboard in jupyter notebook.

How to run TensorBoard inside Jupyter Notebook?

You need to update your tensorflow and install tensorflow 2.0.0-aplha0 to run this feature.

Update tensorflow:

# CPU
pip install tensorflow==2.0.0-alpha0
# GPU
pip install tensorflow-gpu==2.0.0-alpha0

Check what version you have installed:

  1. Open Command Prompt.
  2. Startpython
  3. Run the given command and check the version.
import tensorflow as tf
print(tf.__version__)

Install tensorboard-Jupyter Notebook Extension

Install tensorboard extension for jupyter notebook using command prompt or terminal:

pip(3) install jupyter-tensorboard

Run Tensorboard inside Jupyter Notebook

You’ll need some test logs that could be visualized in tensorboard, unless you already have the output files.

So just run the test code to create log files.

You can also download the code file from my gihub link.

The test code is given below:

from kaggle_data import load_data, preprocess_data, preprocess_labels
import numpy as np
import matplotlib.pyplot as plt
X_train, labels = load_data('../data/kaggle_ottogroup/train.csv', train=True)
X_train, scaler = preprocess_data(X_train)
Y_train, encoder = preprocess_labels(labels)
X_test, ids = load_data('../data/kaggle_ottogroup/test.csv', train=False)
X_test, _ = preprocess_data(X_test, scaler)
nb_classes = Y_train.shape[1]
print(nb_classes, 'classes')
dims = X_train.shape[1]
print(dims, 'dims')
import tensorflow as tf
from keras.layers import Dense, Activation
dims = X_train.shape[1]
print(dims, 'dims')
print("Building model...")
nb_classes = Y_train.shape[1]
print(nb_classes, 'classes')
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(nb_classes, input_shape=(dims,), activation='sigmoid'))
model.compile(optimizer = 'sgd',
              loss='categorical_crossentropy')
model.fit(X_train, Y_train,
          epochs=10,
          callbacks=[tf.keras.callbacks.TensorBoard('logs')]
         )

RUN TENSORBOARD

# Load TENSORBOARD
%load_ext tensorboard.notebook
# Start TENSORBOARD
%tensorboard --logdir logs

This should open tensorboard in the same cell.

TensorBoard in Tensorflow 2.0 GIF
GIF

Best of Luck with your adventures.

If you hit a wall while implementing this post, reach out and comment below.

Previous Posts you will Love:

Leave a Reply

Your email address will not be published. Required fields are marked *