Attention Is All You Need: How Transformers Actually Work

Transformers Explained Visually

Attention Is All You Need: How Transformers Actually Work

The 2017 paper "Attention Is All You Need" introduced the Transformer, now the go-to architecture for GPT, Llama, and Gemini. This visual explainer walks through every piece of a text-generative Transformer using GPT-2 (124M parameters) as a concrete example: tokenization, embedding, positional encoding, multi-head self-attention, the MLP block, and the final softmax that predicts the next token. It also covers temperature, top-k, and top-p sampling, plus auxiliary features like layer normalization and residual connections.

The core innovation and power of Transformers lie in their use of self-attention mechanism, which allows them to process entire sequences and capture long-range dependencies more effectively than previous architectures.
  1. noncovalence

    Hm, I wonder why my laptop is suddenly pulling about 5 fps while I'm just browsing HN?

    The nefarious "2.2 GB RAM usage within 10 seconds" tab open in the background:

  2. jasonjmcghee

    For the uninitiated, I can't recommend enough, The Illustrated Transformer:

    https://jalammar.github.io/illustrated-transformer/

  3. andblac

    Nicely done. For me the most fascinating thing about attention heads is the place where Attention matrix is already computed and is getting multiplied by Value vector. It behaves exactly like pushing Value vector through Dense layer of ordinary network where Attention matrix forms weights of that layer. So attention head is trained to construct this small single layer network dynamically during inference from Key and Query. And that's the point. That's rarely underlined in explanations of LLMs architecture and for me it's quite amazing that it works so well. This mechanism easy to observe in this particular visualization if you click through it.

  4. throw0101a

    As someone with an EE degree (though a sysadmin), this use of the term "transformer" is constantly confusing. :)

    (Also "cryto" for cryptocurrency rather than cryptography.)

  5. robrenaud

    Regarding the temperature explanation:

    > "Instead of picking the highest-probability token, we can use different selection strategies to balance safety and creativity in the generated text".

    Safety is definitely the wrong word here.

    Temperature 0 generated text actually has a weird "lack of surprise" character that makes it seem artificial. [1]

    > "high-probability texts can be dull or repetitive. Humans use language as a means of communicating information, aiming to do so in a simultaneously efficient and error-minimizing manner; in fact, psycholinguistics research suggests humans choose each word in a string with this subconscious goal in mind."

    I'd completely drop the dropout explanation. It's just not part of the modern recipe anymore, AFAICT.

    As for the ambitious goal of explaining transformers with a single interactive visualization, I just have a hard time imagining a person is going to newly understand both word embeddings (word2vec blew my mind in 2014) and also gain an understanding of attention.

    I am making my own visualizations for a presentation on "Full Bandwidth Transformers"[2] that I am giving tomorrow at the Deep Learning Study Group (SF) (on zoom for the non-locals)[3]. It's not meant to be stand alone/context free, but I'd love some feedback.

    https://rrenaud.github.io/fullbandwidth_transformer_viz/

    [1] https://arxiv.org/abs/2202.00666

    [2] https://arxiv.org/abs/2608.08888

    [3] https://www.meetup.com/deep-learning-sf/events/316601593/

More from this day

2026-09-21