1. Home
  2. Explore
  3. Computer Science
  4. Transformer Architecture and Attention

Study methods

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

Transformer Architecture and Attention

  • 62Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Motivation and Tokens

Why can convolutional neural networks be less effective than global methods for judging relationships between distant parts of an image?

62 questions

  1. 1

    Why can convolutional neural networks be less effective than global methods for judging relationships between distant parts of an image?

    Model answer

    CNN filters process local regions, so information about distant parts must pass through multiple layers before it can be combined.

    Explanation

    CNNs have a strong locality bias. This is useful for many signals, but local processing may be inefficient for relationships between distant image regions.

  2. 2

    In a two-layer CNN with length-three filters, why might no output neuron be able to base its decision jointly on input variables x1x_1x1​ and x7x_7x7​?

    Model answer

    The receptive fields expand gradually, and at that depth no output neuron receives information from both x1x_1x1​ and x7x_7x7​.

    Explanation

    The example illustrates that a shallow CNN’s local receptive fields may not span distant inputs. A deeper CNN could expand the receptive field enough to connect them.

  3. 3

    Why can a fully connected network model global relationships immediately, and what is its main cost compared with sparse attention?

    Model answer

    Every output neuron connects to every input neuron, giving immediate global information flow, but a mapping from Rn\mathbb{R}^nRn to Rn\mathbb{R}^nRn can require n2n^2n2 parameters and connections.

    Explanation

    Attention also looks globally, but does so selectively rather than requiring every possible connection to be independently parameterized.

  4. 4

    What is the central intuition behind attention, and how can it adapt when the question being asked changes?

    Model answer

    Attention gives greater weight to input parts relevant to the current task, so different questions can lead the system to focus on different parts.

    Explanation

    For example, counting birds calls for attending to bird locations, while asking about the sky’s color calls for attending to the sky.

  5. 5

    What is a token in the sense used to describe transformer computation?

    Model answer

    A token is a vector-valued unit that bundles information for processing as one unit.

    Explanation

    This usage treats tokens as encapsulated information units, rather than as scalar-valued neurons.

  6. 6

    How does the course’s use of the term “token” differ from its common use in natural language processing?

    Model answer

    Here a token is a vector or information bundle; in common NLP usage, it is usually a discrete vocabulary unit, such as a character, word, or subword.

    Explanation

    The vector-based definition is intended as a general way to describe the chunks into which a problem is factorized.

  7. 7

    How do transformers achieve broad applicability across different data domains?

    Model answer

    A domain-specific step converts the data into tokens, after which a largely generic transformer processes those tokens.

    Explanation

    The tokenization step captures important details of the input domain; much of the subsequent processing can use the same general framework.

  8. 8

    How can an image be converted into transformer tokens?

    Model answer

    Divide the image into patches, flatten each patch’s pixel values into a vector, and project that vector into a chosen token dimension.

    Explanation

    The projection can be a learned matrix or a hard-coded linear operation. Each resulting token represents one image patch.

  9. 9

    What are the standard choices for image patch overlap and token size, and what alternatives might be explored?

    Model answer

    Standard image transformers generally use non-overlapping patches and tokens of the same size, though overlapping patches or variable-sized tokens are possible alternatives.

    Explanation

    Variable-sized tokens and patch overlap are not described as standard choices; variable sizes may also complicate hardware processing.

  10. 10

    How are text inputs commonly divided into tokens in the described NLP approach?

    Model answer

    Text is commonly divided into byte-pair units, which can represent recurring character sequences such as “TH” or “ING.”

    Explanation

    Other possible units include individual characters or whole words, but byte pairs are given as a common choice.

  11. 11

    How is a set of transformer tokens represented in the notation used in the lecture?

    Model answer

    The token vectors are arranged as rows of a matrix with nnn tokens and ddd channels, so each token has the same dimensionality.

    Explanation

    Using a common dimensionality keeps the representation and associated operations straightforward.

  12. 12

    Why can transformers be described as operating on sets of tokens rather than necessarily on ordered arrays?

    Model answer

    Without positional information, the token ordering does not affect the computation in the same way as the token contents do.

    Explanation

    The standard transformer operations are permutation equivariant: reordering the input tokens reorders the output tokens correspondingly.

  13. 13

    How does a linear combination of tokens differ in expressive power from a fully general linear combination of all their individual neurons?

    Model answer

    A token-level combination uses one weight per token to combine whole vectors, making it a lower-rank and less expressive operation than assigning separate weights to every neuron.

    Explanation

    For instance, combining three tokens uses three weights even if those tokens together contain many individual scalar values.

  14. 14

    What is a token-wise nonlinearity?

    Model answer

    It applies the same nonlinear function independently to every token vector.

    Explanation

    In transformers, the function is typically an MLP, whose parameters are learned.

  15. 15

    How can a token-wise nonlinear operation be viewed as a convolution?

    Model answer

    It is equivalent to applying a shared 1×11\times 11×1 convolutional filter across the sequence of tokens.

    Explanation

    The operation processes each token independently and identically, like a filter that acts on the channels at each position without combining neighboring positions.

  16. 16

    What computational motif do token networks share with ordinary multilayer perceptrons?

    Model answer

    Both alternate a combination operation with a nonlinearity and repeat that pattern through layers.

    Explanation

    An MLP combines scalar-valued neurons, while a token network combines vector-valued tokens and applies nonlinear functions to them.

  17. 17

    How are token networks related to unrolled graph neural networks?

    Model answer

    Both aggregate information from units and then apply an update function, repeating the process across layers.

    Explanation

    In graph neural networks, the aggregation and update functions are associated with graph nodes and their connections; token networks use the analogous operations on tokens.

  18. 18

    What determines which tokens can exchange information in a graph neural network, and how does this compare with a transformer?

    Model answer

    A graph neural network uses the graph’s connectivity, whereas a transformer typically allows every token to interact with every other token.

    Explanation

    A transformer can be viewed as a graph network on a fully connected graph, with attention determining the strengths of the interactions.

  19. 19

    How may depth-wise weight sharing differ between graph neural networks and standard transformers?

    Model answer

    Graph neural networks often share weights across depth, while standard transformers typically use separate weights at different layers.

    Explanation

    Weight sharing is a design choice rather than a strict requirement: either architecture can be configured differently.

  20. 20

    How does the usual nonlinear update function in a transformer differ from the point-wise nonlinearity in a basic MLP?

    Model answer

    A transformer’s token-wise function is typically a learned MLP, while a basic MLP’s point-wise activation, such as ReLU, is typically not itself learned.

    Explanation

    The transformer’s token-wise MLP contains trainable parameters optimized through backpropagation.

  21. 21

    What structural property does a convolution’s matrix representation have, and what benefit does it provide?

    Model answer

    It has a Toeplitz structure with repeated values along diagonals, reflecting shared filter weights and supporting translation equivariance.

    Explanation

    Weight sharing means translating the input shifts the corresponding pattern of operations, which gives the convolution its translation-equivariant property.

  22. 22

    How are attention weights different from the weights of an ordinary learned linear combination?

    Model answer

    Ordinary combination weights are free learned parameters, whereas attention weights are computed from the data being processed.

    Explanation

    The attention weights are activations produced by a function of the input, rather than a fixed weight vector directly learned for every possible input.

  23. 23

    In query-key-value attention, what role does the query play?

    Model answer

    The query represents what information is being sought and is compared with keys to determine which tokens are relevant.

    Explanation

    A query may come from an external question token or, in self-attention, from a token in the input sequence itself.

  24. 24

    What does a key represent in query-key-value attention?

    Model answer

    A key represents information about a token that can be matched against a query.

    Explanation

    The query-key comparison determines how much attention to give to that token’s value.

  25. 25

    How is the relevance of a key to a query measured in the described attention mechanism?

    Model answer

    It is measured using the dot product of the query vector and the key vector.

    Explanation

    A larger similarity score can lead to greater attention weight after the scores are normalized.

  26. 26

    What is the role of a value vector in attention?

    Model answer

    The value contains the information from a token that is passed onward when the token receives attention.

    Explanation

    The attention operation forms a weighted combination of value vectors, using weights determined by query-key similarities.

  27. 27

    How are attention scores converted into weights for combining values?

    Model answer

    A softmax is applied to the scores, producing weights that sum to one.

    Explanation

    The resulting weights specify how much each token’s value contributes to the output.

  28. 28

    How are queries, keys, and values usually produced from token vectors?

    Model answer

    Each is produced by a learned linear projection of the token vector, using separate projection matrices.

    Explanation

    The query, key, and value projections are learned parameters of the attention layer.

  29. 29

    Write the standard scaled dot-product self-attention equations and explain what their output represents.

    Model answer

    The attention weights are A=softmax⁡ ⁣(QKTdk)A=\operatorname{softmax}\!\left(\frac{QK^{\mathsf T}}{\sqrt{d_k}}\right)A=softmax(dk​​QKT​), and the output is AVAVAV. The output is a weighted combination of value vectors.

    Explanation

    The query-key products provide pairwise scores, scaling uses the key/query dimension dkd_kdk​, and softmax normalizes the weights.

  30. 30

    Why is the query-key score matrix divided by the square root of the query and key dimension?

    Model answer

    The score matrix is scaled by dk\sqrt{d_k}dk​​ before softmax as part of the standard attention formulation.

    Explanation

    This scaling is included in the standard mechanism described for computing attention weights.

  31. 31

    How could attention count animals in an image?

    Model answer

    It could attend to patches containing animal heads and sum token values that indicate an animal head, producing the count.

    Explanation

    The example illustrates how selecting relevant patches and combining their values can answer a specific query.

  32. 32

    How could attention estimate the color of a particular animal in an image?

    Model answer

    It could attend to patches of that animal and combine their value vectors, which represent color information.

    Explanation

    The attention weights select relevant patches, while the values supply the information used to produce the color estimate.

  33. 33

    Why does an image-text attention system generally need separate tokenization approaches for images and text?

    Model answer

    Images and text have different structures, so each requires a tokenizer suited to its own domain before attention combines their representations.

    Explanation

    Attention can operate on the resulting token representations without requiring the image and text tokenizers themselves to be the same.

  34. 34

    What is self-attention, and how does its source of queries differ from external-query attention?

    Model answer

    Self-attention uses queries emitted by the tokens in the sequence being processed, rather than queries supplied by a separate question or signal.

    Explanation

    Each input token can produce a query, key, and value, allowing tokens within the same sequence to exchange information.

  35. 35

    Why might a token attend to other tokens that appear similar to it?

    Model answer

    Combining information from similar tokens may improve the token’s representation by providing related evidence from the input.

    Explanation

    For example, an uncertain image patch may use information from other patches that look like the same object.

  36. 36

    What does it mean for a self-attention map to show a token attending to other parts of the same object?

    Model answer

    It means the token assigns greater attention to related patches, which can correspond to grouping an object’s parts and separating it from other objects.

    Explanation

    In the described visual example, this pattern resembles segmentation, but it is an observed behavior rather than a guaranteed property.

  37. 37

    Is an intuitive attention pattern, such as attending to parts of the same object, guaranteed by the transformer architecture?

    Model answer

    No. Such patterns are empirical results of the data, architecture, and optimization, not guarantees of the mechanism.

    Explanation

    Interpretability research studies what models learn internally, but the same task may be solved using other strategies.

  38. 38

    If a sequence contains nnn tokens, what are the dimensions of its pairwise self-attention score matrix, and why?

    Model answer

    It is an n×nn\times nn×n matrix because it contains a score for each query-token and key-token pair.

    Explanation

    The scores determine how each input token attends across the set of tokens.

  39. 39

    How does the parameter dependence of query-key-value attention differ from directly learning a separate attention matrix for every sequence?

    Model answer

    The query, key, and value projection parameters depend on token dimensionality rather than having a separate set of n2n^2n2 learned weights for each sequence length.

    Explanation

    This is useful for long sequences because the sequence length can be very large while the token dimension is smaller.

  40. 40

    What is multi-headed self-attention?

    Model answer

    It runs multiple attention operations in parallel and combines their results.

    Explanation

    Different heads can learn to attend to different kinds of information, such as shapes or textures.

  41. 41

    What are the principal components of the described vision-transformer layer?

    Model answer

    It uses multi-headed self-attention, a token-wise MLP, residual connections, and token normalization.

    Explanation

    The attention and MLP perform the main token processing; residual connections and normalization are additional components of the architecture.

  42. 42

    What does LayerNorm do to a token’s activations in the described transformer?

    Model answer

    It normalizes the elements of the token vector to have zero mean and unit variance.

    Explanation

    The lecture notes that normalization has useful optimization properties, although the precise reason this choice works is not fully settled.

  43. 43

    How is a transformer computation well suited to modern computing hardware?

    Model answer

    Much of its computation can be expressed as matrix multiplications, which modern hardware handles efficiently.

    Explanation

    This also makes the computation relatively straightforward to implement with common deep-learning software and hardware.

  44. 44

    How does a transformer without positional information respond to a permutation of its input tokens?

    Model answer

    It produces the same output tokens in the correspondingly permuted order; the mapping is permutation equivariant.

    Explanation

    Token-wise functions treat tokens identically, and attention is computed from token contents rather than their row order.

  45. 45

    Why add positional information to transformer tokens?

    Model answer

    Positional information lets the model distinguish where tokens came from when their order or location matters.

    Explanation

    Without it, the basic transformer does not represent sequence order or image location through the token ordering alone.

  46. 46

    How can positional information be incorporated into token representations?

    Model answer

    A positional code can be concatenated with each token to identify its position in the input.

    Explanation

    The position may refer to a word’s place in a sentence or a patch’s location in an image.

  47. 47

    How does the described Fourier-style positional encoding represent a patch’s location in an image?

    Model answer

    It represents the patch’s coordinates using values of sine and cosine functions at that location, in horizontal and vertical directions.

    Explanation

    This encodes location in a Fourier basis rather than simply supplying the Cartesian coordinates.

  48. 48

    What domain-specific information can positional encoding provide to a transformer?

    Model answer

    It can provide an inductive bias about what locality and position mean for the input domain.

    Explanation

    The encoding is a place to introduce domain knowledge, such as spatial relationships in images or structural relationships in graphs.

  49. 49

    How could positional encoding be adapted for data on a globe?

    Model answer

    It could use latitude and longitude represented with a Fourier-like basis, such as spherical harmonics.

    Explanation

    This adapts the positional representation to the geometry of a sphere rather than a flat image grid.

  50. 50

    How can graph Laplacian eigenvectors be used as positional information for graph tokens?

    Model answer

    Eigenvectors of the graph Laplacian can provide coordinates that describe nodes’ canonical positions in the graph.

    Explanation

    Different eigenvectors capture different patterns across the graph, from smoother, more global variation to higher-frequency variation.

  51. 51

    How does adding positional information relate to translation equivariance in convolutional networks?

    Model answer

    Providing position information allows processing to depend on location, so the operation need not remain translation equivariant.

    Explanation

    This is analogous to telling a convolutional system where a feature occurs so it can make a location-dependent decision.

  52. 52

    What is an autoregressive language model trained to predict?

    Model answer

    It is trained to predict the next word or token from the preceding sequence.

    Explanation

    Training uses observed text sequences and their continuations as supervised examples.

  53. 53

    How does autoregressive generation produce a longer sequence?

    Model answer

    The model predicts a next token, appends that prediction to the input, and repeats the process.

    Explanation

    Each new prediction becomes part of the context for predicting what comes next.

  54. 54

    How is next-token prediction framed during training?

    Model answer

    It is framed as supervised learning that predicts a token from a vocabulary using examples from observed text.

    Explanation

    The model’s predictions are trained to match the actual next tokens in the training sequences.

  55. 55

    What does “autoregressive” mean in the described generation process?

    Model answer

    The model repeatedly uses its previous output as part of the input for producing the next output.

    Explanation

    This feedback of predictions into later inputs allows the model to generate sequences one token at a time.

  56. 56

    Does autoregressive generation have to proceed in the ordinary left-to-right order?

    Model answer

    No. The sequence can be generated in other orders, although left-to-right generation is a common choice.

    Explanation

    The direction is a modeling choice; the described language-model example uses the conventional sequence order.

  57. 57

    What does GPT stand for, and what two modeling ideas does it combine?

    Model answer

    GPT stands for Generative Pre-trained Transformer; it uses a transformer in an autoregressive language-modeling setup.

    Explanation

    The transformer processes token representations, while autoregressive modeling predicts successive tokens.

  58. 58

    Why must a next-token prediction avoid attending to the token it is supposed to predict?

    Model answer

    Otherwise the model could see the answer in its input and learn an identity shortcut rather than predict from context.

    Explanation

    For example, predicting a word is only meaningful if the model cannot use that same word as input evidence.

  59. 59

    What does causal masking do in an autoregressive transformer?

    Model answer

    It prevents each output position from attending to future tokens, allowing it to use only earlier sequence information.

    Explanation

    Forbidden attention connections are set to zero, so predictions depend on the permitted preceding context.

  60. 60

    How does causal masking affect permutation equivariance?

    Model answer

    It makes token order matter, so the masked transformer is no longer permutation equivariant.

    Explanation

    The mask defines which sequence positions may influence others, imposing an order on token interactions.

  61. 61

    How can a causally masked transformer produce predictions for many positions efficiently during training?

    Model answer

    It processes the sequence with a mask that lets each position attend to itself and earlier positions, so each output can be trained to predict its next token.

    Explanation

    The mask gives each position an appropriate past-only receptive field while allowing the model to produce predictions across the sequence in one pass.

  62. 62

    How does cross-attention support image captioning?

    Model answer

    The text-generation tokens can attend both to earlier words in the caption and to tokens representing the image.

    Explanation

    The image is tokenized and processed, then its representations are made available to the autoregressive text generator through cross-attention.

1 of 62
Question 1 of 62

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent