Transformers and Pretraining

Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 7

17 cards in “7.1 Attention” · whole deck

7.1 Attention

  1. Why do static embeddings fall short for a word like it?
    A static vector is the same in every context, but it refers to the chicken in “…because it was too tired” and to the road in “…because it was too wide”. Its meaning depends on context.
  2. Give examples of words whose interpretation depends on possibly distant context.
    Pronouns (it → chicken or road), agreement (“The keys to the cabinet are on the table”: the verb agrees with a distant subject), and word senses (bank as riverside because of pond).
  3. What are contextual embeddings, and how does a transformer build them?
    Representations of a token’s meaning in its context. Layer by layer, a transformer combines a token’s representation from the previous layer with information from neighboring tokens, giving richer contextual representations at each layer.
  4. Define attention in the transformer.
    The mechanism that weighs and combines the representations of other tokens in the context at layer \(k\) to build the representation of a token at layer \(k+1\).
  5. When computing the representation of it in “The chicken didn’t cross the road because it”, where would we expect attention to go?
    Mostly to chicken and road: at that point it could corefer with either, so its representation should draw on both.

    The chicken didn’t cross the road because it

    darker = more attention from “it” (illustrative)

  6. What does a causal self-attention layer have access to when processing position \(i\)?
    Its own input \(x_i\) and the representations of all prior tokens \(x_1 \ldots x_{i-1}\) in the context window, but no tokens after \(i\). It maps an input sequence to an output sequence of the same length, computing every position in parallel.
  7. Describe the simplified version of attention.
    The output is a weighted sum of the prior inputs, \(a_i = \sum_{j \le i}\alpha_{ij}x_j\), with weights from a softmax over dot-product similarities: \(\alpha_{ij} = \text{softmax}(x_i \cdot x_j)\) for \(j \le i\).
  8. In simplified attention, which input usually gets the highest weight, and why?
    \(x_i\) itself, since a vector is very similar to itself and gets a high dot product; other similar context tokens also get some weight.
  9. What three roles does each input embedding play in attention?
    Query: the current element being compared to preceding inputs. Key: a preceding input being compared to the current element to get a similarity weight. Value: a preceding element’s content that gets weighted and summed into the output.
  10. How are queries, keys and values computed?
    By projecting each input with learned matrices: \(q_i = x_iW^Q\), \(k_i = x_iW^K\), \(v_i = x_iW^V\).
  11. Why is the query–key dot product divided by \(\sqrt{d_k}\)?
    Dot products can be arbitrarily large, and exponentiating large values causes numerical issues and loss of gradients in training. Scaling by the square root of the query/key dimensionality keeps scores in a reasonable range.
  12. Write the equations for a single attention head computing \(a_i\).
    \(\text{score}(x_i, x_j) = \frac{q_i \cdot k_j}{\sqrt{d_k}}\); \(\alpha_{ij} = \text{softmax}(\text{score}(x_i, x_j))\) for \(j \le i\); \(\text{head}_i = \sum_{j \le i}\alpha_{ij}v_j\); \(a_i = \text{head}_i W^O\).
  13. What is the role of \(W^O\), and what are the shapes in a single attention head?
    It reshapes the head output back to the model dimension. \(W^Q, W^K\): \([d \times d_k]\); \(W^V\): \([d \times d_v]\); the head output is \([1 \times d_v]\); \(W^O\): \([d_v \times d]\), so input and output are both \([1 \times d]\).
  14. Why is it convenient that every vector inside a transformer has the model dimensionality \(d\)?
    It makes the transformer very modular: blocks and components can be stacked and combined freely.
  15. What is the intuition behind multi-head attention?
    Each head can attend to the context for a different purpose, e.g. specializing in different linguistic relationships or patterns. A heads run in parallel at the same depth, each with its own \(W^{Q_c}, W^{K_c}, W^{V_c}\).
  16. How are the outputs of multiple heads combined?
    The \(A\) head outputs (each \([1 \times d_v]\)) are concatenated into \([1 \times Ad_v]\), then projected by \(W^O\) of shape \([Ad_v \times d]\): \(a_i = (\text{head}^1 \oplus \cdots \oplus \text{head}^A)W^O\).
  17. Why does multi-head attention cost about the same in parameters as a single head?
    The head dimensionality is set to \(d_v = d/A\), so the heads together have the size of one full-width head.

Source