Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +918448811320
Back to Deep Learning Notes
Topic #432

Dot-Product Attention

Dot-product attention assembles every piece covered so far in this category — Q/K/V and attention scores — into the complete mechanism: scores, normalized via softmax into weights, used to combine values into a final output.

Formula

\[ \text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}(\mathbf{Q}\mathbf{K}^\top)\mathbf{V} \]

Reading left to right: compute every query's raw score against every key (\(\mathbf{Q}\mathbf{K}^\top\), a matrix of scores), convert each query's row of scores into a valid probability distribution via softmax (see Softmax Function), then use those probabilities as weights for a weighted sum of the value vectors.

Numerical Example — Continuing From Attention Score

Using the scores from Attention Score: \([1.0, 0.0, 0.7]\). Applying softmax:

\[ e^{1.0}\approx2.718,\ e^{0.0}=1.0,\ e^{0.7}\approx2.014, \qquad \text{sum}\approx5.732 \] \[ \text{weights} \approx [0.474,\ 0.174,\ 0.351] \]

With values \(\mathbf{v}_1=[1,0]\), \(\mathbf{v}_2=[0,1]\), \(\mathbf{v}_3=[0.5,0.5]\):

\[ \text{output} = 0.474[1,0]+0.174[0,1]+0.351[0.5,0.5] \approx [0.474+0.176,\ 0.174+0.176] = [0.649,\ 0.350] \]

The output is a weighted blend of every value, dominated by \(\mathbf{v}_1\) (since key 1 scored highest against the query) but with genuine contributions from every other value too — exactly the "soft," differentiable selection mechanism attention provides, as opposed to a hard, all-or-nothing choice.

Code

import torch
import torch.nn.functional as F

Q = torch.tensor([[1.0, 0.0]])           # one query
K = torch.tensor([[1.0, 0.0], [0.0, 1.0], [0.7, 0.7]])   # three keys
V = torch.tensor([[1.0, 0.0], [0.0, 1.0], [0.5, 0.5]])   # three values

scores = Q @ K.T                    # (1, 3) -- raw scores
weights = F.softmax(scores, dim=-1)   # normalize into a probability distribution
output = weights @ V                  # weighted sum of values

print(weights)   # tensor([[0.4741, 0.1743, 0.3516]]) -- matches the hand-worked example
print(output)     # tensor([[0.6483, 0.3517]])

Why This Is a "Soft," Differentiable Lookup

Unlike a hard lookup (picking exactly one best-matching key/value), this weighted-sum approach is fully differentiable — gradients can flow back through every step (the scores, the softmax, the weighted sum) via backpropagation, letting the entire attention mechanism, including the Q/K/V projection weights, be learned end-to-end alongside the rest of the network, exactly like any other layer.

Common Mistakes

  • Forgetting the softmax step and using raw scores directly as weights — without normalization, the weighted sum wouldn't represent a valid convex combination of the values, and could produce nonsensical (unbounded, or even negative-weighted) outputs.
  • Applying softmax across the wrong dimension for a full batch of queries — softmax must normalize each query's row of scores independently (across the key dimension), not across queries or across the batch.

Interview Relevance

Q: "Why is dot-product attention described as a 'soft' lookup, and why does that matter for training?" Instead of selecting exactly one best-matching key/value pair, attention computes a weighted combination of all values, weighted by softmax-normalized similarity scores — every value contributes something, proportional to relevance. This makes the entire operation smooth and differentiable, so gradients can flow through it during backpropagation, allowing the whole mechanism (including the learned Q/K/V projections) to be trained end-to-end via standard gradient descent.

Practice Question

If one key's score is vastly larger than all the others (e.g. 100 vs single-digit scores for the rest), what will softmax do to the resulting attention weights, and what will the output approximately equal?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →