Papers · Architectures and attention · 2017

Attention Is All You Need

Ashish Vaswani, Noam Shazeer, Niki Parmar and others

The notation of the Transformer paper, explained: its 5 displayed equations, then every symbol it uses, grouped by category and shown the way the paper writes it.

Read the paper on arXiv

Reading this paper

The paper that introduced the Transformer: a model built from attention alone, with no recurrence. Its central formula is Attention⁡(Q,K,V)=softmax⁡(QKT/dk)V\operatorname{Attention}(Q, K, V) = \operatorname{softmax}(QK^T/\sqrt{d_k})V, decoded term by term on its own page (linked below).

Two things trip readers up. The matrices are written unbolded, so QQ, KK and VV look like scalars but are matrices with one row per token. And in WiQW_i^Q the superscript is a label (the query projection of head ii), not a power. The sizes are named dmodel=512d_{\text{model}} = 512, dkd_k, dvd_v and h=8h = 8 heads, and the positional encoding PE(pos,2i)PE_{(pos, 2i)} uses pospos and ii as plain italic names, not products of letters.

Read from arXiv's HTML version: 5 displayed equations and 96 pieces of inline math. 29 symbols and constructs from the catalog, read as attention and transformers.

The paper's displayed equations

Open any of them in the decoder for a symbol-by-symbol reading.

  1. (1)
    Attention(Q,K,V)=softmax(QKTdk)V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V
    Decode →
  2. MultiHead(Q,K,V)=Concat(head1,...,headh)WOwhere headi=Attention(QWiQ,KWiK,VWiV)\begin{gathered}\mathrm{MultiHead}(Q,K,V) =\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O} \\ \text{where}~\mathrm{head_{i}} =\mathrm{Attention}(QW^{Q}_{i},KW^{K}_{i},VW^{V}_{i})\end{gathered}
    Decode →
  3. (2)
    FFN(x)=max⁡(0,xW1+b1)W2+b2\mathrm{FFN}(x)=\max(0,xW_{1}+b_{1})W_{2}+b_{2}
    Decode →
  4. PE(pos,2i)=sin(pos/100002i/dmodel)PE(pos,2i+1)=cos(pos/100002i/dmodel)\begin{gathered}PE_{(pos,2i)}=sin(pos/10000^{2i/d_{\text{model}}}) \\ PE_{(pos,2i+1)}=cos(pos/10000^{2i/d_{\text{model}}})\end{gathered}
    Decode →
  5. (3)
    lrate=dmodel−0.5⋅min⁡(step_num−0.5,step_num⋅warmup_steps−1.5)lrate=d_{\text{model}}^{-0.5}\cdot\min({step\_num}^{-0.5},{step\_num}\cdot{warmup\_steps}^{-1.5})
    Decode →

Famous equations in this paper

Symbols, by category

Most used first. The examples are written as the paper writes them.

Notation conventions

  1. dd, WW, xx

    Italic letters (variables) 163 uses

    A plain italic letter is a variable; conventions say what kind (x input, y target, W weights…).

  2. dkd_{k}, WiQW^{Q}_{i}, WiKW^{K}_{i}

    Subscript (index or label) 81 uses

    A subscript picks an element (xᵢ), a time step (h_t), a named copy (W_q), or the parameters (p_θ).

  3. WOW^{O}, WiQW^{Q}_{i}, WiKW^{K}_{i}

    Superscript (power or label) 33 uses

    Usually a power (x² = x·x), but a capital or a word up there is often just a label (W^Q).

  4. PEPE, pospos

    Multi-letter names 7 uses

    A run of italic letters like pos or model is usually one name, not a product of single letters.

Greek letters

  1. ϵ\epsilon

    Epsilon 3 uses

    A tiny constant for numerical safety (10⁻⁸ in Adam), or noise ε ~ 𝒩(0, I).

  2. π\pi

    Pi 2 uses

    A policy π(a | s) in reinforcement learning, or the constant 3.14159…

  3. β\beta

    Beta 2 uses

    Usually a decay rate (Adam’s β₁, β₂), a shift parameter in normalization, or a weight on a loss term.

  4. α\alpha

    Alpha 2 uses

    Often a learning rate or step size; also attention weights αᵢⱼ or a mixing weight.

