Abstract
The dominant sequence transduction models are based on complex recurrent or convolutional neural networks that include an encoder and a decoder. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train.
1. Introduction & Model Architecture
Recurrent models typically factor computation along the symbol positions of the input and output sequences. This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples. The Transformer follows an encoder-decoder structure using stacked and point-wise, fully connected layers, employing residual connections followed by around each sub-layer.
3.2 Scaled Dot-Product & Multi-Head Attention
We call our particular attention . The input consists of queries and keys of dimension , and values of dimension . We compute the dot products of the query with all keys, divide each by , and apply a softmax function to obtain the weights on the values. In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix .
Instead of performing a single attention function with -dimensional keys, values and queries, we found it beneficial to linearly project the queries, keys and values times with different, learned linear projections. allows the model to jointly attend to information from different representation subspaces at different positions, while decoder layers preserve the property.
3.5 Positional Encoding & Complexity Comparison
Since our model contains no recurrence and no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute position of the tokens. To this end, we add to the input embeddings using sine and cosine functions of different frequencies.