Introduction

This paper introduces an alternative state of the art architecture called the Transformer. Previously recurrent neural networks, long short-term memory and gated recurrent neural networks had been used to approach sequence modeling.

Recurrent models compute a hidden state say , as a function of the previous state and the input at that position . This inherently sequence natures precludes parallelization and also puts forwards memory constraints for longer sequences.

For more on sequence models you can refer to Sequence models.

Background

The goal of reducing sequential computation also forms the foundation of the Extended Neural GPU, ByteNet and ConvS2S, all of which use convolutional neural networks as basic building blocks and help in computing hidden states in parallel.

The problem with those CNN-based models: Even though they’re parallel, they still struggle to connect information between two positions that are far apart in the sequence. Think of a CNN layer as only “seeing” a small local window (its kernel size) at a time. To connect word 1 and word 100, you need to stack many layers so the “receptive field” grows enough to cover that distance.

  • For ConvS2S, the number of layers needed grows linearly with the distance between positions.
  • For ByteNet, it grows logarithmically (better, but still grows).

This matters because the more “hops” or operations needed to relate two distant positions, the harder it is for the model to learn dependencies between them — the signal has to pass through more transformations and can degrade.

How the Transformer fixes this: Self-attention lets any position directly attend to any other position in a single step — a constant number of operations, regardless of distance. Word 1 and word 100 are just as directly connected as word 1 and word 2.

This directness comes at a cost. When you attend to many positions at once, we compute weighted average of their representations. Averaging blurs things together, so we lose precision. To fix this we use Multi-Head Attention. Instead of doing one averaging operation, the model runs several attention “heads” in parallel, each learning to focus on different aspects/positions. This recovers the resolution lost from averaging, since different heads can specialize in different relationships rather than everything getting blended into one generic average.

Model Architecture

Here, the encoder maps an input sequence representations to a sequence of continuous representations . Given , the decoder then generates an output sequence of symbols one element at a time. At each step the model is auto-regressive, consuming the previously generated symbols as additional input when generating the next.

Encoder and Decoder Stacks

The encoder is composed of a stack of identical layers. Each layer has two sub-layers. The first is a multi-head self attention mechanism, and the second is a simple, position wise fully connected feed forward network.

We employ residual connection around each of the two sub-layers, followed by layer-normalization. That is, output of each sub-layer is

To facilitate the residual connections, all sub-layers in the model, as well as the embedding layers, produce outputs of dimensions .

The decoder is also composed of a stack of identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack. Here also we employ residual connections around each sub-layer followed by layer normalization.

We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.

Attention

An attention function takes a query, a set a key-value pairs, and produces an output. All of these are vectors. The output is a weighted sum of the values, where the weight given to each value comes from how well the query matches the corresponding key (a compatibility score).

We think of it like a soft lookup table. You have a query (“what am I looking for?”), a bunch of keys (“what does each item represent?”), and values (“the actual content of each item”). Instead of picking one exact match, we compute how similar the query is to every key, turn those similarities into weights, and blend all the values together according to there weights.

This is the specific compatibility function we use. Given queries and keys of dimension , the value of the dimension :

  1. MatMul (Q, K) : Dot product of the query with every key. Raw similarity scores.
  2. Scale : Divide by
  3. Mask (optional) : Used in the decoder to block attending to future positions.
  4. SoftMax : Converts scores into a probability distribution
  5. MatMul with V : Weighted sum of the value vectors using those probabilities.

Dividing by keeps the variance around 1 regardless of dimension, keeping softmax in a well-behaved region.

Instead of doing attention once with full dimensional , we linearly project , into different lower-dimensional sub-spaces, run scaled dot-product attention on each of these projections in parallel, then concatenate all the outputs and project once more.

Multiple head lets the model attend to different representation subspaces at different positions simultaneously. One head might learn to track syntactic dependencies, another might track co-reference, etc.

In this paper heads, and .

This table below shows the complexity for each layer type :

Layer TypeComplexity/LayerSequential OpsMax Path length
Self - Attention
Recurrent
Convolutional
Self-Attention (restricted)
  • Self-attention connects any two positions in one step but pays cost since it computes pairwise interaction between all positions.

Three ways attention is used in the Transformer

  1. Encoder-decoder attention : Queries come from the decoder, keys/values come from the encoder’s output. This lets every decoder position attend over the entire input sequence - this is the classic seq2seq attention mechanism.

  2. Encoder self-attention : all come from the same place i.e. the previous encoder layer. Every position can attend to every position in the input.

  3. Decoder self-attention (masked) : Same idea, but positions can only attend to earlier positions and itself, not future ones. This preserves the auto-regressive property.

Position-wise Feed-Forward Networks

Each encoder/decoder layer, in addition to attention, has a small feed forward network applied identically and independently to each position. This consists of two linear transformations with a activation in between :

This is just two linear layers with a in between. Input/output dimension is , and the linear hidden layer is much wider .

Embeddings and Softmax

Standard learned embeddings convert input/output tokens into dimensional vectors and a learned linear + softmax converts decoder output back into next-token probabilities.

Positional Encoding

Since there is no recurrence or convolution, the model has no inherent notion of token order. Self-attention treats the sequence like a bag/set. So we inject explicit position information by adding encoding vector to each input embedding, with the same dimension so they can be summed.

In this paper, we use sine and cosine functions of different frequencies :

where is the position and is the dimension. That is, each dimension of the positional encoding corresponds to a sinusoid. The wavelengths form a geometric progression from to .