
Transformer: Is a seq-seq model based on self-attention that transforms input sequence into context aware token representations that model global and local dependencies. Originally made for translation.
The encoder converts input tokens into contextual representations using multi-head self-attention and a feed-forward network, with residual connections and layer normalization.
Transformer encoder
With n tokens and embedding dimension d_model, rows of X contain token embeddings plus positional information.
In the original post-normalization architecture, each sublayer is followed by a residual connection and layer normalization:
Encoder dimensions and the role of each operation
n is the number of tokens. Example embedding widths are 512, 768, and 1024; the selected width must be compatible with the head configuration.
QKᵀ contains a score for every token pair. Dividing by √dₖ controls score scale; very large logits can saturate softmax. Softmax normalizes each score row, and multiplication by V produces a weighted sum of value vectors.
For example, eight heads with d_model=512 use 64-dimensional keys and values per head. Twelve heads are another possible configuration when the width is chosen accordingly.
The output projection maps the concatenated heads back to the model width. Residual connections retain input information and provide a path for gradients. The same two-layer FFN is applied separately to every token position, in parallel; it does not flatten all tokens into one vector.
Both encoder sublayers preserve the n×d_model shape, so their residual additions are well defined.
Why Apply same FNN at every position?
- Parameter sharing just like CNNs
- Parallelization
- Attention already lets each token see the entire sequence (global information), FNN refines what the token already knows by applying non-linear transformations
Why positional Encodings? Transformers don’t have recurrence, they treat each input token as a set (not sequence). We add positional information to each token embedding at that position
Sinusoidal positional encoding
For d = 6, the three frequency denominators are approximately 1, 21.5443, and 464.1589. Add the positional vector to each token embedding.
| Token | Position | Embedding |
|---|---|---|
| I | 0 | [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] |
| love | 1 | [0.5, 0.4, 0.3, 0.2, 0.1, 0.0] |
| AI | 2 | [0.9, 0.8, 0.7, 0.6, 0.5, 0.4] |
| Token | Embedding + positional encoding |
|---|---|
| I | [0.10000, 1.20000, 0.30000, 1.40000, 0.50000, 1.60000] |
| love | [1.34147, 0.94030, 0.34640, 1.19892, 0.10215, 1.00000] |
| AI | [1.80930, 0.38385, 0.79270, 1.59569, 0.50431, 1.39999] |
Positional encoding: every component
| i | 2i/d with d=6 | 10000^(2i/d) |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 2/6 = 1/3 | 21.54434690 |
| 2 | 4/6 = 2/3 | 464.15888336 |
| Position | i | Denominator | Sine calculation | Cosine calculation |
|---|---|---|---|---|
| 0 | 0 | 1.000000 | sin(0/1.000000) = 0.00000000 | cos(0/1.000000) = 1.00000000 |
| 0 | 1 | 21.544347 | sin(0/21.544347) = 0.00000000 | cos(0/21.544347) = 1.00000000 |
| 0 | 2 | 464.158883 | sin(0/464.158883) = 0.00000000 | cos(0/464.158883) = 1.00000000 |
| 1 | 0 | 1.000000 | sin(1/1.000000) = 0.84147098 | cos(1/1.000000) = 0.54030231 |
| 1 | 1 | 21.544347 | sin(1/21.544347) = 0.04639922 | cos(1/21.544347) = 0.99892298 |
| 1 | 2 | 464.158883 | sin(1/464.158883) = 0.00215443 | cos(1/464.158883) = 0.99999768 |
| 2 | 0 | 1.000000 | sin(2/1.000000) = 0.90929743 | cos(2/1.000000) = -0.41614684 |
| 2 | 1 | 21.544347 | sin(2/21.544347) = 0.09269850 | cos(2/21.544347) = 0.99569422 |
| 2 | 2 | 464.158883 | sin(2/464.158883) = 0.00430886 | cos(2/464.158883) = 0.99999072 |
For position zero, all three sine terms are zero and all three cosine terms are one. Each positional vector is added component by component to its token embedding.
Rounding is deferred until display; the source’s final “AI” vector had small rounding/arithmetic differences. The final vectors above use the stated embeddings and sinusoidal formula.
The autoregressive decoder attends to previous output tokens and, in an encoder–decoder model, to the encoder’s representations.
Transformer decoder
The decoder receives the previous output tokens, shifted right during training. Causal self-attention prevents a position from seeing future tokens.
In encoder–decoder cross-attention, queries come from the decoder and keys and values come from encoder outputs. A feed-forward sublayer follows, with residual connections and normalization around the sublayers.
Decoder-only models such as GPT omit the encoder and encoder–decoder cross-attention.
Decoder projections and shapes
Let N be the number of target tokens and S the number of source tokens. The input contains the previously generated targets, shifted right during training.
For a single head, Z_self has N×d_v entries. Concatenating heads and applying the output projection restores N×d_model before the next sublayer.
Cross-attention retrieves source information using queries from the decoder and keys/values from encoder memory. The decoder FFN then operates independently at each target position:
Each row of P is a distribution over the vocabulary for one target position. The next-token distribution determines the probabilities of the possible next words.
Adding −∞ to future positions before softmax gives them zero attention weight.
Attention: Umbrella term, means to focus on different parts of input data when producing an output.
Cross-Attention: one sequence uses its elements as queries to attend and extract relevant information from another sequence by using their keys and values
Self-Attention: elements of a single sequence attend to each other to gather contextual information before generating the output
Causal masks allow all target positions to be processed together during teacher-forced training without revealing future tokens. Autoregressive inference still generates successive tokens using the available prefix.
