jessevig / bertviz

BertViz: Visualize Attention in NLP Models (BERT, GPT2, BART, etc.)
https://towardsdatascience.com/deconstructing-bert-part-2-visualizing-the-inner-workings-of-attention-60a16d86b5c1
Apache License 2.0
6.87k stars 775 forks source link

Is there any way to "pin" the attention view for a single token? #129

Open allenyllee opened 8 months ago

allenyllee commented 8 months ago

Hi, thanks for your effort to develop this package! I wonder is there any way to "stick" or "pin" the attention weight view for a single token?

Currently, when I move the mouse pointer to hover a token, the attention view will only focus on connections for that token. But when I move the pointer out of that place, the view will turn in to "bird's eye view", show all the connection for all the tokens.

However, sometimes I need to show the connections for a specific token. When the sequence is so long, I have to scroll down the notebook, and move the pointer out of that token position, then I lost the focus view.

Is there any way to solve this?

Bachstelze commented 7 months ago

You could zoom out in the browser. That isn't the perfect solution, though could work around in some use cases. A better solution could be collapsable sections.

jamesanto commented 5 months ago

I was looking for the same feature, we can probably make the HTML events configurable, but for now you can work around it using something like below:

from IPython.display import HTML, display
html = head_view(
    decoder_attention=self_attn_weights,
    cross_attention=cross_attn_weights,
    encoder_tokens=src_seq,
    decoder_tokens=tgt_seq,
    html_action='return'   # Note: by default it is 'view', you need to change it to 'return' to get the HTML data
)

#  As of now the attention highlighting is done using mouseover and mouseleave events, I have replaced them with click and dblclick respectively. This would break if the underlying javascript changes, but as of now it works with version `1.4.0`
html = HTML(html.data.replace('mouseover', 'click').replace('mouseleave', 'dblclick'))
display(html)

@jessevig please let me know if you are open to making the events configurable. Maybe just a boolean toggle to switch between mouseover/mouseleave to click/dblclick.