🎯Attention Mechanism Explained
Attention is the core of the Transformer and one of the most important structural innovations in sequence modeling in recent years. It solves the problem that RNNs and LSTMs face on long sequences — information fading as it propagates step by step — by letting the model look directly at every position in the sequence and weight them by relevance, instead of relying on a single hidden state passed along over time.
Starting from intuition: what attention does
When reading a sentence, understanding a word often requires looking back at other words. For example, "bank" means entirely different things in "the bank of the river" and "the bank where I deposit money" — the context decides. Attention turns this into something computable: for each position in the sequence, it computes how relevant every other position is, then aggregates information weighted by that relevance.
Query, Key, Value: a retrieval analogy
Attention describes this process with three vectors: Query, Key, and Value. Think of it as searching a database: the Query is what you're looking for, the Key is the label on each record, and the Value is the record's content. Compute similarity between the Query and each Key to get weights, then take the weighted sum of all Values — that is the output for this position.
Query (Q)
What the current position wants to attend to
Key (K)
What label each position can offer
Value (V)
The actual information each position carries
Attention(Q, K, V) = softmax(QKᵀ / √d_k) V
Scaled dot-product attention: compute Q–K similarity, softmax-normalize, then take a weighted sum of V
Self-attention: the sequence looking at itself
When Q, K, and V all come from the same sequence, it's called self-attention. It lets every position in the sequence interact directly with every other position, crossing any distance in a single step. This is exactly what LSTMs cannot do: two far-apart words in an LSTM must pass information along time steps, where it tends to decay or get drowned out; in self-attention, the distance between them is a single constant-cost computation.
Distance-independent
The path length between any two positions is 1, with no long-range decay
Parallelizable
Q, K, and V for all positions can be computed simultaneously, unlike sequential RNNs
Interpretable weights
Attention weights themselves show which positions the model considers important
Multi-head attention: viewing the same sequence from multiple angles
A single attention head can only learn one "relevance perspective." Multi-head attention projects Q, K, and V into multiple low-dimensional subspaces, runs attention independently in each, then concatenates the results. Different heads can focus on different patterns — some on nearby positions, some on syntactic structure, some on semantic similarity. This significantly increases the model's expressive capacity.
Positional encoding: attention has no sense of order
Self-attention is inherently order-agnostic: shuffle the sequence and the weighted sum stays the same. So Transformers must inject positional information explicitly, via positional encoding. Common approaches use fixed sine/cosine functions or learnable position vectors. Without positional encoding, the model cannot distinguish "A before B" from "B before A."
Attention vs LSTM: key differences
| Dimension | LSTM | Attention |
|---|---|---|
| Long-range dependency | Propagated step by step, prone to decay | Direct interaction, no decay |
| Parallelism | Must be sequential over time steps | All positions can be computed at once |
| Computational complexity | O(n), linear in sequence length | O(n²), quadratic in sequence length |
| Interpretability | Hidden states hard to interpret directly | Attention weights are visually intuitive |
The cost: quadratic complexity
Self-attention's compute scales quadratically with sequence length, because every position must compute similarity against every other position. The longer the sequence, the more noticeable the cost. This is why long-sequence settings introduce variants like sparse attention, local window attention, and linear attention. In browser-based training, sequence length is a parameter that must be traded off: too long slows training significantly, too short fails to capture enough context.
Why this site uses attention
AI-Spinach Lab's Transformer model is built on multi-head self-attention. Compared to LSTM, it parallelizes better on the GPU in the browser, training faster; and attention weights make it observable which historical positions the model considers more relevant, which is practically useful for understanding the prediction process. As this site repeatedly emphasizes: attention learns statistical correlations from historical data — it does not represent genuine predictive ability about the future.
A necessary reminder
Attention is powerful, but it is not a tool for "understanding causality." It learns "which combinations of positions correlate with the output in the training data," not "why." For independent random events like lottery draws, correlations in historical data are essentially without predictive value, and no amount of elegant attention weights changes that. Attention is used here for technical demonstration and learning — not for finding patterns.