Write an interactive visualization code of self-attention of Transformer in HTML and JavaScript without any libraries.
code
Use an example sentence: "The quick brown fox"
When hovering over a token, visualize the edges whose thicknesses are proportional to the attention score.
The edges should be curved and should not overlap each other.
Make sure that the edges start and end from each token's center.
When clicked on a token, show the value of attention scores. Visualize this nicely in a LaTeX-rendered vector notation. Put each token next to the score. Make sure the LaTeX rendering is well-aligned.
When clicked again, remove the attention score vector.
Have vertical spacing of 50px at the top.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is the HTML and JavaScript code for an interactive visualization of self-attention in a Transformer model, using the example sentence "The quick brown fox". This code does not use any external libraries and fulfills all the requirements you've specified.
Sentence Tokens: The sentence "The quick brown fox" is broken into individual tokens, each displayed as a clickable and hoverable element.
Hover Interactivity: When you hover over a token, curved edges appear connecting it to other tokens. The thickness of each edge is proportional to the attention score between the tokens. The curves are designed not to overlap and start/end from each token's center.
Click Interactivity: Clicking on a token displays or hides the attention score vector. The vector is displayed in a LaTeX-like notation, with each token next to its corresponding attention score, aligned neatly in a table format to simulate vector notation.
Styling:
Vertical Spacing: There is a 50px margin at the top of the container to provide the required vertical spacing.
Token Styling: Tokens change background color on hover to enhance interactivity.
Vector Notation: The vector is styled using CSS to resemble LaTeX-rendered notation with brackets created using pseudo-elements.
How to Use:
Hover Over Tokens: Move your mouse over any of the tokens ("The", "quick", "brown", "fox") to see the edges appear.
View Attention Scores: Click on a token to display its attention scores. The scores will appear below the tokens in a styled vector format. Click again to hide the scores.
Note: This code does not rely on any external libraries and avoids the use of LaTeX rendering engines. Instead, it simulates the vector notation using HTML and CSS for alignment and styling.
Feel free to copy this code into an HTML file and open it in a web browser to see the interactive visualization in action.