๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #241

Attention Score

The attention score is the raw number measuring how well a given query matches a given key โ€” before any normalization, this is what "relevance" looks like numerically in the attention mechanism.

Formula

\[ \text{score}(\mathbf{q}, \mathbf{k}) = \mathbf{q} \cdot \mathbf{k} \]

The simplest and most common choice is the dot product between the query and key vectors โ€” reusing exactly the geometric intuition from Dot Product: a large positive dot product means the query and key vectors point in a similar direction in the learned embedding space, interpreted here as "this key is highly relevant to this query."

Computing Scores Against Every Key

For one query \(\mathbf{q}\) and a sequence of \(n\) keys \(\mathbf{k}_1, \ldots, \mathbf{k}_n\), a full set of scores is computed โ€” one per key:

\[ \text{scores} = [\mathbf{q}\cdot\mathbf{k}_1,\ \mathbf{q}\cdot\mathbf{k}_2,\ \ldots,\ \mathbf{q}\cdot\mathbf{k}_n] \]

For a full sequence of queries as well, this generalizes cleanly to a matrix multiplication: \(\mathbf{Q}\mathbf{K}^\top\) โ€” the exact matrix-multiplication mechanics from Matrix Multiplication, producing a full grid of every query's score against every key.

Numerical Example

Query \(\mathbf{q}=[1, 0]\), three keys \(\mathbf{k}_1=[1,0]\), \(\mathbf{k}_2=[0,1]\), \(\mathbf{k}_3=[0.7,0.7]\):

\[ \text{score}(\mathbf{q},\mathbf{k}_1) = (1)(1)+(0)(0) = 1.0 \] \[ \text{score}(\mathbf{q},\mathbf{k}_2) = (1)(0)+(0)(1) = 0.0 \] \[ \text{score}(\mathbf{q},\mathbf{k}_3) = (1)(0.7)+(0)(0.7) = 0.7 \]

Key 1 (pointing in the exact same direction as the query) scores highest; key 2 (perpendicular) scores zero โ€” no relevance at all; key 3 (partially aligned) scores in between. These raw scores aren't yet usable as weights (they don't sum to 1, and can be negative) โ€” that conversion is exactly what happens next.

Code

import torch

q = torch.tensor([1.0, 0.0])
keys = torch.tensor([[1.0, 0.0], [0.0, 1.0], [0.7, 0.7]])

scores = keys @ q   # equivalently: torch.matmul(keys, q)
print(scores)   # tensor([1.0000, 0.0000, 0.7000]) -- matches the hand-worked example

From Scores to Usable Weights โ€” A Preview

Raw scores need to be converted into a proper probability distribution over the keys โ€” non-negative, summing to 1 โ€” before they can be used as weights for combining values. This conversion, via softmax, is exactly the subject of Dot-Product Attention, the next note.

Common Mistakes

  • Treating raw attention scores directly as weights without normalizing them โ€” they can be negative, and don't sum to any particular value, so they can't be used directly to combine values via a weighted sum.
  • Assuming the dot product is the only valid way to compute an attention score โ€” additive/"Bahdanau-style" attention historically used a small feedforward network instead; the dot-product approach ("Luong-style") became the standard largely because it's more computationally efficient (pure matrix multiplication) at scale.

Interview Relevance

Q: "What does a large positive attention score between a query and a key represent, geometrically?" It means the query and key vectors point in a similar direction in the learned embedding space โ€” the dot product being large and positive is exactly the geometric signature of directional alignment, as established in Dot Product. In attention's context, this is interpreted as "this key's associated value is highly relevant to what this query is looking for."

Practice Question

For query \(\mathbf{q}=[0, 1]\) and key \(\mathbf{k}=[0,-1]\), compute the attention score. What does the negative value suggest about the relevance of this key to this query?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Attention Score โ€“ FAQs

Quick answers about learning Attention Score in Deep Learning.

This free note from CodingNow 2.0 explains Attention Score in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Attention Score, is 100% free with no signup required.
With focused practice, most students grasp Attention Score in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now