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

Study methods

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

Attention and Transformers

  • 66Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Sequence Models

What new kinds of problems become possible when neural networks can process sequences rather than only individual inputs?

66 questions

  1. 1

    What new kinds of problems become possible when neural networks can process sequences rather than only individual inputs?

    Model answer

    Sequence-processing networks can handle one-to-many tasks such as image captioning, many-to-one tasks such as classifying a sequence of frames, and many-to-many tasks such as translation.

    Explanation

    Convolutional networks were commonly used for one-to-one mappings, whereas sequence architectures support inputs or outputs consisting of multiple ordered elements.

  2. 2

    What is the purpose of an encoder-decoder recurrent neural network for sequence-to-sequence translation?

    Model answer

    The encoder processes the input sequence and summarizes it, while the decoder uses that summary to generate the output sequence one token at a time.

    Explanation

    This architecture can translate an English word sequence into an Italian or French word sequence even when the lengths and word orders differ.

  3. 3

    What two inputs does a recurrent neural network unit use at each time step?

    Model answer

    It uses the current input xtx_txt​ and the previous hidden state ht−1h_{t-1}ht−1​ to produce the next hidden state.

    Explanation

    Applying the same recurrent unit repeatedly allows the network to process sequences of variable length.

  4. 4

    How does an encoder recurrent neural network summarize an input sequence?

    Model answer

    After processing all input tokens, it commonly uses its final hidden state as a fixed-length context vector representing the entire sequence.

    Explanation

    The recurrent structure allows the final hidden state to incorporate information from earlier time steps.

  5. 5

    What inputs does the decoder recurrent unit receive at each output time step?

    Model answer

    It receives the previous output token yt−1y_{t-1}yt−1​, the previous decoder hidden state st−1s_{t-1}st−1​, and the context vector CCC.

    Explanation

    These inputs allow the decoder to generate output tokens sequentially while using information from the encoded input.

  6. 6

    Why can the standard recurrent encoder-decoder architecture create a communication bottleneck?

    Model answer

    All information from the input sequence must pass through one fixed-length context vector, whose size does not grow as the sequence becomes longer.

    Explanation

    A vector that can summarize a short sentence may be inadequate for representing a paragraph, book, or larger corpus.

  7. 7

    How does attention address the fixed-length context-vector bottleneck?

    Model answer

    At every decoder time step, attention allows the decoder to look back at the entire input sequence and compute a new context vector tailored to the current output.

    Explanation

    The input is repeatedly re-summarized rather than being compressed once into a single vector.

  8. 8

    How are alignment scores computed in the recurrent attention mechanism?

    Model answer

    A learned function compares the current decoder hidden state with each encoder hidden state, often by concatenating them and applying a linear layer that outputs a scalar.

    Explanation

    Each scalar indicates how relevant one encoder state may be to the current decoder state.

  9. 9

    Why is a softmax applied to the alignment scores?

    Model answer

    Softmax converts the unbounded alignment scores into attention weights between 000 and 111 that sum to 111.

    Explanation

    The resulting values can be interpreted as a probability distribution over the input positions.

  10. 10

    How is an attention context vector computed from encoder hidden states?

    Model answer

    It is a weighted linear combination of the encoder hidden states, with the attention weights providing the combination coefficients.

    Explanation

    The resulting context vector emphasizes the input information most relevant to the current decoder step.

  11. 11

    Why might a translation model assign high attention weights to the words “we see” while generating their corresponding output word?

    Model answer

    Those input words are likely the most relevant evidence for producing that output word, whereas unrelated words such as “the sky” contribute less.

    Explanation

    Attention lets the model select relevant portions of the input separately for each output token.

  12. 12

    Does the attention mechanism require explicit supervision indicating which input words align with each output word?

    Model answer

    No. The alignment scores and attention weights can be learned end to end through gradient descent using the output prediction loss.

    Explanation

    The model learns useful alignments while minimizing the cross-entropy loss for the predicted output tokens.

  13. 13

    What loss is typically used to train an attention-based sequence decoder?

    Model answer

    The model is trained with a cross-entropy loss on the predicted output tokens, commonly using a softmax over possible tokens.

    Explanation

    Correct output prediction indirectly teaches the model how to assign attention to useful input positions.

  14. 14

    How can the weights of a decoder recurrent neural network be initialized during training?

    Model answer

    Its learnable weights are typically initialized randomly and then optimized using gradient descent.

    Explanation

    This is distinct from choosing the initial hidden state used when processing a particular output sequence.

  15. 15

    Give two possible methods for setting the decoder's initial hidden state.

    Model answer

    It can be set to the encoder's final hidden state, produced by a learned projection of that state, or initialized to all zeros.

    Explanation

    Any such choice is effective if the model is trained consistently with that initialization scheme.

  16. 16

    How is attention recomputed at each successive decoder time step?

    Model answer

    The current decoder hidden state is compared with every encoder hidden state, the scores are passed through softmax, and a new weighted context vector is formed.

    Explanation

    Thus, each output token can use a different summary of the complete input sequence.

  17. 17

    What does an attention matrix reveal about a translation model?

    Model answer

    It shows the distribution of attention over input words for each generated output word, providing an interpretable view of what the model considered relevant.

    Explanation

    Each row or column, depending on the chosen convention, represents attention assigned to the full input for one output step.

  18. 18

    What does a diagonal pattern in a translation attention matrix usually indicate?

    Model answer

    It indicates that input and output words tend to correspond in the same order.

    Explanation

    Diagonal structures can reveal approximately one-to-one, order-preserving alignments.

  19. 19

    What can a backward diagonal pattern in an attention matrix indicate?

    Model answer

    It can indicate that corresponding words appear in a different order in the input and output languages.

    Explanation

    The pattern reflects a learned reordering between the source and target sequences.

  20. 20

    What might a small two-by-two block in an attention matrix represent?

    Model answer

    It may represent two input words corresponding collectively to two output words without a perfectly disentangled one-to-one alignment.

    Explanation

    Attention visualizations can expose many-to-many relationships as well as simple one-to-one matches.

  21. 21

    What are data vectors and query vectors in the generalized attention operator?

    Model answer

    Data vectors contain information to be summarized, while each query vector specifies the output for which the data should be summarized.

    Explanation

    In the recurrent example, encoder hidden states serve as data vectors and decoder hidden states serve as query vectors.

  22. 22

    What does the generalized attention operator produce for each query vector?

    Model answer

    It produces an output vector formed by summarizing the data vectors according to their relevance to that query.

    Explanation

    The operator first computes similarities, converts them to weights, and then uses those weights to combine the data.

  23. 23

    What is the basic sequence of computations performed by an attention operator?

    Model answer

    It computes query-data similarities, applies softmax to obtain attention weights, and takes a weighted linear combination of the data vectors.

    Explanation

    This computation can be used independently of a recurrent neural network.

  24. 24

    What similarity function is commonly used as a simplification of a general learned alignment function?

    Model answer

    A dot product between a query vector and a data or key vector is commonly used.

    Explanation

    The dot product provides a simple scalar measure of similarity and is efficient to compute.

  25. 25

    Why is the dot product scaled by the square root of the vector dimension in scaled dot-product attention?

    Model answer

    The dot product is divided by the square root of the vector dimension to prevent overly concentrated softmax distributions and vanishing gradients as dimensions increase.

    Explanation

    Scaling improves gradient flow and makes the mechanism more stable across different vector sizes.

  26. 26

    Why must query and data vectors have matching dimensions when their dot product is used directly?

    Model answer

    A dot product requires the two vectors to have the same dimension so that corresponding components can be multiplied and summed.

    Explanation

    Learned projections later allow the original input and output dimensions to differ while still producing compatible query and key vectors.

  27. 27

    How can attention process many query vectors in parallel?

    Model answer

    The query vectors are stored as a matrix, and all pairwise query-key similarities are computed using a matrix multiplication.

    Explanation

    Matrix multiplication efficiently computes a similarity score for every query-key pair at once.

  28. 28

    How are output vectors computed for many queries in parallel?

    Model answer

    The attention-weight matrix is multiplied by the matrix of value or data vectors, producing a weighted combination for every query simultaneously.

    Explanation

    This replaces repeated individual weighted sums with one efficient matrix operation.

  29. 29

    Why does the generalized attention mechanism separate data vectors into keys and values?

    Model answer

    Keys are used to calculate similarity with queries, while values are combined to produce the output, allowing the model to learn different representations for matching and retrieving information.

    Explanation

    The same original data vector no longer has to serve identical roles in both operations.

  30. 30

    How are keys and values produced from data vectors?

    Model answer

    Each data vector is passed through separate learned linear projections, producing a key vector and a value vector.

    Explanation

    The key and value projection matrices are learned jointly with the rest of the network through gradient descent.

  31. 31

    What is the role of the key projection in attention?

    Model answer

    The key projection transforms each data vector into a representation that can be compared with query vectors to calculate alignment scores.

    Explanation

    Key vectors encode the aspects of the data that should be used for matching.

  32. 32

    What is the role of the value projection in attention?

    Model answer

    The value projection transforms each data vector into the representation that is weighted and combined to form the attention output.

    Explanation

    Value vectors encode the information that should be retrieved after matching occurs.

  33. 33

    How does the query-key-value structure resemble a search engine?

    Model answer

    The query describes what is being sought, keys are used to match the query against stored records, and values contain the information returned after a match.

    Explanation

    This separation allows a query to match one aspect of stored data while retrieving a different representation of it.

  34. 34

    Are the key and value projections manually specified for a task?

    Model answer

    No. Their projection matrices are learnable parameters, so gradient descent determines how to represent keys and values in a useful way.

    Explanation

    The model learns how to filter data differently for matching and information retrieval.

  35. 35

    What is cross-attention?

    Model answer

    Cross-attention is an attention layer in which queries and data, or keys and values, come from two potentially different sets of vectors.

    Explanation

    Machine translation and image captioning naturally provide separate input and output sources that can be connected through cross-attention.

  36. 36

    What is self-attention?

    Model answer

    Self-attention processes one set of input vectors by projecting each input into a query, a key, and a value, then allowing the vectors to attend to one another.

    Explanation

    Unlike cross-attention, self-attention does not require two separate sources of vectors.

  37. 37

    How does self-attention differ from cross-attention in terms of its inputs?

    Model answer

    Cross-attention uses separate query and data sets, whereas self-attention derives queries, keys, and values from the same input set.

    Explanation

    The choice depends on whether a task involves two distinct kinds of information or one set that needs to interact internally.

  38. 38

    Why would image classification commonly use self-attention rather than cross-attention?

    Model answer

    Image classification primarily involves one set of image features, so the model can compare parts of the image with one another using self-attention.

    Explanation

    Cross-attention is more appropriate when two distinct inputs, such as an image and a generated sentence, must interact.

  39. 39

    What happens to self-attention outputs if the input vectors are permuted?

    Model answer

    The outputs remain the same vectors but appear in the same permuted order as the inputs.

    Explanation

    Linear projections, dot products, softmax, and weighted combinations preserve this consistent reordering.

  40. 40

    What is permutation equivariance in self-attention?

    Model answer

    Permutation equivariance means that permuting the inputs permutes the outputs in the corresponding way, without changing the computed content of those outputs.

    Explanation

    This property follows because the self-attention computation does not depend on the original ordering of the input vectors.

  41. 41

    Why does basic self-attention not know the order of a sequence?

    Model answer

    Its computations depend on pairwise relationships among vectors, not on their positions in the input sequence.

    Explanation

    Without additional information, self-attention fundamentally operates on an unordered set rather than an ordered sequence.

  42. 42

    What is a positional embedding and why is it added to self-attention inputs?

    Model answer

    A positional embedding is information indicating an input's position, such as its index, and it lets the network use sequence order.

    Explanation

    Positional information compensates for self-attention's natural insensitivity to input ordering.

  43. 43

    What is masked self-attention?

    Model answer

    Masked self-attention restricts which input positions may attend to one another by blocking selected pairwise interactions.

    Explanation

    It imposes a task-specific structure on otherwise unrestricted self-attention.

  44. 44

    How is masking implemented using alignment scores?

    Model answer

    Alignment scores for prohibited interactions are replaced with negative infinity before applying softmax.

    Explanation

    A negative-infinity score becomes zero after softmax, eliminating that interaction from the weighted sum.

  45. 45

    Why is masked self-attention useful for autoregressive language modeling?

    Model answer

    It ensures that the output at position ttt can depend only on the current and earlier tokens, preventing the model from looking ahead at future tokens.

    Explanation

    For example, the second output may use the first two words, but it must not use later words during training.

  46. 46

    What is multi-headed self-attention?

    Model answer

    It runs several independent self-attention heads in parallel, concatenates or combines their outputs, and applies an additional learned projection.

    Explanation

    Each head has the same computational structure but different weights, allowing it to learn a different form of processing.

  47. 47

    Why can multiple attention heads be more powerful than one head?

    Model answer

    Different heads can learn different relationships or patterns among the input vectors, increasing the layer's capacity.

    Explanation

    The heads differ through separately learned and initialized parameters even though their architectures are identical.

  48. 48

    How can the query, key, and value projections in self-attention be computed efficiently?

    Model answer

    The three projection matrices can be combined into one larger matrix multiplication rather than performing three smaller multiplications separately.

    Explanation

    Fewer larger matrix multiplications are often more efficient on modern hardware.

  49. 49

    What are the main computational operations in a multi-headed self-attention layer?

    Model answer

    They are the input projection to queries, keys, and values; the query-key similarity multiplication; the value-weighting multiplication; and the final output projection.

    Explanation

    Thus, despite its notation, the layer is largely a sequence of four large batched matrix multiplications.

  50. 50

    How do recurrent neural networks, convolutions, and self-attention differ in the structures they process?

    Model answer

    Recurrent networks process ordered one-dimensional sequences, convolutions process multidimensional grids locally, and self-attention processes sets of vectors by allowing global interactions.

    Explanation

    Each primitive imposes a different structure on how information can be combined.

  51. 51

    Why are recurrent neural networks difficult to parallelize across a sequence?

    Model answer

    Each hidden state depends on the previous hidden state, forcing the sequence to be processed sequentially.

    Explanation

    This sequential dependency makes recurrent networks difficult to scale to very large models and datasets.

  52. 52

    Why are convolutions highly parallelizable?

    Model answer

    Different locations at which a convolutional kernel is applied can generally be computed independently and in parallel.

    Explanation

    The sliding-kernel operation does not require one spatial position to wait for another.

  53. 53

    What limitation do convolutions have when modeling very large receptive fields?

    Model answer

    They require either very large kernels or many stacked layers to combine information across distant positions.

    Explanation

    Stacking many layers introduces sequential depth even though the individual convolution operations are parallelizable.

  54. 54

    Why is parallelizability valuable for scaling neural networks?

    Model answer

    A parallelizable algorithm can use many processors or accelerators simultaneously, increasing total computation without requiring each individual processor to become faster.

    Explanation

    This makes large-scale training and inference practical on GPUs and distributed systems.

  55. 55

    What is the main computational disadvantage of full self-attention for a sequence of length nnn?

    Model answer

    It requires roughly O(n2)O(n^2)O(n2) computation and memory because it considers interactions between every pair of positions.

    Explanation

    This becomes expensive for extremely long sequences even though the operation is highly parallelizable.

  56. 56

    Why can the quadratic cost of self-attention still be useful in neural networks?

    Model answer

    The extra computation allows every vector to interact directly with every other vector, potentially giving the model greater processing capacity.

    Explanation

    Although more expensive, additional computation is not necessarily harmful when it improves the model's ability to reason over the input.

  57. 57

    What is the core computational primitive of a transformer?

    Model answer

    A transformer uses self-attention as its central mechanism for allowing input vectors to exchange information.

    Explanation

    This architecture has become widely used across language, vision, audio, and other deep-learning applications.

  58. 58

    What is the role of the residual connection around self-attention in a transformer block?

    Model answer

    It adds the self-attention input back to its output, helping information and gradients pass through the network more easily.

    Explanation

    Residual connections support stable optimization in deep architectures.

  59. 59

    Why is layer normalization applied within a transformer block?

    Model answer

    Layer normalization helps stabilize the activations and makes the network train more reliably.

    Explanation

    Normalization is used alongside residual connections to improve optimization in deep neural architectures.

  60. 60

    What is the function of the MLP or FFN sublayer in a transformer block?

    Model answer

    The MLP or FFN performs learned computation independently on each vector after self-attention has allowed the vectors to exchange information.

    Explanation

    Self-attention mixes information across vectors, whereas the FFN transforms each vector individually.

  61. 61

    What is a transformer block?

    Model answer

    A transformer block combines a self-attention sublayer and a position-wise MLP or FFN, with residual connections and layer normalization.

    Explanation

    A transformer is formed by stacking multiple such blocks.

  62. 62

    How can transformers be applied to image classification?

    Model answer

    An image can be divided into patches, each patch can be projected into a vector, and the resulting vectors can be processed by a transformer.

    Explanation

    A pooling operation over the output vectors followed by a linear layer can produce class scores.

  63. 63

    What does a transformer output for an image represented as patch vectors?

    Model answer

    It produces an output vector corresponding to each input patch.

    Explanation

    For classification, these patch-level outputs can be pooled before the final prediction layer.

  64. 64

    How is a classification prediction obtained from transformer outputs for an image?

    Model answer

    The output vectors are pooled, and a linear layer maps the pooled representation to class scores.

    Explanation

    Pooling combines information from all image patches into a representation suitable for classification.

  65. 65

    Why can the same transformer architecture be used for both language and images?

    Model answer

    Both inputs can be converted into sequences or sets of vectors, which self-attention can process regardless of whether the vectors represent words or image patches.

    Explanation

    The transformer operates on vector representations rather than requiring a domain-specific grid or recurrence structure.

  66. 66

    Why are transformers highly scalable compared with recurrent architectures?

    Model answer

    Their main operations are highly parallelizable matrix multiplications, and self-attention allows all vectors to interact within a single layer.

    Explanation

    They avoid recurrent time-step dependencies and do not require many layers merely to create long-range interactions.

1 of 66
Question 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