2

I'm new to tensorboard and learning the use of it following the tutorial, which goes well and tensorboard works as expected.

Referring to that tutorial, I wrote my own code to train a logic-and model with jupyter notebook

%load_ext tensorboard
import datetime
log_folder = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
import tensorflow as tf
import numpy as np

x_train = np.asarray([[0, 0],[0, 1],[1, 0],[1, 1]], np.float32)
y_train = np.asarray([0, 0, 0, 1], np.float32)

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])

def custom_loss(y,a):
    return -(y*tf.math.log(a) + (1-y)*tf.math.log(1-a))

model.compile(loss=custom_loss,
              optimizer='SGD',
              metrics=['accuracy'])

tensorboard_callback = tf.keras.callbacks.TensorBoard(log_folder, histogram_freq=1)

model.fit(x_train, y_train, epochs=2000, verbose=0,
          callbacks=[tensorboard_callback])

the training goes well and needs some improvement.

However, tensorboard shows nothing

%tensorboard --logdir log_folder

where is the key to make tensorboard work?

JJJohn
  • 915
  • 8
  • 26

1 Answers1

3

You're just using the ipython magic wrong. You need to put a dollar sign in front of your variable name (see e.g. How to pass a variable to magic ´run´ function in IPython).

%tensorboard --logdir $log_folder

For further exploring, try pretending you're working in the future (at least as far as date goes), and add a cell like this

log_folder_future = "logs/fit/" + (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d-%H%M%S")
up_dir = './' + '/'.join(log_folder_future.split('/')[:-1])
model.compile(loss=custom_loss,
              optimizer='SGD',
              metrics=['accuracy'])

tensorboard_callback_future = tf.keras.callbacks.TensorBoard(log_folder_future, histogram_freq=1)
model.fit(x_train, y_train, epochs=2500, verbose=0,
          callbacks=[tensorboard_callback_future])

and call like this

%tensorboard --logdir $up_dir

and end up with something like this tensorboard_two_dates

For more on tensorboard directory structures and multiple runs, see this page https://github.com/tensorflow/tensorboard/blob/master/README.md#runs-comparing-different-executions-of-your-model

Andrew Holmgren
  • 1,225
  • 1
  • 11
  • 18
  • Thank you so much! Your answer is quit helpful! Just one tiny question, why do I need to `- datetime.timedelta(days=1)` to name the directory with one day ago? – JJJohn Jul 27 '21 at 05:50
  • 1
    @yaojp Easiest way to subtract a day. You could also manually manipulate the string afterword, but then you're reinventing work that the datetime module was made to do. Staying with a datetime object gives you the advantages of the module. Another tip I forgot to mention, realistically you want to run the tensorboard magic before you fit and enable updates so that you can see your progress as it trains (you can always load up another ipython or make a call from command line too -- vscode has tensorboard integrated these days). – Andrew Holmgren Jul 27 '21 at 05:55