Tensorboard

Open In Colab

Integration with tensorboard

  1. /usr/local/lib/python3.8/dist-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  2. return torch._C._cuda_getDeviceCount() > 0

First thing first, you need to install tensorboard with

  1. pip install tensorboard

Then launch tensorboard with

  1. tensorboard --logdir=runs

in your terminal. You can change the logdir as long as it matches the log_dir you pass to TensorBoardCallback (default is runs in the working directory).

Tensorboard Embedding Projector support

Tensorboard Embedding Projector is currently only supported for image classification

Export Image Features during Training

Tensorboard Embedding Projector is supported in TensorBoardCallback (set parameter projector=True) during training. The validation set embeddings will be written after each epoch.

  1. cbs = [TensorBoardCallback(projector=True)]
  2. learn = cnn_learner(dls, resnet18, metrics=accuracy)
  3. learn.fit_one_cycle(3, cbs=cbs)

Export Image Features during Inference

To write the embeddings for a custom dataset (e. g. after loading a learner) use TensorBoardProjectorCallback. Add the callback manually to the learner.

  1. learn = load_learner('path/to/export.pkl')
  2. learn.add_cb(TensorBoardProjectorCallback())
  3. dl = learn.dls.test_dl(files, with_labels=True)
  4. _ = learn.get_preds(dl=dl)

If using a custom model (non fastai-resnet) pass the layer where the embeddings should be extracted as a callback-parameter.

  1. layer = learn.model[1][1]
  2. cbs = [TensorBoardProjectorCallback(layer=layer)]
  3. preds = learn.get_preds(dl=dl, cbs=cbs)

Export Word Embeddings from Language Models

To export word embeddings from Language Models (tested with AWD_LSTM (fast.ai) and GPT2 / BERT (transformers)) but works with every model that contains an embedding layer.

For a fast.ai TextLearner or LMLearner just pass the learner - the embedding layer and vocab will be extracted automatically:

  1. dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
  2. learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
  3. projector_word_embeddings(learn=learn, limit=2000, start=2000)

For other language models - like the ones in the transformers library - you’ll have to pass the layer and vocab. Here’s an example for a BERT model.

  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
  3. model = AutoModel.from_pretrained("bert-base-uncased")
  4. # get the word embedding layer
  5. layer = model.embeddings.word_embeddings
  6. # get and sort vocab
  7. vocab_dict = tokenizer.get_vocab()
  8. vocab = [k for k, v in sorted(vocab_dict.items(), key=lambda x: x[1])]
  9. # write the embeddings for tb projector
  10. projector_word_embeddings(layer=layer, vocab=vocab, limit=2000, start=2000)

class TensorBoardBaseCallback[source]

TensorBoardBaseCallback() :: Callback

Basic class handling tweaks of the training loop by changing a Learner in various events

class TensorBoardCallback[source]

TensorBoardCallback(log_dir=None, trace_model=True, log_preds=True, n_preds=9, projector=False, layer=None) :: TensorBoardBaseCallback

Saves model topology, losses & metrics for tensorboard and tensorboard projector during training

class TensorBoardProjectorCallback[source]

TensorBoardProjectorCallback(log_dir=None, layer=None) :: TensorBoardBaseCallback

Extracts and exports image featuers for tensorboard projector during inference

projector_word_embeddings[source]

projector_word_embeddings(learn=None, layer=None, vocab=None, limit=-1, start=0, log_dir=None)

Extracts and exports word embeddings from language models embedding layers

TensorBoardCallback

  1. from fastai.vision.all import Resize, RandomSubsetSplitter, aug_transforms, cnn_learner, resnet18
  1. path = untar_data(URLs.PETS)
  2. db = DataBlock(blocks=(ImageBlock, CategoryBlock),
  3. get_items=get_image_files,
  4. item_tfms=Resize(128),
  5. splitter=RandomSubsetSplitter(train_sz=0.1, valid_sz=0.01),
  6. batch_tfms=aug_transforms(size=64),
  7. get_y=using_attr(RegexLabeller(r'(.+)_d+.*$'), 'name'))
  8. dls = db.dataloaders(path/'images')
  1. learn = cnn_learner(dls, resnet18, metrics=accuracy)
  1. learn.unfreeze()
  2. learn.fit_one_cycle(3, cbs=TensorBoardCallback(Path.home()/'tmp'/'runs'/'tb', trace_model=True))
