Tag: Transformer

  • DL0026 Self-Attention vs Cross-Attention

    What distinguishes self-attention from cross-attention in transformer models?

    Answer

    The distinction is where Q, K, and V come from. In self-attention, all three are projections of the same sequence, so every token weighs every other token within its own sequence, modeling internal dependencies. In cross-attention, the queries come from one sequence (e.g., the decoder state) while the keys and values come from a different sequence (e.g., the encoder output), letting one representation selectively read from another. Both use the identical scaled dot-product computation.

    (1) Input Scope: Self-attention: Q, K, V from one sequence; cross-attention: Q from the target sequence, K/V from the source sequence.
    (2) Architectural Role: Self-attention appears in encoder and decoder blocks; cross-attention is the bridge that injects encoder context into each decoder step.
    (3) Score Matrix Shape: Self-attention produces an n \times n map; cross-attention produces n_{dec} \times n_{enc}, rectangular when lengths differ.

    Side-by-side attention heatmaps: self-attention among the tokens The cat sat on the mat, and cross-attention from decoder queries to the encoder sequence The animal rested on a rug.

    Figure 1: Same mechanism, different sources: self-attention relates tokens within one sentence; cross-attention aligns decoder tokens to a separate encoder sequence.

    Mathematical Formulation:
    \mathrm{Attention}(Q, K, V) = \mathrm{softmax}\left(\frac{QK^{\top}}{\sqrt{d_k}}\right) V
    Q = X_{q} W^Q, \quad K = X_{kv} W^K, \quad V = X_{kv} W^V

    Where:

    • X_q is the query-source sequence; X_{kv} is the key/value-source sequence; self-attention is the special case X_q = X_{kv}.
    • W^Q, W^K, W^V are learnable projection matrices; d_k is the key dimension used for scaling.

    Side-by-Side Comparison:

    AspectSelf-AttentionCross-Attention
    Q sourceSame sequenceTarget sequence (e.g., decoder)
    K, V sourceSame sequenceDifferent sequence (e.g., encoder output)
    ModelsIntra-sequence dependenciesInter-sequence alignment
    Score matrixn \times n (square)n_{dec} \times n_{enc} (rectangular)
    Typical locationEncoder & decoder blocksDecoder blocks (bridging to encoder)

    Bottom line: self-attention answers “which of my own tokens matter to me?”; cross-attention answers “which tokens of the other sequence should I read now?”


    Login to view more content
  • DL0025 Attention Mechanism

    Please explain the concept of “Attention Mechanism.”

    Answer

    The attention mechanism lets a model dynamically focus on the most relevant parts of the input when producing each output, instead of compressing the entire input into a single fixed-size context vector. For every output element, attention computes a weighted sum over input representations, where the weights (produced by a similarity score and a softmax) express how much each input element matters for that output. This solved the information bottleneck of early sequence-to-sequence models and is the core operation of Transformers.

    (1) Query (Q): What the current output position is “looking for”: the element being processed.
    (2) Key (K): An index of what each input position offers; queries are compared against keys to get similarity scores.
    (3) Value (V): The actual content retrieved; scores are softmax-normalized into weights that blend the values into the output.

    Bar chart of softmax attention weights over the five tokens I, love, machine, learning, models, with machine receiving 0.46 and learning 0.26 of the total weight.

    Figure 1: Attention weights are a probability distribution over inputs: here “machine” and “learning” absorb 72% of the weight for a topic-focused query.

    Mathematical Formulation (Scaled Dot-Product Attention):
    \mathrm{Attention}(Q, K, V) = \mathrm{softmax}\left(\frac{QK^{\top}}{\sqrt{d_k}}\right) V

    Where:

    • Q, K, V are the query, key, and value matrices, linear projections of the token embeddings.
    • QK^{\top} holds the raw dot-product similarity scores between every query-key pair.
    • \sqrt{d_k} scales the scores (key dimension d_k) to keep softmax out of its saturated, tiny-gradient region; softmax normalizes each row into weights summing to 1.

    Login to view more content