Tensorboard visualization don't appear in google collab

Viewed 223

I am implementing a simple linear regression code in google collab and trying to visualize the results with tensorboard with the following command %tensorboard --logdir=/tmp/lr-train.

However, when I run this command, the tensorboard just simply does not show up. Instead I just see the following message Reusing TensorBoard on port 6012 (pid 3219), started 0:07:06 ago. (Use '!kill 3219' to kill it.)

How to launch the tensorboard in my case? Here is the code I am trying to run:

%tensorflow_version 1.x
import tensorflow as tf
import numpy as np

N = 100
x_zeros = np.random.multivariate_normal(
                            mean=np.array((-1, -1)), cov = 0.1 * np.eye(2), size = (N//2))
y_zeros = np.zeros((N//2,))
x_ones = np.random.multivariate_normal(mean=np.array((1, 1)), cov = 0.1 * np.eye(2), size=(N//2))
y_ones = np.zeros((N//2))
x_np = np.vstack([x_zeros, x_ones])
y_np = np.concatenate([y_zeros, y_ones])

with tf.name_scope("placeholders"):
  x = tf.placeholder(tf.float32, (N, 2))
  y = tf.placeholder(tf.float32, (N, 1))
with tf.name_scope("weights"):
  W = tf.Variable(tf.random_normal((2, 1)))
  b = tf.Variable(tf.random_normal((1, )))
with tf.name_scope("prediction"):
  y_pred = tf.matmul(x, W) + b
with tf.name_scope("loss"):
  l = tf.reduce_sum((y - y_pred)**2)
with tf.name_scope("optim"):
  train_op = tf.train.AdamOptimizer(0.05).minimize(l)
with tf.name_scope("summaries"):
  tf.summary.scalar("loss", l)
  merged = tf.summary.merge_all()

train_writer = tf.summary.FileWriter('/tmp/lr-train', tf.get_default_graph())

n_steps = 100
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  # Train model
  for i in range(n_steps):
    feed_dict = {x: x_np, y: y_np.reshape(-1,1)}
    _, summary, loss = sess.run([train_op, merged, l], feed_dict=feed_dict)
    if i%10 == 0:
      print("step %d, loss: %f" % (i, loss))

I tried an example in tf-2, and tensorboard launched without any issues with the same command.

2 Answers

I tried your code in Colab and was able to reproduce what you mentioned and found a solution that worked as described below.

Use a ”space” between —logdir and /tmp/lr-train instead of a =.

What did not work as mentioned in the question:

%load_ext tensorboard
%tensorboard --logdir=/tmp/lr-train

What did work:

%load_ext tensorboard
%tensorboard --logdir /tmp/lr-train

You can also terminate your active session and then rerun all cells afterward. But maybe you even found a way of killing a tensorboard within a session.

Related