callbacks.tensorboard

Callbacks that saves the tracked metrics during training and output logs for tensorboard to read

Tensorboard

Load tensorboard magic command to show tensorboard embed in Jupyter Notebook.

  1. %load_ext tensorboard

class LearnerTensorboardWriter[source][test]

LearnerTensorboardWriter(learn:Learner, base_dir:Path, name:str, loss_iters:int=25, hist_iters:int=500, stats_iters:int=100) :: LearnerCallback No tests found for LearnerTensorboardWriter. To contribute a test please refer to this guide and this discussion.

Broadly useful callback for Learners that writes to Tensorboard. Writes model histograms, losses/metrics, and gradient stats.

LearnerTensorboardWriter

First let’s show an example of use, with a training on the MovieLens sample dataset.

  1. path = untar_data(URLs.ML_SAMPLE)
  2. ratings = pd.read_csv(path/'ratings.csv')
  3. series2cat(ratings, 'userId', 'movieId')
  4. data = CollabDataBunch.from_df(ratings, seed=42)
  5. learn = collab_learner(data, n_factors=30, y_range = [0, 5.5])

Specify log path for tensorboard to read from. Then append callback partial to learner callback functions.

  1. project_id = 'projct1'
  2. tboard_path = Path('data/tensorboard/' + project_id)
  3. learn.callback_fns.append(partial(LearnerTensorboardWriter,
  4. base_dir=tboard_path,
  5. name='run1'))

run tensorboard magic command with logdir parameter. Default port is 6006.

  1. %tensorboard --logdir=$tboard_path --port=6006

Or you can launch the Tensorboard server from shell with tensorboard --logdir=data/tensorboard/project1 --port=6006 then navigate to http://localhost:6006

  1. learn.fit(10)

Calback methods

You don’t call these yourself - they’re called by fastai’s Callback system automatically to enable the class’s functionality.

on_train_begin[source][test]

on_train_begin(**kwargs:Any) No tests found for on_train_begin. To contribute a test please refer to this guide and this discussion.

To initialize constants in the callback.

on_batch_end[source][test]

on_batch_end(last_loss:Tensor, iteration:int, train:bool, **kwargs) No tests found for on_batch_end. To contribute a test please refer to this guide and this discussion.

Callback function that writes batch end appropriate data to Tensorboard.

on_backward_end[source][test]

on_backward_end(iteration:int, train:bool, **kwargs) No tests found for on_backward_end. To contribute a test please refer to this guide and this discussion.

Callback function that writes backward end appropriate data to Tensorboard.

on_epoch_end[source][test]

on_epoch_end(last_metrics:MetricsList, iteration:int, **kwargs) No tests found for on_epoch_end. To contribute a test please refer to this guide and this discussion.

Callback function that writes epoch end appropriate data to Tensorboard.


Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021