A Visual Analysis Tool to Explore Learned Representations in Transformers Models
APACHE-2.0 License
by Ben Hoover, Hendrik Strobelt, Sebastian Gehrmann from IBM Research and Harvard NLP
Link to pre-paper and demo: exbert.net
exBERT is a tool that enables users to explore the learned attention weights and contextual representations of different huggingface Transformer models. Input a sentence, and exBERT will pass the tokenized input through the specified model, visualizing attentions as curved lines and allowing the embeddings to be searchable across an annotated corpus, if available.
Features:
[CLS]
and [SEP]
tokens that often assume the functionality of a no-operation
of a particular head.Limitations:
Support for visualizing the attentions only is available out of the box. This minimal visualization is available with select deployed models on Huggingface's model page here.
Significant preprocessing needs to be performed to allow corpus searching. Please see the instructions here
Simply run make env
from the root directory
conda env create -f environment.yml
. This will create an environment named exbert
.conda env update -f environment-dev.yml
conda activate exbert
pip install -e server/transformers
pip install -e server/spacyface
pip install -e server
python -m spacy download en_core_web_sm
Out of the box configuration is the same as the demo at [www.exbert.net].
conda activate exbert
python server/main.py
Limitations:
AutoModelWithLMHead
modeling_auto.py
of the original Transformers repo (as of v2.8):The model class to instantiate is selected as the first pattern matching
in the `pretrained_model_name_or_path` string (in the following order):
- contains `t5`: T5Model (T5 model)
- contains `distilbert`: DistilBertModel (DistilBERT model)
- contains `albert`: AlbertModel (ALBERT model)
- contains `camembert`: CamembertModel (CamemBERT model)
- contains `xlm-roberta`: XLMRobertaModel (XLM-RoBERTa model)
- contains `roberta`: RobertaModel (RoBERTa model)
- contains `bert`: BertModel (Bert model)
- contains `openai-gpt`: OpenAIGPTModel (OpenAI GPT model)
- contains `gpt2`: GPT2Model (OpenAI GPT-2 model)
- contains `transfo-xl`: TransfoXLModel (Transformer-XL model)
- contains `xlnet`: XLNetModel (XLNet model)
- contains `xlm`: XLMModel (XLM model)
- contains `ctrl`: CTRLModel (Salesforce CTRL model)
Create the reference corpus. Instructions here. Warning: Depending on the number of layers and size of the hidden dimension in the model, this step could take many gigabytes on your computer to store the hidden representations and attentions at every layer.
Run python server/main.py --model PATH_TO_MODEL --kind {bidirectional | autoregressive} --corpus PATH_TO_CORPUS
, selecting bidirectional if your model was trained with masking (e.g., BERT) and autoregressive otherwise (e.g., GPT2). PATH_TO_CORPUS
should be the name of the directory containing data.hdf5
, embedding_faiss/
and context_faiss
.
Because exBERT lazy-loads the large annotated corpus and models, the first call to search across the corpus will be slow.
cd client/src
npm install #installs all necessary node packages
npm run build #This will create the static files living in `client/dist`.
You can run a client server that automatically recompiles the frontend with npm run watch
rather than npm run build
. After making a change, you should be able to refresh the browser window to see your most recent changes.
Because the backend has to load in a lot of data for inference, we do not allow auto-backend refresh on every saved change.
This project was inspired in part by the original BertViz by Jesse Vig.
This project makes use of two public pip repositories (transformers
and spacyface
), both of which needed modification as this project was being developed. The git-subrepo
tool was used to achieve this workflow with a forked repository of both transformers and spacyface. However, this introduces the following steps when setting up the environment:
transformers/
directory, run pip install -e .
spacyface/
directory.No module named '_swigfaiss'
error, check that libomp
is installed on your system. If you are on a mac, this is as simple as brew install libomp
.