Vectors and matrices

  1. ⋅\cdot

    Centre dot 27 uses

    A dot product between vectors, plain multiplication between numbers, or an empty slot f(·).

  2. QQ, KK, VV

    Q, K, V (query, key, value) 21 uses

    In attention: queries Q, keys K and values V, each a matrix with one row per token.

  3. ×\times

    Times (cross) 5 uses

    Usually the “by” in a shape (n × d); also plain multiplication or a cross product.

  4. z\mathbf{z}

    Bold lowercase (vector) 2 uses

    A bold lowercase letter is a vector: a list of numbers.

  5. KTK^{T}

    Transpose 1 use

    Swap rows and columns. xᵀy is a dot product; xyᵀ is an outer product.

Calculus and gradients

  1. ∞\infty

    Infinity 1 use

    Infinity; as a norm subscript, ‖x‖_∞ is the largest absolute entry.

Probability and statistics

  1. pp, qq, PP

    Probability p(x) 21 uses

    A probability or probability density; p is usually the true or model distribution, q an approximation.

  2. loglog

    Logarithm 2 uses

    A logarithm, almost always the natural log (base e) in ML papers.

Functions and operators

  1. OO

    Big-O notation 17 uses

    How a cost grows with size, ignoring constants: O(n²) means at most proportional to n² for large n.

  2. dk\sqrt{d_{k}}, dmodel\sqrt{d_{\text{model}}}

    Square root 5 uses

    The square root. In attention, dividing by √d_k keeps dot products at a sensible scale.

  3. QKTdk\frac{QK^{T}}{\sqrt{d_{k}}}, 1dk\frac{1}{\sqrt{d_{k}}}

    Fraction 3 uses

    Division: the top divided by the bottom.

  4. max⁡\max, min⁡\min

    Max and min 2 uses

    The largest (or smallest) value, over the arguments or over the range in the subscript.

  5. softmax\mathrm{softmax}

    Softmax 1 use

    Turns a vector of scores into probabilities: exponentiate each, then divide by the total.

  6. Concat\mathrm{Concat}

    Concatenation 1 use

    Join vectors end to end: a vector of length m and one of length n become one of length m + n.

  7. FFN\mathrm{FFN}

    Feed-forward network (FFN, MLP) 1 use

    A small network applied to each position: a linear layer, an activation, and another linear layer.

  8. sinsin

    Sine 1 use

    The sine function; in ML papers mostly inside positional encodings and rotary embeddings.

  9. coscos

    Cosine 1 use

    The cosine function; in ML papers usually cosine similarity, a cosine schedule, or positional encodings.

  10. ∑i=1dk\sum_{i=1}^{d_{k}}

    Summation 1 use

    Add up the expression for every value of the index, from the bottom limit to the top.

Sets, shapes and definitions

  1. ∈\in

    Element of 5 uses

    “Is in” or “belongs to”: x ∈ S says x is a member of the set S.

  2. Rdmodel×dk\mathbb{R}^{d_{\text{model}}\times d_{k}}, Rdmodel×dv\mathbb{R}^{d_{\text{model}}\times d_{v}}, Rhdv×dmodel\mathbb{R}^{hd_{v}\times d_{\text{model}}}

    Tensor shapes 5 uses

    ℝ with a superscript of sizes joined by × declares an array’s shape: ℝ^{B×T×d} is a 3-axis tensor.

  3. <<

    Inequalities 1 use

    Less than / greater than (or equal). ≪ means “much smaller than”.

4 things the catalog has no entry for yet
  • Sublayer\mathrm{Sublayer} Sublayer 2
  • head1\mathrm{head_{1}} head_{1} 1
  • headh\mathrm{head_{h}} head_{h} 1
  • headi\mathrm{head_{i}} head_{i} 1

Formulas from arXiv's HTML version of the paper, © their authors; the explanations are this site's. Title and authors as arXiv lists them.

More architectures and attention papers