jessevig/bertviz

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

Opened this issue · 2 comments

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?

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.

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.