AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Attention Mechanism: Focus on What Matters

📚 Deep Learning⏱️ 22 min read🎓 Grade 10
✍️ AI Computer Institute Editorial Team Updated: September 2026 CBSE-aligned · Peer-reviewed · 22 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Searching Ten Thousand Transactions Without Reading Them All

Open a UPI app and search "electrician" in the transaction history. In well under a second, the app scans hundreds of past payments and returns the three or four that match, even though the word "electrician" never appears next to the amount or the date in any obvious way. The app does not read every transaction with equal care before answering. It scores each one by how relevant it is to the search, then lets the highest-scoring matches dominate the result while the rest are effectively ignored.

Neural networks that process language run into a version of this same problem. Consider translating the Hindi sentence "राजेश ने कल शाम को अपनी बहन के लिए दिल्ली से मुंबई की ट्रेन टिकट बुक की" (Rajesh booked a Delhi-to-Mumbai train ticket for his sister yesterday evening) into English. To produce the word "ticket," a translation model has to decide which of the seventeen Hindi words to draw on. Treating every word as equally important would be like scanning every UPI transaction with equal care before answering a one-word search: thorough, but slow, and likely to dilute exactly the part that mattered. The attention mechanism is the technique that lets a neural network do what the UPI search bar already does: look at all the available information but assign most of the weight to the parts that are actually relevant to the task at hand.

This chapter builds attention from first principles: the problem it was invented to fix, the exact arithmetic behind it, a complete worked example traced by hand and in code, and the way it grew into the self-attention layers running inside nearly every language model and translation system in use today.

Why One Vector Was Never Going to Be Enough

Before attention existed, machine translation was usually built as an encoder-decoder pair of recurrent neural networks. The encoder read the source sentence one word at a time, updating a hidden state at each step, and after the final word it handed that hidden state, a single fixed-length context vector, to the decoder as the sentence's entire meaning. The decoder then generated the full translation using only that one vector as its memory of the source sentence.

The limitation is easy to predict: a five-word sentence and a fifty-word sentence both had to fit into a vector of the same fixed size. Researchers who tested this design in 2014 found what that limitation implies: translation quality held up reasonably well on short sentences and dropped steadily as sentences grew longer, because the fixed vector produced at the end of a long sentence had already been overwritten several times over by the time the encoder reached the last word, so little of what appeared early in the sentence survived to the end.

Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio addressed this directly in a 2014 paper titled "Neural Machine Translation by Jointly Learning to Align and Translate," tested on English-to-French translation. Rather than force the encoder to compress an entire sentence into one vector, they kept its hidden state from every word, all of them, and let the decoder look back across all of those hidden states at each step of generation, learning for itself which ones deserved more weight. Producing "ticket" from the sentence above would then draw most of its signal from "टिकट" and very little from "शाम" (evening) or "कल" (yesterday). Deciding how much weight each source word deserves, for each word being generated, is exactly what attention computes.

Bahdanau's paper includes exactly this kind of evidence: a heatmap with source words along one axis and generated words along the other, shaded by attention weight. Read across any row and the brightest cell almost always lands on the word a bilingual speaker would pick by hand as its translation, including cases where English and French order an adjective and noun differently and the brightest cells shift off the diagonal to match. That was visual proof the network had learned to align two languages on its own, without ever being told which word corresponds to which.

Turning Relevance into Arithmetic

Return to the UPI search bar. Three separate things are at work when you type "electrician":

  • The typed text is the query: what you are looking for right now.
  • Every past transaction carries a description, payee name, and category. That is its key: the label an item uses to advertise what it is, so it can be compared against a query.
  • Once a transaction is judged relevant, what actually gets displayed, the amount, the date, the payee, is its value: the real content being retrieved.

