National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · 15:00–16:00 · 19 October 2026

Attention: selective retrieval

Why force every output to use the same compressed memory?

Machine Learning in Engineering Science Fall 2026 · Week 07 · Hour 2 Chi-Hua Jonny Yu · NCKU / LAiMM

ONE MEMORY, DIFFERENT QUESTIONSstate 1state 2state 3Query-dependent mixContext for this outputCurrent query
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202600:00–02:00 · 01/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

This hour: learn to retrieve

Compare a query with keys, then use normalized weights to combine values.

Goal: compute a context vector and explain a causal mask.

00–14 minMotivation + query / key / value14–34 minScores → weights → context34–48 minMasks + self / cross attention48–60 minNotebook checks + summary
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202602:00–04:00 · 02/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Alignment is output-dependent

A translation decoder may emphasize different source positions at different steps.

0.700.200.100.100.700.200.150.150.70Key / source position →Rows: queries / receiving positions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202604:00–06:00 · 03/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Attention predates the Transformer

LSTM or GRU encoders can supply states to an attention mechanism.

2014/15: recurrent attention 2017: Transformer

Recurrent encoderDecoder queryAttention contextOutput
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202606:00–08:00 · 04/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Keep the full set of encoder states

The decoder can retrieve from H instead of using only the final state.

H = [h₁; h₂; …; hₙ]

state 1h₁state 2h₂state 3h₃state 4h₄Shared update parameters across timePast → running state → later state
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202608:00–10:00 · 05/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

A query specifies what to look for

The query is a vector used to compare the current need with memory keys.

q ∈ ℝᵈᵏ

q / kAkBkCChosen vectors for an inspectable example
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202610:00–12:00 · 06/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Keys address; values carry content

Matching uses keys; the retrieved combination uses values.

K ∈ ℝᴺˣᵈᵏ V ∈ ℝᴺˣᵈᵛ

A: key [1,0] → value [10,0]B: key [0,1] → value [0,10]C: key [1,1] → value [10,10]
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202612:00–14:00 · 07/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

A score is a compatibility function

A score compares one query with one key before normalization.

dot score: eⱼ = q · kⱼ

1A0B1CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202614:00–16:00 · 08/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Additive attention learns a scoring network

An MLP can compare a decoder query with each encoder key.

eᵢⱼ = vₐᵀ tanh(Wq qᵢ + Wk kⱼ + b)

Project queryProject keytanh + learned vectorOne scalar score
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202616:00–18:00 · 09/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Scaled dot products control score magnitude

Divide by √dₖ so high-dimensional dot products are less likely to saturate softmax.

S = QKᵀ / √dₖ

1A0B0.7071CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202618:00–20:00 · 10/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Softmax allocates a unit total weight

Normalize separately for each query over its available keys.

aᵢⱼ = exp(sᵢⱼ) / Σₘ exp(sᵢₘ)

0.4011A0.1978B0.4011CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202620:00–22:00 · 11/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Retrieval is a weighted sum

Each value contributes according to its attention weight.

cᵢ = Σⱼ aᵢⱼ vⱼ

0.4011 × vA0.1978 × vB0.4011 × vCSum to context
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202622:00–24:00 · 12/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Read matrix axes before the formula

Rows are query destinations; columns are key sources.

(Nq × dₖ)(dₖ × Nk) → Nq × Nk

0.600.300.100.200.200.60Key / source position →Rows: queries / receiving positions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202624:00–26:00 · 13/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Work one example: choose Q, K, and V

The values are deliberately small so all intermediates can be checked.

q=[1,0] K=[[1,0],[0,1],[1,1]]

Q: 1 × 2K: 3 × 2V: [[10,0], [0,10], [10,10]]
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202626:00–28:00 · 14/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Work one example: scores become weights

A zero dot product still receives a positive normalized weight.

scores ≈ [0.7071, 0, 0.7071]

What are the three attention weights?

