The formula itself isn't the hard part, it's remembering to divide by sqrt(d_k) before softmax and not after.
Start by clearly stating the scaled dot-product attention formula: softmax(QK^T / sqrt(d_k) + mask) V. Then implement it step-by-step in NumPy, handling the optional additive mask by adding it to the scaled scores before softmax, and ensure numerical stability by subtracting the row-wise maximum before exponentiation.
Pro tip: Mention that you subtract the maximum value per row before softmax to prevent overflow, and that the mask should be added as a large negative number (e.g., -1e9) to effectively zero out attention weights. This shows production-level awareness.
Confirm the shapes of Q, K, V, and the mask, and restate the scaled dot-product attention formula. Mention that d_k is the last dimension of Q and K.
Compute the raw attention scores as Q @ K.T, then scale by 1/sqrt(d_k). Explain why scaling is necessary to prevent softmax saturation.
If a mask is provided, add it to the scaled scores. Note that the mask should be additive and broadcastable, with large negative values for positions to ignore.
Implement softmax along the last axis: subtract the row-wise maximum, exponentiate, and normalize by the sum. This avoids overflow and underflow.
Multiply the attention weights by V to get the output. Optionally, test with small random inputs and compare against a reference implementation or check properties like row sums of attention weights equaling 1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Start by explaining the motivation for multi-head attention: allowing the model to attend to information from different representation subspaces. Then walk through the step-by-step process: linear projections, splitting into heads, parallel attention, concatenation, and output projection. Finally, discuss the trade-offs and implementation details, such as computational complexity and parameter count.
Pro tip: Emphasize that multi-head attention is not just about parallelism but about enabling the model to capture diverse relationships. Mention that the total computational cost is similar to single-head with full dimension, but the representational power increases.
Explain why multi-head attention is used: to allow the model to jointly attend to information from different representation subspaces at different positions.
Describe how the input is projected into queries, keys, and values for each head using learned linear transformations, and how the dimension is split across heads.
Detail how scaled dot-product attention is computed independently for each head, potentially in parallel, and how the outputs are combined.
Explain that the outputs from all heads are concatenated and then linearly projected to produce the final output.
Discuss computational complexity, parameter count, and practical considerations like using efficient matrix operations and handling masking.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.