epochtrain_lossvalid_lossaccuracytime
04.9732945.0096700.08219200:03
14.3827694.4382820.09589000:03
23.8771723.6658550.17808200:04

Projector

Projector in TensorBoardCallback

  1. path = untar_data(URLs.PETS)
  1. db = DataBlock(blocks=(ImageBlock, CategoryBlock),
  2. get_items=get_image_files,
  3. item_tfms=Resize(128),
  4. splitter=RandomSubsetSplitter(train_sz=0.05, valid_sz=0.01),
  5. batch_tfms=aug_transforms(size=64),
  6. get_y=using_attr(RegexLabeller(r'(.+)_d+.*$'), 'name'))
  7. dls = db.dataloaders(path/'images')
  1. cbs = [TensorBoardCallback(log_dir=Path.home()/'tmp'/'runs'/'vision1', projector=True)]
  2. learn = cnn_learner(dls, resnet18, metrics=accuracy)
  1. learn.unfreeze()
  2. learn.fit_one_cycle(3, cbs=cbs)
epochtrain_lossvalid_lossaccuracytime
05.1433226.7367270.08219200:03
14.5081005.1065800.10958900:03
24.0578894.1946020.06849300:03

TensorBoardProjectorCallback

  1. path = untar_data(URLs.PETS)
  1. db = DataBlock(blocks=(ImageBlock, CategoryBlock),
  2. get_items=get_image_files,
  3. item_tfms=Resize(128),
  4. splitter=RandomSubsetSplitter(train_sz=0.1, valid_sz=0.01),
  5. batch_tfms=aug_transforms(size=64),
  6. get_y=using_attr(RegexLabeller(r'(.+)_d+.*$'), 'name'))
  7. dls = db.dataloaders(path/'images')
  1. files = get_image_files(path/'images')
  2. files = files[:256]
  1. dl = learn.dls.test_dl(files, with_labels=True)
  1. learn = cnn_learner(dls, resnet18, metrics=accuracy)
  2. layer = learn.model[1][0].ap
  3. cbs = [TensorBoardProjectorCallback(layer=layer, log_dir=Path.home()/'tmp'/'runs'/'vision2')]
  1. _ = learn.get_preds(dl=dl, cbs=cbs)

projector_word_embeddings

fastai text or lm learner

  1. from fastai.text.all import TextDataLoaders, text_classifier_learner, AWD_LSTM
  1. dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
  2. learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
  1. projector_word_embeddings(learn, limit=1000, log_dir=Path.home()/'tmp'/'runs'/'text')

transformers

GPT2

  1. from transformers import GPT2LMHeadModel, GPT2TokenizerFast
  2. tokenizer = GPT2TokenizerFast.from_pretrained('gpt2')
  3. model = GPT2LMHeadModel.from_pretrained('gpt2')
  4. layer = model.transformer.wte
  5. vocab_dict = tokenizer.get_vocab()
  6. vocab = [k for k, v in sorted(vocab_dict.items(), key=lambda x: x[1])]
  7. projector_word_embeddings(layer=layer, vocab=vocab, limit=2000, log_dir=Path.home()/'tmp'/'runs'/'transformers')

BERT

  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
  3. model = AutoModel.from_pretrained("bert-base-uncased")
  4. layer = model.embeddings.word_embeddings
  5. vocab_dict = tokenizer.get_vocab()
  6. vocab = [k for k, v in sorted(vocab_dict.items(), key=lambda x: x[1])]
  7. projector_word_embeddings(layer=layer, vocab=vocab, limit=2000, start=2000, log_dir=Path.home()/'tmp'/'runs'/'transformers')
  1. warning: Embedding dir exists, did you set global_step for add_embedding()?

Validate results in tensorboard

Run the following command in the command line to check if the projector embeddings have been correctly wirtten:

  1. tensorboard --logdir=~/tmp/runs

Open http://localhost:6006 in browser (TensorBoard Projector doesn’t work correctly in Safari!)


Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021