The Bottleneck Problem
Traditional recurrent neural networks (RNNs) like LSTMs and GRUs struggled with long sequences due to the ‘bottleneck’ problem. The entire input sequence was compressed into a fixed-size context vector, inevitably losing information about earlier parts of the sequence.
Introducing Attention: Weighted Context
Attention mechanisms address this by allowing the model to directly access all previous hidden states when processing a new input. Instead of a single context vector, attention assigns weights to each part of the input sequence based on its relevance.
α(i) = softmax(q(i); h_1, ..., h_T)
Self-Attention: Intra-Sequence Relationships
Self-attention, popularized by the Transformer architecture, calculates attention scores within a single sequence. Each element attends to all other elements in the same sequence, learning internal dependencies and relationships.
Attention(Q, K, V) = softmax((QK^T)/sqrt(d_k))V
Cross-Attention: Bridging Sequences
Cross-attention extends self-attention to multiple sequences. For example, in machine translation, one sequence (the encoder) attends to another (the decoder), allowing the decoder to focus on relevant parts of the source sentence.
Frequently asked questions
What is a ‘query’, ‘key’ and ‘value’ in attention?
These are learned projections of the input sequence. The query represents what you're looking for, the key represents what's available, and the value contains the actual information.
Why is softmax used in attention?
Softmax converts the attention scores into probabilities, ensuring they sum to 1 and represent a distribution over the input sequence.
What are some real-world applications of attention mechanisms?
Attention is used extensively in machine translation, image captioning, speech recognition, and many other areas where sequential data processing is crucial.
Try it live
Everything above runs in your browser — open SPH Fluid and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open SPH Fluid simulation