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


W07 · H2 · 15:00–16:00 · 19 October 2026
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


W07 · H2 · Machine Learning in Engineering Science
Compare a query with keys, then use normalized weights to combine values.
Goal: compute a context vector and explain a causal mask.


W07 · H2 · Machine Learning in Engineering Science
A translation decoder may emphasize different source positions at different steps.


W07 · H2 · Machine Learning in Engineering Science
LSTM or GRU encoders can supply states to an attention mechanism.
2014/15: recurrent attention 2017: Transformer


W07 · H2 · Machine Learning in Engineering Science
The decoder can retrieve from H instead of using only the final state.
H = [h₁; h₂; …; hₙ]


W07 · H2 · Machine Learning in Engineering Science
The query is a vector used to compare the current need with memory keys.
q ∈ ℝᵈᵏ


W07 · H2 · Machine Learning in Engineering Science
Matching uses keys; the retrieved combination uses values.
K ∈ ℝᴺˣᵈᵏ V ∈ ℝᴺˣᵈᵛ


W07 · H2 · Machine Learning in Engineering Science
A score compares one query with one key before normalization.
dot score: eⱼ = q · kⱼ


W07 · H2 · Machine Learning in Engineering Science
An MLP can compare a decoder query with each encoder key.
eᵢⱼ = vₐᵀ tanh(Wq qᵢ + Wk kⱼ + b)


W07 · H2 · Machine Learning in Engineering Science
Divide by √dₖ so high-dimensional dot products are less likely to saturate softmax.
S = QKᵀ / √dₖ


W07 · H2 · Machine Learning in Engineering Science
Normalize separately for each query over its available keys.
aᵢⱼ = exp(sᵢⱼ) / Σₘ exp(sᵢₘ)


W07 · H2 · Machine Learning in Engineering Science
Each value contributes according to its attention weight.
cᵢ = Σⱼ aᵢⱼ vⱼ


W07 · H2 · Machine Learning in Engineering Science
Rows are query destinations; columns are key sources.
(Nq × dₖ)(dₖ × Nk) → Nq × Nk


W07 · H2 · Machine Learning in Engineering Science
The values are deliberately small so all intermediates can be checked.
q=[1,0] K=[[1,0],[0,1],[1,1]]


W07 · H2 · Machine Learning in Engineering Science
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.


W07 · H2 · Machine Learning in Engineering Science
Use the same weights for every value coordinate.
context ≈ [8.0222, 5.9889]


W07 · H2 · Machine Learning in Engineering Science
Attention can route the same memory differently for each request.


W07 · H2 · Machine Learning in Engineering Science
Q, K, and V come from learned projections of the same representations.
Q=XWQ K=XWK V=XWV


W07 · H2 · Machine Learning in Engineering Science
Queries come from one sequence; keys and values come from another.
Q=YWQ K=XWK V=XWV


W07 · H2 · Machine Learning in Engineering Science
Before softmax, set scores for key positions j>i to −∞.
allowed(i,j) ⇔ j ≤ i


W07 · H2 · Machine Learning in Engineering Science
Padding and causality enforce different constraints.


W07 · H2 · Machine Learning in Engineering Science
Softmax requires at least one permitted source per query.
all scores = −∞ → no valid normalization


W07 · H2 · Machine Learning in Engineering Science
A query can access a distant key within one dense attention layer.


W07 · H2 · Machine Learning in Engineering Science
Doubling context length quadruples score entries at fixed head count.
per head: T × T scores


W07 · H2 · Machine Learning in Engineering Science
Verify your hand calculation before exploring masks.


W07 · H2 · Machine Learning in Engineering Science
Every permitted row sums to one; every future weight is zero.
row_sum(A)=1 A[i,j>i]=0


W07 · H2 · Machine Learning in Engineering Science
NumPy and PyTorch should agree for the same inputs, scale, and mask.


W07 · H2 · Machine Learning in Engineering Science
Changing values can alter the output while attention weights remain fixed.


W07 · H2 · Machine Learning in Engineering Science
State the sequence: compare, normalize, retrieve.


W07 · H2 · Machine Learning in Engineering Science
Attention retrieves a query-dependent mixture of values. Masks determine which information is available.
A = softmax((QKᵀ / √dₖ) + mask) Context = AV
← → navigate · N notes · R answer · O overview · F fullscreen