Transformer Models
Transformer models are neural network architectures that use self-attention to process sequences in parallel rather than step by step. They power modern language models, machine translation, and many other tasks where understanding relationships between elements in a sequence is critical.
itArtificial intelligence and machine learning | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic — Transformer Models
A transformer is a neural-network architecture for finding useful relationships inside a sequence. It became famous through language models, but text is only one guest at this particular party. Image patches and audio frames can also arrive as a sequence, provided someone has given them labels and seats.
Earlier recurrent models read tokens in a line, carrying a summary forward like a relay runner with an increasingly alarming amount of luggage. Transformers use self-attention instead. Each token can compare itself with every other token directly, so a later word can connect with an earlier one without waiting for every intermediate step to preserve the message.
The comparison has three names: query, what a token seeks; key, what a token offers; and value, the information that can be carried forward. The resulting relevance scores decide how values are blended into a new token representation. Several such comparisons run at once in multi-head attention, because a single view of a sentence would be a remarkably bold career choice for a neural network.
There is a catch. Attention alone knows relationships, but not order. A token at the beginning and the same token later in a sequence look alike until positional encoding supplies location information. Modern models often use rotary positional encoding, which changes query and key vectors according to position. This is why word order did not vanish into a very expensive bag of tokens.
A full transformer stacks blocks. Each block combines multi-head attention, a feed-forward network, residual connections, and layer normalization. The familiar family split follows from how those blocks are arranged: encoder-only models build representations for understanding, decoder-only models generate one token at a time, and encoder-decoder models turn a full input into another sequence. The names are less mystical than they first appear, which is fortunate because there are already enough mysterious names in machine learning.
Start with the Intro for the architecture and its limits. Use Slides when the query-key-value flow needs a compact map, and keep the Cheatsheet nearby when comparing the three architecture families. The practice reference then turns one attention row into numbers you can inspect, while the exercise asks you to show exactly what changes when a key or position changes.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://arxiv.org/abs/1706.03762
Supports
- The transformer is based solely on attention mechanisms, dispensing with recurrence and convolutions entirely
- Self-attention computes relationships between all positions in a sequence in parallel
- Multi-head attention runs multiple attention computations in parallel to capture different relationship types
- The architecture is more parallelizable and requires significantly less time to train than recurrent architectures
- Each encoder/decoder layer contains a feed-forward network in addition to attention sub-layers
- Residual connections around each sub-layer followed by layer normalization
- The original design is an encoder-decoder architecture aimed at sequence-to-sequence tasks like translation
- The Transformer was introduced in 2017 with multi-head attention and positional encoding
- https://huggingface.co/learn/llm-course/chapter1/4
Supports
- Transformers use an encoder-decoder structure where the encoder builds a representation of input and the decoder generates a target sequence
- Encoder-only models (BERT) excel at understanding tasks such as classification and entity recognition
- Decoder-only models (GPT) focus on generation, predicting one token at a time
- Encoder-decoder models (T5) perform well on generative tasks requiring input context, such as translation or summarization
- https://huggingface.co/learn/llm-course/chapter1/5
Supports
- Most transformer implementations use variants of encoder-only, decoder-only, or encoder-decoder configurations
- Vision Transformer (ViT) converts images into patch sequences processed like text tokens
- Whisper converts raw audio to spectrograms, then processes them through an encoder-decoder architecture
- Encoder-decoder models like BART handle sequence-to-sequence work such as summarization
- https://huggingface.co/blog/designing-positional-encoding
Supports
- Self-attention is a set operation and is permutation equivariant, treating tokens identically regardless of position
- Positional encodings are added to token embeddings to indicate position, since transformers process all tokens in parallel
- Desirable properties for a positional encoding scheme include consistency across sequence lengths, generalization to longer sequences, and clean combination with attention
- Rotary Positional Encoding (RoPE) is the current widely adopted approach, encoding relative position by rotating query and key vectors as a function of position
- https://huggingface.co/docs/transformers/
Supports
- Hugging Face Transformers provides model definitions and pretrained model interfaces for transformer architectures
- https://docs.pytorch.org/docs/stable/generated/torch.nn.Transformer.html
Supports
- PyTorch provides a reference implementation of the original Transformer architecture
- https://jax.dev/
Supports
- JAX provides array programming and compilation for accelerator-oriented numerical computation
- https://www.tensorflow.org/
Supports
- TensorFlow provides an open-source framework for machine learning model construction and execution
- https://www.nvidia.com/en-us/ai-data-science/products/nemo/
Supports
- NVIDIA NeMo provides tooling for developing and customizing generative AI models
- https://arxiv.org/abs/1810.04805
Supports
- BERT introduced deep bidirectional transformer pre-training for language understanding tasks
- https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf
Supports
- Generative Pre-Training applies unsupervised language-model pre-training followed by discriminative fine-tuning
- https://arxiv.org/abs/1901.02860
Supports
- Transformer-XL introduces segment-level recurrence and relative positional encoding for longer contexts
- https://arxiv.org/abs/1910.10683
Supports
- T5 presents a unified text-to-text transfer-learning framework using an encoder-decoder transformer
- https://arxiv.org/abs/2001.04451
Supports
- Reformer uses locality-sensitive hashing attention and reversible layers to reduce long-sequence costs
- https://arxiv.org/abs/2010.11929
Supports
- Vision Transformer applies a transformer directly to sequences of image patches
- https://arxiv.org/abs/2005.14165
Supports
- GPT-3 studies large autoregressive language models and few-shot prompting
- https://arxiv.org/abs/2101.03961
Supports
- Switch Transformers use sparse expert routing in a transformer architecture
- https://arxiv.org/abs/2203.15556
Supports
- Chinchilla studies compute-optimal scaling between model size and training tokens
- https://research.google/blog/reformer-the-efficient-transformer/
Supports
- Dense attention has quadratic cost in sequence length and long contexts create memory pressure
- https://research.google/blog/rethinking-attention-with-performers/
Supports
- Efficient attention methods involve compatibility and representation tradeoffs rather than a cost-free replacement
