1. Home
  2. Explore
  3. Computer Science
  4. Self-Attention and Transformers

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Self-Attention and Transformers

  • 66Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recurrent Limitations
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What recurrent architecture was commonly used for sequence-to-sequence NLP systems before Transformers?

Tap to flipClick to flipspace

A bidirectional LSTM encoder combined with a unidirectional LSTM decoder, often augmented with attention.

Or swipe the card

66 cards

  1. What recurrent architecture was commonly used for sequence-to-sequence NLP systems before Transformers?

    A bidirectional LSTM encoder combined with a unidirectional LSTM decoder, often augmented with attention.

  2. Why was attention originally introduced for machine translation?

    To avoid forcing the entire source sentence into a single fixed-size vector.

  3. What is the linear interaction distance problem in recurrent neural networks?

    Words that are far apart in a sequence can interact only through many recurrent steps.

  4. Why are long-distance dependencies difficult for recurrent neural networks to learn?

    The gradients must propagate through many recurrent steps, creating potential vanishing or exploding gradient problems.

  5. Why are recurrent neural networks difficult to parallelize?

    The hidden state at position ttt depends on the hidden state at position t−1t-1t−1, so positions must be computed sequentially.

  6. How does self-attention address the linear interaction distance problem?

    Any word can directly attend to any other word, regardless of their distance in the sequence.

  7. How does self-attention improve parallelization compared with recurrence?

    All word representations can attend to one another in parallel without walking through the sequence index by index.

  8. What is the key-value lookup interpretation of attention?

    A query softly matches a set of keys, and the corresponding values are combined according to the match strengths.

  9. How does attention differ from an exact lookup-table operation?

    The query does not need to exactly match one key; it assigns soft weights to all keys.

  10. What are the three learned transformations in key-query-value self-attention?

    The query, key, and value matrices transform each input word embedding into a query vector, key vector, and value vector.

  11. What are the dimensions of the basic query, key, and value matrices when the model dimension is DDD?

    Each matrix maps oldsymbol{x}_i in bb R^D to another vector in bb R^D, so each has shape D times D.

  12. How is the raw attention score between word iii and word jjj computed?

    By taking the dot product of the query for word iii and the key for word jjj: e_{ij} = oldsymbol{q}_i^ op oldsymbol{k}_j.

  13. How is the attention weight from position iii to position jjj calculated?

    It is the softmax-normalized score: b1_{ij} = rac{exp(e_{ij})}{sum_{j'} exp(e_{ij'})}.

  14. How is the self-attention output for position iii computed?

    As a weighted sum of all value vectors: boldsymbol{o}_i = sum_j alpha_{ij}boldsymbol{v}_j.

  15. Why can separate query and key matrices determine whether a word attends to itself?

    The learned transformations can make boldsymbol{q}_i and boldsymbol{k}_i similar or dissimilar, so self-attention to the same position is learned rather than guaranteed.

  16. What does self-attention mean when applied within one sentence?

    Each word uses its query to access information from the keys and values of all words in that same sentence.

  17. Why does ordinary self-attention lose word-order information?

    The operation treats its inputs like a set of vectors and does not directly use the sequence index of each word.

  18. Give an example showing why position information is necessary in self-attention.

    The sequences “Zuko made his uncle” and “his uncle made Zuko” contain the same words but have different meanings.

  19. How can positional information be added to word embeddings?

    Represent each position iii with a vector oldsymbol{p}_i and add it to the word embedding oldsymbol{x}_i.

  20. What are sinusoidal positional representations?

    Position vectors whose dimensions use sine or cosine functions with different periods.

  21. Why might sinusoidal position encodings be useful for sequence positions?

    Their differing periodicities can represent positions and may support extrapolation to longer sequences.

  22. What is a learned positional embedding?

    A trainable matrix with one learned position vector for each position up to a maximum sequence length.

  23. What is the main limitation of learned absolute positional embeddings?

    They cannot directly represent sequences longer than the maximum length for which position vectors were learned.

  24. Why is sequence length a practical problem for Transformers?

    Self-attention has quadratic memory and computation in sequence length because it computes all pairs of interactions.

  25. What problem results from stacking only self-attention layers?

    Repeated self-attention mainly re-averages value vectors and provides no explicit nonlinear processing.

  26. What is the role of the position-wise feed-forward network in a Transformer?

    It independently post-processes each self-attention output vector with nonlinear layers.

  27. Why are position-wise feed-forward networks computationally efficient?

    Each word is processed independently, so the computations can be highly parallelized on GPUs.

  28. What is the minimal self-attention architecture described in the lecture?

    Input embeddings are combined with position embeddings, processed by self-attention, and then passed through a position-wise feed-forward network.

  29. Why is causal masking needed for language modeling?

    A position predicting the next token must not access tokens that occur later in the sequence.

  30. How is causal masking implemented in self-attention?

    For position iii, scores involving future positions j>ij > ij>i are set to negative infinity before the softmax.

  31. Which positions can a causally masked position iii attend to?

    It can attend to positions j le i, including itself and all earlier positions.

  32. Is causal masking generally used in Transformer encoders?

    No. Encoders usually use unmasked attention so each position can access bidirectional context.

  33. How can self-attention be computed efficiently for a whole sequence?

    Stack the word embeddings into a matrix X in bb R^{n times D} and multiply by the query, key, and value matrices in parallel.

  34. What matrix contains all pairwise raw attention scores?

    The matrix QKopQK^ opQKop, where Q=XWQQ = XW_QQ=XWQ​ and K=XWKK = XW_KK=XWK​.

  35. How is the complete self-attention output written in matrix form?

    operatorname{softmax}(QK^ op)V, with softmax applied across each row of the score matrix.

  36. What is multi-head self-attention?

    Multiple independent attention operations, or heads, each with its own query, key, and value projections.

  37. Why does multi-head attention use lower-dimensional projections for each head?

    Each head operates in a space of dimension approximately D/HD/HD/H, helping keep the total computation manageable.

  38. What does each attention head independently learn?

    Its own query, key, and value transformations, allowing it to focus on different aspects of the sequence.

  39. How are the outputs of multiple attention heads combined?

    Their outputs are concatenated and passed through a final linear transformation.

  40. Are attention heads guaranteed to learn different functions?

    No. They are encouraged by separate parameters to specialize, but some redundancy can remain.

  41. How does the number of attention heads typically relate to model dimension?

    The number of heads is often increased with model dimension so each head retains a reasonable number of dimensions.

  42. What is scaled dot-product attention?

    Attention in which each query-key dot product is divided by the square root of the per-head dimension: frac{boldsymbol{q}_i^top boldsymbol{k}_j}{sqrt{d_h}}.

  43. Why is scaling by sqrt{d_h} useful in attention?

    Without scaling, large dot products can saturate the softmax and produce very small gradients.

  44. What is a residual connection in a Transformer layer?

    The layer's input is added to its output: boldsymbol{y} = F(boldsymbol{x}) + boldsymbol{x}.

  45. How do residual connections help gradient flow?

    They provide an identity path through which gradients can pass directly, reducing problems from vanishing gradients.

  46. Why can residual connections help at initialization?

    If the learned transformation is initially small, the overall layer behaves approximately like the identity function.

  47. What does layer normalization normalize in a Transformer?

    It normalizes the dimensions of an individual word representation, independently for each position.

  48. How is the mean computed for layer normalization?

    For boldsymbol{x} in bb R^D, the mean is mu = frac{1}{D}sum_{j=1}^{D} x_j.

  49. What is the basic standardization step in layer normalization?

    The vector is transformed as frac{boldsymbol{x} - mu}{sqrt{sigma^2 + epsilon}}.

  50. What optional learned parameters are commonly included after layer normalization?

    A learned scale gamma and offset beta, producing gamma odot frac{boldsymbol{x}-mu}{sqrt{sigma^2+epsilon}}+beta.

  51. How does layer normalization differ from batch normalization?

    Layer normalization computes statistics independently for each example and position rather than sharing statistics across a batch.

  52. What is the structure of a standard Transformer decoder block?

    Masked multi-head self-attention, followed by add-and-normalize, then a feed-forward network, followed by another add-and-normalize operation.

  53. What does an add-and-normalize operation contain?

    A residual addition followed by layer normalization.

  54. How does a Transformer encoder differ from a Transformer decoder?

    The encoder uses unmasked multi-head self-attention, whereas the decoder uses causally masked self-attention.

  55. What is cross-attention in an encoder-decoder Transformer?

    An attention operation in which decoder representations provide queries and encoder representations provide keys and values.

  56. Why does cross-attention use decoder vectors as queries?

    The decoder determines what source information it needs at each target-generation step.

  57. What are the keys and values in Transformer cross-attention derived from?

    They are learned projections of the encoder's output representations.

  58. What are the queries in Transformer cross-attention derived from?

    They are learned projections of the decoder's current representations.

  59. What was a major efficiency advantage of Transformers over LSTMs?

    Transformers remove recurrent sequential dependencies, allowing more computation and training data to be processed in parallel on GPUs.

  60. What major NLP development did Transformers enable after their introduction?

    Large-scale pre-training on massive amounts of data and computation.

  61. Why did Transformers largely replace LSTMs in modern NLP systems?

    Their parallel computation and ability to model long-range interactions produced better scalability and performance across many applications.

  62. What is the main computational drawback of standard self-attention?

    Its all-pairs interaction matrix requires quadratic computation and memory in sequence length: O(n2)O(n^2)O(n2).

  63. Why is quadratic attention especially problematic for long documents?

    Increasing sequence length causes the number of pairwise interactions to grow as n2n^2n2, quickly exceeding practical memory and computation budgets.

  64. What is one reason longer context windows remain difficult even in large language models?

    The memory cost of storing and computing the full pairwise attention matrix grows quadratically with context length.

  65. What types of improvements to Transformers remain active research areas?

    Reducing quadratic attention costs, improving positional representations, and modifying feed-forward nonlinearities.

  66. Why might absolute position embeddings be an imperfect representation of word position?

    A word's absolute index may be less informative than its relative position or structural relationship to other words.

Card 1 of 66
Card 1 of 66

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent