Attention mechanism
An attention mechanism is a component in neural network architectures that allows the model to dynamically assign different weights to different parts of an input sequence. Instead of compressing an entire input into a single fixed-length vector, the mechanism computes a set of attention weights to determine which specific elements of the input are most relevant to the current processing step.
This mechanism is primarily used in Transformer-based models, such as Large Language Models (LLMs), and in sequence-to-sequence architectures for tasks like machine translation and image captioning. It enables the model to handle long-range dependencies by creating direct connections between distant tokens in a sequence, bypassing the linear constraints of traditional recurrent neural networks (RNNs).
Technically, attention is often implemented as a mapping of a query and a set of key-value pairs to an output. The model calculates the similarity between the query and each key to produce a probability distribution; this distribution is then used to create a weighted sum of the values. "Self-attention" is a specific variation where the queries, keys, and values all originate from the same input sequence.
For example, in the sentence "The animal didn't cross the street because it was too tired," an attention mechanism allows the model to associate the word "it" more strongly with "animal" than with "street" when calculating the representation for that token.