Repository navigation
Module 15
Level: Advanced
Status: Introductory technical chapter with supporting lessons and learning resources
This module is a self-contained learning unit within the larger Deep Learning curriculum. Its purpose is to build understanding from motivation and prerequisites through mathematical reasoning, implementation and critical evaluation.
Self-attention alone does not encode sequence order: permuting tokens permutes the outputs in the same way. Positional information supplies order or relative displacement. Different positional schemes make different trade-offs in extrapolation, efficiency and representation.
Compare learned and sinusoidal absolute positions with relative biases and rotary position embeddings. Understand how position affects queries, keys or attention logits. Consider long-context behaviour and the distinction between absolute and relative position.
By the end of this module, a reader should be able to define its central objects, explain the motivation behind its main methods, interpret the governing equations, work through a small example, and identify assumptions and limitations. The goal is understanding the mechanism—not memorizing a list of terms.
- Explain why position is needed.
- Derive sinusoidal encoding.
- Compare absolute and relative positions.
Read Module 14 first for scaled dot-product and multi-head attention; this module focuses on position and attention variants.
Useful background
Where these ideas are used next
- Module-15-Lesson-Map
- Module-15-Important-Lessons
- Module-15-Numericals-and-Exercises
- Module-15-Implementation-Roadmap
- Module-15-Research-Extensions
- Module-15-positional-encoding-modern-attention-variants
- Module-15-Lesson-01
- Module-15-Lesson-02
- Module-15-Lesson-03
- Module-15-Lesson-04
- Module-15-Lesson-05
- Module-15-Lesson-06
- Module-15-Lesson-07
- Module-15-Lesson-08
- Module-15-Lesson-09
- Module-15-Lesson-10
- Study the lessons in prerequisite order.
- Write a notation sheet and derive central equations.
- Solve at least one hand-worked example per major mechanism.
- Implement core operations where appropriate.
- Verify outputs and gradients.
- Complete conceptual and coding exercises.
- Record limitations and research extensions.
- Conceptual understanding
- Mathematical derivations
- Worked numerical examples
- NumPy implementation (where applicable)
- PyTorch implementation (where applicable)
- Tests and numerical verification
- Experiments and interpretation
- Revision and self-assessment
Self-attention alone does not encode token order. Positional methods inject sequence position or relative displacement into the representation or attention computation. Different schemes trade off learned flexibility, extrapolation and compatibility with attention variants.
Sinusoidal positional encoding: $$ PE(pos,2i)=\sin\left(pos/10000^{2i/d}\right), $$ $$ PE(pos,2i+1)=\cos\left(pos/10000^{2i/d}\right). $$
Rotary embeddings rotate query/key coordinate pairs by position-dependent angles before their dot product.
For two tokens with identical embeddings but different positions, adding distinct positional vectors makes their input representations different. Relative schemes instead make attention depend explicitly on the displacement between positions.
flowchart TD
A[Token embeddings] --> B{Position strategy}
B --> C[Absolute: add position vector]
B --> D[Relative: modify attention score]
B --> E[RoPE: rotate Q and K]
B --> F[ALiBi: add distance bias]
C --> G[Attention layers]
D --> G
E --> G
F --> G
Visualize positional vectors and pairwise attention biases for a short sequence. Test behaviour on sequence lengths longer than the training context and report extrapolation limitations.
Position encoding is not the same as token embedding. Learned absolute embeddings may be tied to a fixed context length. RoPE and ALiBi are not interchangeable; they modify attention in different ways.
Study RoPE scaling, YaRN, NTK-aware scaling, relative bias, ALiBi, position interpolation and long-context evaluation.
- Reproduce the smallest mathematical example by hand.
- Implement the central operation using NumPy.
- Implement the same operation using PyTorch.
- Compare outputs and gradients with numerical tolerances.
- Change one assumption or hyperparameter and document what changes.
- Record environment, random seed, data split, compute budget and limitations.
- Explain the motivation and assumptions without notes.
- Derive the principal equations and define every symbol.
- Complete at least one worked numerical example.
- Trace tensor shapes through the computation.
- Implement and test the central mechanism.
- Explain at least two failure modes.
- Read one primary paper and summarize its contribution and limitations.
Content status: This page now contains a technical chapter and a practical learning specification. The linked lesson pages provide focused subtopic treatment; experiments are not claimed complete until their result logs are added.
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths · ❓ Question bank · 📖 Glossary · 📚 References · ✏️ Contributing
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths
❓ Question bank · 📖 Glossary · 📚 References
Jump to module:
01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24