A plain database search stops there: match the query against the keys, return the values of whatever matches. Attention goes further. Instead of a strict match or no match, it computes a relevance score between the query and every single key, converts those scores into weights that sum to 1, and returns a blend of every value in proportion to those weights. A transaction that matches closely contributes heavily to that blend; one that barely relates contributes almost nothing, though rarely exactly zero.

Written with vectors, this is scaled dot-product attention, the version used inside the Transformer architecture:

Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) V

Four moving parts, each with one job:

  • Q and K are vectors of the same length, d_k. Their dot product, QK^T, comes out large when the two vectors point in similar directions, meaning the query and that key are relevant to each other, and small or negative when they are not.
  • Dividing by sqrt(d_k) keeps those dot products from growing too large as the vector length increases. Left unscaled, a large d_k pushes some scores far above the rest, and softmax reacts to a wide gap between scores by driving its output toward one weight near 1 and the rest near 0. Once that happens during training, the gradient flowing back through the smaller scores shrinks close to zero, and the network effectively stops learning from them.
  • softmax turns the scaled scores into a proper probability distribution: every weight lands between 0 and 1, and the weights for one query always sum to exactly 1.
  • Multiplying those weights by V produces the output: a weighted average of every value, tilted toward whichever ones the query found most relevant.

Why softmax specifically, rather than just dividing each score by the total? Softmax is exponential, so it exaggerates the gap between a high score and a low one instead of preserving it in plain proportion: a raw score that is only slightly larger than another turns into a noticeably larger weight, which is what lets attention commit strongly to the one or two most relevant keys instead of spreading weight thinly across everything. It is also smooth everywhere, which is what lets gradients flow back through it during training.

A Fully Worked Example: What Does "It" Refer To?

Take the sentence "The mechanic fixed the scooter because it was leaking petrol." A human reader resolves "it" to "scooter" without thinking twice. A network reading this sentence word by word has no such instinct built in. It has to compute which earlier word "it" should draw meaning from, using exactly the formula above.

To keep the arithmetic short enough to follow by hand, use toy 2-dimensional key and value vectors for the three candidate nouns, standing in for the 64-dimensional (or larger) vectors a real trained model would use:

            key vector    value vector
mechanic    [1.0, 0.0]    [ 2.0,  0.0]
scooter     [0.0, 1.0]    [ 0.0,  2.0]
petrol      [0.5, 0.5]    [ 1.0, -1.0]

Suppose the query vector produced for the word "it" is Q = [0.0, 4.0]. These numbers are illustrative, chosen to make the arithmetic land on clean values; in an actual model, every one of these vectors is learned from data during training, not set by hand.

Step 1: raw scores, QK^T. Take the dot product of Q with each key.

  • mechanic: (0.0)(1.0) + (4.0)(0.0) = 0.0
  • scooter: (0.0)(0.0) + (4.0)(1.0) = 4.0
  • petrol: (0.0)(0.5) + (4.0)(0.5) = 2.0

Step 2: scale by sqrt(d_k). Here d_k = 2, so sqrt(d_k) ≈ 1.4142.

  • mechanic: 0.0 / 1.4142 = 0.0000
  • scooter: 4.0 / 1.4142 = 2.8284
  • petrol: 2.0 / 1.4142 = 1.4142

Step 3: softmax. Exponentiate each scaled score, then divide by their sum.

  • e^0.0000 = 1.0000
  • e^2.8284 = 16.9188
  • e^1.4142 = 4.1133
  • sum = 22.0321

Dividing each exponentiated score by 22.0321 gives the final attention weights: mechanic = 0.0454 (4.5%), scooter = 0.7679 (76.8%), petrol = 0.1867 (18.7%). These three numbers sum to 1.0000, and they say something specific: given this query, "it" should draw more than three-quarters of its meaning from "scooter," a little under a fifth from "petrol," and almost nothing from "mechanic."

Step 4: weighted sum of values. Multiply each value vector by its weight and add the results.

0.0454 × [2.0,  0.0] = [0.0908,  0.0000]
0.7679 × [0.0,  2.0] = [0.0000,  1.5358]
0.1867 × [1.0, -1.0] = [0.1867, -0.1867]
                        ------------------
                 sum = [0.2775,  1.3491]

The vector [0.2775, 1.3491] is the new, contextualized representation of "it" after one layer of attention. It sits close to the value vector for scooter, [0.0, 2.0], and far from the value vector for mechanic, [2.0, 0.0]: the output was pulled almost entirely toward the value that the attention weights favoured. In a real model, this contextualized vector, rather than the original, context-free embedding for "it," is what gets passed to the next layer, a direct reason Transformer-based models handle pronouns and other context-dependent words far better than models that process each word in isolation.

The Same Computation in Code

Every step above is five lines of NumPy. Writing it as a function also makes a subtle point clear: attention itself carries no learned parameters beyond whatever produced Q, K, and V in the first place. The mechanism is fixed arithmetic; only its inputs are learned.

import numpy as np

def softmax(x):
    e = np.exp(x - np.max(x))   # subtract max only for numerical stability
    return e / e.sum()

def scaled_dot_product_attention(Q, K, V):
    d_k = K.shape[-1]
    scores = Q @ K.T / np.sqrt(d_k)   # Step 1 + Step 2: raw scores, then scaled
    weights = softmax(scores)          # Step 3: scores become a distribution
    output = weights @ V               # Step 4: weighted sum of values
    return output, weights

# key and value vectors for: mechanic, scooter, petrol
K = np.array([[1.0, 0.0],
              [0.0, 1.0],
              [0.5, 0.5]])

V = np.array([[2.0,  0.0],
              [0.0,  2.0],
              [1.0, -1.0]])

Q_it = np.array([0.0, 4.0])   # query vector for the word "it"

output, weights = scaled_dot_product_attention(Q_it, K, V)

print("attention weights:", np.round(weights, 4))
print("context vector for 'it':", np.round(output, 4))

Running this prints:

attention weights: [0.0454 0.7679 0.1867]
context vector for 'it': [0.2775 1.3491]

Every number matches the hand calculation above. Attention has no hidden trick: four steps, score, scale, normalize, combine, applied to whatever vectors the rest of the network supplies. A real Transformer layer runs the same four steps, except Q, K, and V are each computed from the input embeddings X through learned weight matrices instead of being set by hand:

Q = X @ W_Q
K = X @ W_K
V = X @ W_V

W_Q, W_K, and W_V are what training actually adjusts. The softmax-and-weighted-sum step never changes; training only reshapes which directions in vector space count as similar, so that after enough examples, the query built for a pronoun like "it" comes to point toward the key of whichever noun it refers to, just as this chapter's toy Q vector was hand-chosen to do above.

Self-Attention and the Rise of the Transformer

The worked example above is a special case called self-attention: the query, the keys, and the values all come from the same sentence. Every word takes a turn being the query and attends over every word in that same sentence, including itself. Run this for all ten words in "The mechanic fixed the scooter because it was leaking petrol," not just for "it," and the result is a full 10×10 grid of attention weights describing how strongly every word relates to every other word. That grid is recomputed at every layer of the network, so early layers tend to pick up local, grammatical relationships, a verb and its subject, while deeper layers pick up longer-range ones, a pronoun and the noun several clauses back.

The same computation would run for every other word in the sentence too. A query built from "mechanic" would likely put most of its weight on itself and on "fixed," the verb it is the subject of, and very little on "petrol," a noun with no direct grammatical relationship to it. Nothing forces this outcome by rule; a trained model settles into it because attending to the right words is what minimizes prediction error over millions of training examples.

This is different from the encoder-decoder attention Bahdanau's team introduced in 2014, usually called cross-attention: there, queries come from the decoder while keys and values come from the encoder, so the two sides being compared are different sequences. Self-attention applies the same arithmetic within a single sequence, to build a better representation of it, rather than to connect an input sequence to a separate output sequence.

