0

Is there any way to run tensorboard in google collab while using tensorflow-1.x? If not, how to use tensorboard in with tensorflow-1.x?

I would appreciate posting an any working example.

user48115
  • 445
  • 1
  • 8
  • 18
  • Maybe answers in [here](https://stackoverflow.com/questions/47818822/can-i-use-tensorboard-with-google-colab) can help you. – Fazil Əmirli Jul 21 '20 at 10:31

1 Answers1

1

Yes, it is possible. Here is the complete working code to visualize histogram using Tensorboard in Google Colab.

%tensorflow_version 1.x
%load_ext tensorboard

import tensorflow as tf
print(tf.__version__)
import datetime, os

fashion_mnist = tf.keras.datasets.fashion_mnist

(x_train, y_train),(x_test, y_test) = fashion_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')
  ])

def train_model():

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

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

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

train_model()

%tensorboard --logdir logs

Output:

TensorFlow 1.x selected.
1.15.2
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
60000/60000 [==============================] - 15s 250us/sample - loss: 0.4987 - acc: 0.8206 - val_loss: 0.4289 - val_acc: 0.8476
Epoch 2/5
60000/60000 [==============================] - 15s 253us/sample - loss: 0.3847 - acc: 0.8592 - val_loss: 0.3928 - val_acc: 0.8600
Epoch 3/5
60000/60000 [==============================] - 15s 246us/sample - loss: 0.3463 - acc: 0.8730 - val_loss: 0.3713 - val_acc: 0.8660
Epoch 4/5
60000/60000 [==============================] - 15s 246us/sample - loss: 0.3292 - acc: 0.8786 - val_loss: 0.3523 - val_acc: 0.8697
Epoch 5/5
60000/60000 [==============================] - 15s 249us/sample - loss: 0.3100 - acc: 0.8848 - val_loss: 0.3455 - val_acc: 0.8757

enter image description here

  • I copy-pasted and run your code in google collab, but somehow I don't see the tensorboard itself --- it just does not show up after the training. Howhewer, if I run '%tensorboard --logdir logs', I see a message: 'Reusing TensorBoard on port 6006 (pid 139), started 0:11:31 ago. (Use '!kill 139' to kill it.)'. Which means that the tensorboard is running, but just does not show up. Do you have any suggestions on what I should do to fix that? – user48115 Sep 27 '20 at 13:35
  • @user48115, Simple trick, please execute `%tensorboard --logdir logs` in the next cell in Colab. Don't include this part in the first cell for the execution. Please let me know, still are you facing issue? –  Sep 28 '20 at 09:05