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.

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):
Where:
are the query, key, and value matrices, linear projections of the token embeddings.
holds the raw dot-product similarity scores between every query-key pair.
scales the scores (key dimension
) to keep softmax out of its saturated, tiny-gradient region; softmax normalizes each row into weights summing to 1.




