Approximately [0.4011, 0.1978, 0.4011], with sum 1.

0.4011A0.1978B0.4011CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202628:00–30:00 · 15/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Work one example: combine value coordinates

Use the same weights for every value coordinate.

context ≈ [8.0222, 5.9889]

4.011A1.978B4.011CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202630:00–32:00 · 16/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Change the query; keep memory fixed

Attention can route the same memory differently for each request.

0.401Key A
0.198Key B
0.401Key C
context ≈ [8.022, 5.989]
Interactive chosen values — change one variable
Machine Learning in Engineering Science · 19 Oct 202632:00–34:00 · 17/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Self-attention uses one source sequence

Q, K, and V come from learned projections of the same representations.

Q=XWQ K=XWK V=XWV

One sequence XThree learned projectionsAll-pairs scoresUpdated sequence
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202634:00–36:00 · 18/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Cross-attention connects two streams

Queries come from one sequence; keys and values come from another.

Q=YWQ K=XWK V=XWV

Decoder Y supplies queriesAEncoder X supplies keys and valuesB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202636:00–38:00 · 19/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Causal masking hides future keys

Before softmax, set scores for key positions j>i to −∞.

allowed(i,j) ⇔ j ≤ i

1.00×××××0.500.50××××0.330.330.33×××0.250.250.250.25××0.200.200.200.200.20×0.170.170.170.170.170.17Key / source position →Rows: queries / receiving positions× = blocked before softmax
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202638:00–40:00 · 20/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Padding masks hide nonexistent tokens

Padding and causality enforce different constraints.

1.00×××××0.500.50××××0.330.330.33×××0.250.250.250.25××0.200.200.200.20××0.170.170.170.17××Key / source position →Rows: queries / receiving positions× = blocked before softmax
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202640:00–42:00 · 21/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

An all-masked row is undefined

Softmax requires at least one permitted source per query.

all scores = −∞ → no valid normalization

Valid: at least one visible keyAInvalid: every key is maskedB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202642:00–44:00 · 22/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Attention exposes a short information path

A query can access a distant key within one dense attention layer.

RNN: pass through many state updatesAAttention: direct query-to-key edgeB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202644:00–46:00 · 23/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Dense attention has a quadratic score grid

Doubling context length quadruples score entries at fixed head count.

per head: T × T scores

256T=161024T=324096T=6416384T=128Dense score entries per head
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202646:00–48:00 · 24/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Open the attention notebook

Verify your hand calculation before exploring masks.

NumPy Q,K,VScoresSoftmax weightsContext + assertions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202648:00–50:00 · 25/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Test causality and normalization

Every permitted row sums to one; every future weight is zero.

row_sum(A)=1 A[i,j>i]=0

1.00××0.300.70×0.200.300.50Key / source position →Rows: queries / receiving positions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202650:00–52:00 · 26/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Compare two implementations of one operation

NumPy and PyTorch should agree for the same inputs, scale, and mask.

Explicit NumPy operationAPyTorch scaled-dot-product APIB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202652:00–54:00 · 27/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Routing weights are not causal explanations

Changing values can alter the output while attention weights remain fixed.

Same Q,K → same attention weightsADifferent V → different retrieved contextB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202654:00–56:00 · 28/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Explain the mechanism before the architecture

State the sequence: compare, normalize, retrieve.

Query-key comparisonMask + row softmaxWeighted value sumOutput representation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202656:00–58:00 · 29/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H2 · Machine Learning in Engineering Science

Summary: compare, normalize, retrieve

Attention retrieves a query-dependent mixture of values. Masks determine which information is available.

A = softmax((QKᵀ / √dₖ) + mask) Context = AV

CompareQuery-key scores measure compatibilityNormalizeMask first; softmax across keysRetrieveUse the weights to combine valuesNEXT: add positions, MLPs, and residual paths
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202658:00–60:00 · 30/30

← → navigate · N notes · R answer · O overview · F fullscreen