In 2017, Ashish Vaswani and colleagues at Google published a paper titled "Attention Is All You Need," proposing something that looked reckless on paper: build the entire translation model, encoder and decoder both, using nothing but self-attention and simple feedforward layers, with no recurrence at all. They called the architecture the Transformer, and within a few years it became the dominant design for language models, the same basic design behind the large language models in everyday use now.

Dropping recurrence fixed a real bottleneck rather than just changing style. An RNN processes a sentence one word at a time, so word 50 cannot be computed until word 49 finishes: training is inherently sequential, and a signal from an early word has to pass through dozens of intermediate steps to reach a late one, weakening along the way. In Big-O terms, the longest path between any two positions is O(n) for a recurrent layer reading a sequence of length n. Self-attention removes that limit: every word attends directly to every other word in a single matrix multiplication, so the path length between any two positions is O(1) regardless of distance, and all of those pairwise comparisons happen at once rather than one after another, which is exactly the kind of computation a GPU is built to run in parallel.

That speed is not free. Total computation for one self-attention layer grows as O(n²) with sequence length n, since every position is compared against every other position at once. For sentence-length inputs, a few dozen words, that cost is small and the parallelism more than makes up for it; for something like an entire book processed in a single pass, the n² term becomes the harder constraint, which is why handling very long sequences efficiently remains an active area of research.

One more piece was needed to make this work. Self-attention on its own has no sense of word order: swapping two words in the input swaps two rows of the same computation without changing any individual score. The original Transformer paper solved this by adding a fixed positional encoding vector, built from sine and cosine functions at different frequencies, to each word's embedding before the first attention layer, giving the model a way to tell position 1 apart from position 40 even though attention itself does not care about order.

Real Transformer layers also do not compute just one query, key, and value per word. They compute several in parallel, called attention heads, each with its own learned W_Q, W_K, and W_V, so one head can specialize in subject-object relationships while another specializes in coreference, the "it"-to-"scooter" kind of link, and another in a pattern no human would think to name. The base Transformer described in the 2017 paper used 8 heads, each working in a 64-dimensional space, concatenated back into a single 512-dimensional vector (8 × 64 = 512) before moving to the next layer. This is multi-head attention: not one relevance judgment per word, but several, computed side by side and combined.

One further detail matters for any model that generates text one word at a time, such as GPT-style models. When predicting the next word, a position must not be allowed to attend to positions that come after it, since those have not been generated yet. This is enforced by masking: before the softmax step, scores for future positions are forced to negative infinity, so their weight after softmax becomes zero. Self-attention with this restriction is called causal, or masked, self-attention. A translation encoder does not need it, since the full source sentence is available before translation starts; a text generator does.

Back to the Search Bar

The UPI search bar from the start of this chapter turns out to run on the same idea. Typing "electrician" produces a query. Every stored transaction contributes a key to be matched against it. Every match, weighted by relevance instead of judged strictly right or wrong, contributes to what finally gets shown. Attention takes that same sequence, score, scale, normalize, combine, and makes it differentiable, so a network can learn from data which parts of its input deserve weight for a given task, without a programmer writing a rule for it by hand.

That is why attention reshaped the field so quickly after 2017. It replaced a fixed-size bottleneck with a mechanism that can look anywhere in its input and decide, case by case, what to focus on. An idea first built to help a translation model handle long sentences now sits underneath nearly every large language model, chatbot, and translation app in daily use, including whichever one untangles the next long WhatsApp forward that turns up in a language you only half read.

Think About It

Think about this: How would you explain attention mechanism: focus on what matters to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where attention mechanism: focus on what matters is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting attention mechanism: focus on what matters to at least 3 other topics you have studied.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind attention mechanism: focus on what matters, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← Residual Connections: Skip and LearnPositional Encoding: Teaching Order →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn