I'm using tensorboard with keras this way:
from keras.callbacks import TensorBoard tensorboard = TensorBoard(log_dir = './logs', histogram_freq = 0, write_graph = True, write_images = False) # define model model.fit(X_train, Y_train, batch_size = batch_size, epochs = nb_epoch, validation_data = (X_test, Y_test), shuffle = True, callbacks = [tensorboard])
Updated July 21st, 2022
Before you can start using TensorBoard you have to install it either via pip or via conda
pip install tensorboard conda install - c conda - forge tensorboard
With TensorBoard installed, you can now load it into your Notebook. Note that you can use it in a Jupyter Notebook or Google’s Colab.
% load_ext tensorboard
Once that is done you have to set a log directory. This is where TensorBoard will store all the logs. It will read from these logs in order to display the various visualizations.
log_folder = 'logs'
You might want to clear the current logs so that you can write fresh ones to the folder. You can achieve that by running this command on Google Colab
!rm - rf / logs /
on Jupyter Notebooks
rm - rf logs
Last updated 2022-01-06 UTC.
This quickstart will show how to quickly get started with TensorBoard. The remaining guides in this website provide more details on specific capabilities, many of which are not included here.
# Load the TensorBoard notebook extension % load_ext tensorboard
# Load the TensorBoard notebook extension
%load_ext tensorboard
import tensorflow as tf
import datetime
import tensorflow as tf
import datetime
# Clear any logs from previous runs
rm - rf. / logs /
Downloading data from https: //storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376 / 11490434[ === === === === === === === === === === ] - 0 s 0 us / step
Place the logs in a timestamped subdirectory to allow easy selection of different training runs.
model = create_model()
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir = log_dir, histogram_freq = 1)
model.fit(x = x_train,
y = y_train,
epochs = 5,
validation_data = (x_test, y_test),
callbacks = [tensorboard_callback])
If you are building deep learning models, you may need to sit for hours (or even days) before you can see any real results. You may need to stop model training to change the learning rate, push training logs to the database for future use, or show the training progress in TensorBoard. It seems that we may need to do a lot of work to achieve these basic tasks—that's where TensorFlow callbacks come into the picture. ,This callback is handy in scenarios where the user wants to update the learning rate as training progresses. For instance, as the training progresses you may want to decrease the learning rate after a certain number of epochs. The LearningRateScheduler will let you do exactly that.,This callback allows us to save the model regularly during training. This is especially useful when training deep learning models which take a long time to train. This callback monitors the training and saves model checkpoints at regular intervals, based on the metrics.,As you can see in the output below, after the fourth epoch the learning rate has been reduced. verbose has been set to 1 to keep tabs on the learning rate.
To use any callback in the model training you just need to pass the callback object in the model.fit
call, for example:
model.fit(x, y, callbacks = list_of_callbacks)
This callback is used very often. This allows us to monitor our metrics, and stop model training when it stops improving. For example, assume that you want to stop training if the accuracy is not improving by 0.05; you can use this callback to do so. This is useful in preventing overfitting of a model, to some extent.
tf.keras.callbacks.EarlyStopping(monitor = 'val_loss',
min_delta = 0,
patience = 0,
verbose = 0,
mode = 'auto',
baseline = None,
restore_best_weights = False)
This callback allows us to save the model regularly during training. This is especially useful when training deep learning models which take a long time to train. This callback monitors the training and saves model checkpoints at regular intervals, based on the metrics.
tf.keras.callbacks.ModelCheckpoint(filepath,
monitor = 'val_loss',
verbose = 0,
save_best_only = False,
save_weights_only = False,
mode = 'auto',
save_freq = 'epoch')
For now we will see only one parameter, log_dir
, which is the path of the folder where you need to store the logs. To launch the TensorBoard you need to execute the following command:
tensorboard--logdir = path_to_your_logs
This callback is handy in scenarios where the user wants to update the learning rate as training progresses. For instance, as the training progresses you may want to decrease the learning rate after a certain number of epochs. The LearningRateScheduler
will let you do exactly that.
tf.keras.callbacks.LearningRateScheduler(schedule, verbose = 0)
Before logging anything, we need to create a writer instance. This can be done with:,Each subfolder will be treated as different experiments in tensorboard. Each time you re-run the experiment with different settings, you should change the name of the sub folder such as runs/exp2, runs/myexp so that you can easily compare different experiment settings. Type tensorboard runs to compare different runs in tensorboard.,At first, the package was named tensorboard, and soon there are issues about name confliction. The first alternative name came to my mind is tensorboard-pytorch, but in order to make it more general, I chose tensorboardX which stands for tensorboard for X.,Logging is cheap, but display is expensive. For my experience, if there are 3 or more experiments to show at a time and each experiment have, say, 50k points, tensorboard might need a lot of time to present the data.
from tensorboardX
import SummaryWriter
#SummaryWriter encapsulates everything
writer = SummaryWriter('runs/exp-1')
#creates writer object.The log will be saved in 'runs/exp-1'
writer2 = SummaryWriter()
#creates writer2 object with auto generated file name, the dir will be something like 'runs/Aug20-17-20-33'
writer3 = SummaryWriter(comment = '3x learning rate')
#creates writer3 object with auto generated file name, the comment will be appended to the filename.The dir will be something like 'runs/Aug20-17-20-33-3xlearning rate'
add_something(tag name, object, iteration number)