Attention Is All You Need
Attention Is All You Need
Background: Attention
- Attention: Module that provides a solution to the bottleneck problem of the original Seq2Seq
- Core Idea: At each time step of the decoder, allow the decoder to utilize a different part of the source sequence


-
(time step = 1) hidden state + input sequence from encoder → Calculate similarity(by dot product) with hidden state vectors of encoder from each time step - Becomes Attention scores
- Softmax → take some possibility vector Attention distribution
- Reflect Attention distribution on hidden state vectors of encoder as weight
- The weighted sum = Attention output
- Use it as additional input for first hidden state of decoder
- Repeat for each decoder hidden state
Benefits of Attention
- Attention significantly improves NMT performance
- Effective to allow the decoder to focus on particular part of the source sequence
- Attention solves the bottleneck problem
-
Allows the decoder to look directly at the source sequence
⇒ Allow the model to address the bottleneck problem
-
- Attention helps with vanishing gradient problem
- Provides a shortcut to faraway states
Motivation and Contribution
Motivation
- The high complexity of existing RNN- and CNN-based models and the correspondingly high computational cost
- In addition, RNNs require sequential computation, which makes parallelization difficult, and training becomes harder as the sequence length increases.
- The Attention mechanism was proposed to complement these RNN-based models, but it had only been used in conjunction with RNNs.
Proposal
- Accordingly, they propose the Transformer, a new model based on the Self-Attention mechanism.
Contribution
- The advent of the Transformer; the first sequence transduction model that relies entirely on Self-Attention, without any RNN or CNN structures.
- Sequences can be processed in parallel, greatly improving training speed.
- Overall performance is better than other models, and it achieves SOTA on translation tasks.
Model
Overall Architecture

-
Attention module can work as both a sequence encoder and a decoder in seq2seq with attention
⇒ RNN or CNN are no longer necessary, but all that is needed is attention modules
Encoder & Decoder
- Encoder:
- A stack of N=6 identically structured layers
- input sequence → Vector embedding
- Sub-layers:
- Multi-head self-attention mechanism
- input & output dimension are matched
- Feed-forward network (position-wise, fully connected)
- Multi-head self-attention mechanism
- Residual connections
-
Layer Normalization
\[\mathrm{LayerNorm}(x + \mathrm{sublayer}(x))\]
Layer Normalization

Two steps of layer normalization
- Normalization of each word vectors to have mean of zero and variance of one
- Affine transformation of each sequence vector with learnable parameters
- Decoder:
- A stack of N=6 identically structured layers
- Vector embedding → output sequence
- Sub-layers:
- Masked multi-head attention
- Prevent positions from attending to subsequent positions
- Multi-head attention
- Feed-forward network
- Masked multi-head attention
Self-Attention
Linear Transformation
- Embedded → Queries
- \(\bold{X} \times \bold{W}^Q ⇒ \bold{Q}\)
- Embedded → Keys
- \(\bold{X} \times \bold{W}^K ⇒ \bold{K}\)
- Embedded → Values
- \(\bold{X} \times \bold{W}^V ⇒ \bold{V}\)
Self-attention
- Unlike the conventional RNN, where the decoder’s hidden state acts as the query and the attention result is derived by taking a weighted sum over the encoder input x to find its similarity, here the input x itself integrates and performs the role of the query.
- One vector set work in three ways:
- Query: Decoder hidden state vector
- Key: Encoder input vector whose similarity with the query vector is calculated
- Value: Vector to find weighted sum by summation with softmax-ed weight
Scaled Dot-product Attention


- \(softmax({\bold{Q} \times \bold{K}^T \over \sqrt{d_k} })\) ⇒ probability matrix of attention weight vector which sums up to 1
- \(\times \bold{V}\) ⇒ result vector that best encoded the sequence of each query is contained in each row
- equals to the most similar hidden state vector
- \(d_k\): a dimension of key vectors

- Problem
- As \(d_k\) gets large, the variance of \(q^Tk\) increases
- Some values inside the softmax get large
-
The softmax gets very peaked
⇒ Gradient gets smaller!
- Solution
-
Scaled by the length of query / key vectors:
\[A(Q, K, V) = \operatorname{softmax}\left(\frac{QK^{T}}{\sqrt{d_k}}\right) V\]- Reason why \(\bold{Q} \times \bold{K}^T\) is divided by \(\sqrt{d_k}\)
-
Multi-head Attention

-
The input word vectors can be the queries, keys and values
⇒ The word vectors themselves select one another
-
Problem
: Only one way for words to interact with one another
-
Solution
: multi head attention maps \(Q, K\) and \(V\) into the \(h\) number of lower dimensional spaces via \(W\) matrices
- Multi sets of \((\bold{W}^Q, \bold{W}^K, \bold{W}^V)\)
- Afterwards, apply attention, then concatenate outputs and pipe through linear layer
Training
Dataset
- WMT 2014 English - German dataset
- about 4.5M sentence pairs
- WMT 2014 Englsih-French dataset
- 36M sentences encoded with a 32,000 word-piece vocabulary
Model Parameters
- Base Model:
- Layers (N): 6 encoder and 6 decoder layers.
- Model dimension (d_model): 512.
- Feed-forward network dimension (d_ff): 2048.
- Attention heads (h): 8.
-
Each attention head dimension: 64 ( \(d_{model}/h=512/8\)).
dmodel/h=512/8d_{model}/h = 512/8
- Dropout rate: 0.1.
- Big Model:
- Layers (N): 6 encoder and 6 decoder layers.
- Model dimension (d_model): 1024.
- Feed-forward network dimension (d_ff): 4096.
- Attention heads (h): 16.
- Each attention head dimension: 64.
- Dropout rate: 0.3
Training Parameters
- Optimizer: Adam with \(\beta_1 = 0.9,\beta_2 = 0.98, \epsilon = 10^{-9}\)
- Learning Rate:
-
Based on:
\[lrate = d_{\text{model}}^{-0.5} \cdot \min(step\_num^{-0.5}, step\_num \cdot warmup\_steps^{-1.5})\] -
warmup steps 4000
-
Hardware and Schedule
- 8 NVIDIA P100 GPUs
- Base Model: 0.4sec per training step, total 12 hours
- Big Model: 1.0sec per training step, total 3.5 days
Regularization
- Residual Dropout
- Label Smoothing
Results
Machine Translation
- For the WMT 2014 English-to-German translation task:
- The Transformer (big) model achieved a BLEU score of 28.4, surpassing the best previously reported models by more than 2.0 BLEU, including ensembles.
- The base model outperformed all previously published models and ensembles with a BLEU score of 27.3.
- For the WMT 2014 English-to-French translation task:
- The Transformer (big) model reached a BLEU score of 41.8, outperforming the best previously reported single models.
- This result was achieved at less than 1/4 of the training cost compared to the previous state-of-the-art model.
Model Variations
- The base model was varied to test the importance of different components:
- Reducing the number of attention heads (to 1) caused a 0.9 BLEU drop, while increasing the heads to 32 also led to a performance drop.
- Reducing the size of the attention keys negatively impacted model quality.
- Larger models performed better, with the big model achieving the highest scores.
English Constituency Parsing
- The Transformer generalized well to the English constituency parsing task.
- With no task-specific tuning, it achieved a competitive F1 score of 91.3 on the WSJ dataset.
- In a semi-supervised setting, it achieved an F1 score of 92.7, surpassing many previous models.