Skip to content
Tech News
← Back to articles

Show HN: LLM Attention Visualization

read original get Build a Large Language Model (From Scratch)" by Sebastian Raschka → more articles
Why This Matters

A developer-built interactive tool renders transformer attention weights as token opacity, letting anyone hover over generated text and see which prior tokens the model drew from. It's a small but useful piece of interpretability tooling that makes an abstract mechanism tangible, including a plausible explanation for why LLMs copy text verbatim so reliably.

Key Takeaways
Worth a Look

Build a Large Language Model (From Scratch)" by Sebastian Raschka — If watching attention weights light up across tokens made you curious how the machinery actually works, this book walks you through coding a transformer step by step, including the self-attention mechanism being visualized here. It's a hands-on path from tokenization to a working model you can inspect yourself.

See Build a Large Language Model (From Scratch)" by Sebastian Raschka on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.

One interesting thing about transformer-based large language models are that, during the generation phase, it is able to draw information from any of its previous tokens. But it needs to be selective; if every token affects the generation equally, it won't be very effective. This process needs a mechanism to decide how much a token affects the next token.

Turns out, we can visualize this mechanism!

You can tap or hover over any of the generated tokens to see the past tokens that affected* the generation.

Loading... (JavaScript required)

* "Affected" might not be fully accurate, as this visualization is highly simplified. It's calculating the attention weight, scaled by the magnitude of the value vector, aggregated across all attention heads, and summed across all layers. This is then used to control the opacity of the previous tokens. The largest values always have an opacity of 1 and the rest are interpolated.

A lot of information had to be thrown away to limit the visualization to just one numeric value per past token. Because of that, when I started implementing this, I actually thought it might not be comprehensible. But it actually can produce some interesting patterns!

For example, in the default "Office Move Summary" prompt, you can hover over the text that are copied verbatim like the address and dates. You can then see the original data stand out quite a bit, because the generated token takes up a lot of the information from the source data.

This addresses one thing that I've previously found unintuitive about LLMs. If they work by predicting the next tokens probabilistically, why are they somehow so good at copy-pasting stuff? Won't they eventually make a mistake just by random chance?

But with this mechanism, you can see that it doesn't predict the entire sequence from some limited internal states. Since it has access to all past tokens, it can just decide which past tokens to draw from when copying, and so the probability of errors can be very low. In the "Debugging an Average Function" example, you can see that this quite small model (600 million parameters) can easily reproduce an entire JS function except for the intended modification. (Although it's not actually capable of finding the issue by itself, so it needed some hints.)

Another interesting part is when you hover over the "remain" in "Existing access cards and phone numbers remain" in the "Office Move Summary" prompt. You can see that it draws from "work" in "Existing employee access cards will work" and "stay the same" in "company phone numbers will stay the same". So it's kind of combining the information from the words in both phrases, which I find quite cool.

... continue reading