Study the paper
Attention Is All You Need
Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.
13 activities
At a glance
Introduction to the Transformer ArchitectureIntroduction to the Transformer Architecture
LessonThe Encoder-Decoder Structure of the Transformer
Introduction to the Transformer ArchitectureTraditional sequence transduction models rely on an encoder-decoder structure. The encoder maps an input sequence of symbol representations (x 1, u, x n) to a sequence of continuous representations mathbf z = (z 1, u, z n) . Given mathbf z , the decoder then generates an output sequence (y 1, u, y m) of symbols one element at a time, operating in an auto-regressive manner by consuming previously generated symbols as additional input.
LessonScaled Dot-Product Attention
Scaled Dot-Product AttentionScaled Dot-Product Attention Formulation
LessonScaled Dot-Product Attention
Scaled Dot-Product AttentionScaled Dot-Product Attention computes attention weights by taking the dot product of queries with keys, scaling them by the square root of the key dimension, applying a softmax function, and using the resulting weights to compute a weighted sum of values.
LessonMulti-Head Attention Mechanism
Multi-Head Attention MechanismInstead of performing a single attention function with d text model -dimensional queries, keys, and values, Multi-Head Attention projects them h times using different, learned linear projections to d k , d k , and d v dimensions, respectively. The attention function is then performed in parallel across these projected versions, yielding d v -dimensional output values.
LessonInjecting Sequence Order via Positional Encodings
Injecting Sequence Order via Positional EncodingsBecause the Transformer contains no recurrence and no convolution, it lacks an inherent sense of sequence order. To make use of the order of the sequence, we must inject information about the relative or absolute position of the tokens. This is achieved by adding "positional encodings" of dimension d text model to the input embeddings at the bottoms of the encoder and decoder stacks.
LessonSinusoidal Positional Encoding (Even Dimensions)
Injecting Sequence Order via Positional EncodingsSince the Transformer contains no recurrence or convolution, positional encodings are added to the input embeddings to inject information about the relative or absolute position of tokens in the sequence.
LessonAnalyzing Computational Complexity and Path Lengths
Comparing Self-Attention, Recurrent, and Convolutional LayersSelf-attention layers connect all positions with a constant number of sequentially executed operations ( O(1) ), whereas recurrent layers require O(n) sequential operations. In terms of computational complexity, self-attention layers are faster than recurrent layers when the sequence length n is smaller than the representation dimensionality d , which is typical for state-of-the-art machine translation models.
LessonLimitations of Recurrent Neural Networks in Sequence Modeling
Comparing Self-Attention, Recurrent, and Convolutional LayersIn sequence modeling, recurrent neural networks process tokens sequentially, which creates a fundamental computational bottleneck. Because the computation of hidden state h t depends on h t-1 , operations cannot be easily parallelized across the sequence length n . This results in O(n) sequential operations, as opposed to O(1) for self-attention layers.
LessonEmpirical Performance on Translation and Parsing
Empirical Performance and GeneralizationOn the WMT 2014 English-to-German translation task, the big Transformer model achieves a state-of-the-art BLEU score of 28.4, outperforming previous models and ensembles by more than 2.0 BLEU. The base model also outperforms previous models at a fraction of the training cost (requiring only 3.3 cdot 10^ 18 FLOPs compared to over 10^ 19 or 10^ 20 FLOPs for competitive architectures).
LessonTable 2: BLEU scores and training costs for translation tasks
Empirical Performance and GeneralizationThe Transformer model achieves state-of-the-art BLEU scores on the WMT 2014 English-to-German (EN-DE) and English-to-French (EN-FR) translation tasks while requiring significantly lower training costs compared to previous recurrent or convolutional architectures.
LessonTable 4: English constituency parsing results
Empirical Performance and GeneralizationThe Transformer generalizes well to English constituency parsing, achieving strong performance on Section 23 of WSJ in both discriminative (WSJ only) and semi-supervised settings.
QuizTest your understanding
Transformer Architecture Assessment12 questions grounded in this paper section.