Skip to content
StackDecode
AI & ML Atlas
Deep LearningarchitectureInteractive simulation

Transformer Architecture

A sequence model built on attention, processing all positions in parallel.

The idea

Each layer lets every token look at every other token through self-attention and then transforms each token with a feed-forward network, with residual connections and layer normalisation around both. Position information is added to the embeddings. Because there is no recurrence, training parallelises well, which let models scale to billions of parameters.

Attention(Q, K, V) = softmax(QKᵀ/√dₖ)·V

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

thecatsatonthemat
  • positive
  • negative
  • attention weight

Take the sentence. We will compute what the word "cat" (highlighted) should pay attention to.

1/4

The vectors here are random, so the pattern of attention is arbitrary. In a trained model these weights are learned. Pick another query word with the slider.

Step by step

  1. 1Turn tokens into embeddings and add positional information.
  2. 2Self-attention: each token gathers information from the others.
  3. 3Add and normalise (residual connection).
  4. 4Feed-forward network on each token, then add and normalise again.
  5. 5Stack many layers; use the final states for prediction.

Where it is used

Language models, translation, vision, speech, protein folding.

Watch out for

Attention cost grows with the square of the sequence length.

Related