This article was originally published on my blog. For the latest version and future updates, please visit the original post: https://jaketao.com/language/en/ai-attention-mechanisms
Press enter or click to view image in full size
Large Language Models (LLMs) have already reshaped the way we interact with artificial intelligence, and we've grown accustomed to conversing with large models like ChatGPT and Claude. But does AI truly understand human language? How does it read through tens of thousands of words in a very short time and grasp the key information? The answer lies in the attention mechanism. This article explores how AI learned to "look back" --- that is, the origins of the attention mechanism.
I. The Human Instinct for Reading
Machine translation was one of the earliest tasks to reveal that "AI cannot allocate attention." Let's look at a few simple examples.
- He plays basketball every weekend. → play
- Remember to call me before you leave work. → call
- Don't hit the child. → hit
Even though the Chinese character "打" is the same, the English translations are completely different. What determines the translation isn't the character "打" itself, but the words it's paired with: basketball, phone, and child. During the translation process, we don't focus solely on a single word either. For example, when translating "play basketball," while paying close attention to "basketball," we also glance at "he" and "every weekend" to confirm the subject and tense.\
When humans understand a sentence, they spontaneously allocate different levels of attention to different words. Early language models, however, processed words one by one in sequence. Take this example: a text begins with a noun, and a pronoun referring to it doesn't appear until several dozen words later. When we read, we can easily skip over the intervening passage and match the pronoun with the noun at the very beginning.\
Selective comprehension based on context is almost instinctive for humans. Yet for early AI, this was precisely what it could not do; it lacked the ability to freely establish connections across distances and allocate attention as needed.
II. The "Sticky Note" Bottleneck
How did researchers at the time attempt to get AI to understand sentences? To solve the problems of digital text representation and temporal memory, researchers first built a set of foundational tools. Here, let's clarify two core concepts.\
1. Word vectors. Computers do not recognize text; they only recognize numbers. Therefore, before a model can process text, it must first convert each word into a string of numbers. For example, the word "book" might be represented as [0.21, -0.53, 0.88, ...]; this string of numbers is called a word vector. Words with similar meanings have word vectors that are also relatively close: "book" and "magazine" are close to each other, while "book" is far from "car."\
2. Recurrent Neural Networks (RNNs). At the time, this was the most commonly used model for processing text in deep learning (though in practice, its improved version, LSTM, was more widely used). Its defining characteristic is sequential processing: it reads one word at a time, and with each new word, it combines that word with its previous memory to update the model. This continuously updated memory is technically referred to as the hidden state.\
This "sequential processing" presents a problem: information from the first word must travel step by step through all the words in between before reaching the target word. The journey is too long, causing the information to degrade along the way; by the time it arrives, it has long since become vague and unclear.\
With these two concepts in mind, let's examine the neural machine translation model that emerged in 2014. It is called the Sequence-to-Sequence (Seq2Seq) model, consisting of two RNNs, also known as the Encoder-Decoder architecture.\
The encoder is responsible for "reading": it processes the word vectors of the source text one by one, continuously updating the hidden state as it does so. Once the last word is processed, the final hidden state represents the meaning of the entire sentence. It is a fixed-length vector called the context vector, which can be thought of as a "sticky note" with limited space. The decoder is responsible for "writing": it cannot see the source text; instead, it uses this sticky note to write the translation one word at a time.\
The problem lies with that sticky note. Whether the source text is 5 words or 50 words, this sticky note remains the same size. When the sentence is short, it's fine --- the sticky note can accommodate it. But as the sentence gets longer, the text becomes increasingly crammed; earlier content is overwritten by what comes later, and details are gradually lost. Researchers refer to this problem as the information bottleneck.\
For example, take the sentence "I bought a book yesterday" --- the model can translate it quite accurately. But what if we try a longer sentence? "The German engineer I met at a meeting in Shanghai last month emailed me yesterday to say that his company plans to open a new R&D center in Hangzhou next quarter." This sentence contains a great deal of information: time (last month, yesterday, next quarter), place (Shanghai, Hangzhou), people (German engineer), and events (met at a meeting, sent an email, opening an R&D center). Early models often encountered two common problems when translating sentences like this.\
First, they omitted information.
- The engineer emailed me yesterday that his company plans to open a new R&D center in Hangzhou next quarter.
The second half of the sentence is translated completely, but the details from the first half --- "last month," "met at a meeting in Shanghai," and "German" --- are all missing.\
Second, the information gets jumbled.
- The German engineer I met in Beijing last year emailed me...
"Shanghai" became "Beijing," and "last month" became "last year."\
The reason is easy to understand: the encoder reads from beginning to end. By the time it reaches the end of the sentence, most of the space on the sticky note has already been filled by the second half of the sentence --- terms like "R&D center," "Hangzhou," and "next quarter" --- leaving only a faint trace of the information at the beginning. Yet when the decoder writes in English, the information at the beginning of the sentence is what it needs first. Experiments at the time confirmed this: once a sentence exceeds twenty or thirty words in length, the model's translation quality drops significantly.\
If a professional translator were handling a long sentence, what would they do? Would they memorize the entire sentence first, then close the source text and translate from memory? No, they would translate while looking at the source text. When they reach the subject, they glance at the subject in the source text; when they reach a temporal adverbial, they go back to find it. For them, the source text is always on the table --- they look wherever they need to. The problem with early AI was that it was required to "close the source text." Could we make AI look at the source text the whole time as well?
III. The Wisdom of Open-Book Translation
In 2014, Dzmitry Bahdanau and his colleagues provided the answer in a paper on machine translation. Teaching AI to "translate with the text open" involves three steps.\
Step 1: Retain the hidden state for every word. As mentioned earlier, the encoder updates the hidden state each time it reads a word. Previously, only the final hidden state was used; now, the hidden state from every step is retained, effectively creating a set of "notes" for each word in the source text.\
Step 2: Assign a score to the source text for each word generated. When the decoder prepares to generate the next word, it compares its current state with each set of "notes" from the source text one by one, assigning a "relevance score" to each. These scores are then converted into proportions that sum to 100%. This proportion is called the attention weight.\
Step 3: Proportional blending. By mixing these notes according to their weights, we obtain a context vector specific to this step.\
The biggest difference from the past is this: previously, an entire sentence had only one fixed context vector --- essentially a single sticky note; now, every time a word is written, a new one is generated on the spot, with its content changing based on the word currently being written. This effectively bypasses the information bottleneck.\
Let's first look at the simplest example to see how the weights are distributed; the mechanism remains the same even for longer sentences.
- Original text: I / yesterday / bought / a / book
- Translation: I bought a book yesterday
When the model writes each English word, the attention weights are distributed roughly as follows:
English word writtenCorresponding original wordAttention weightI我约 90%bought买了约 85%a一本约 70%book书约 90%yesterday昨天约 85%
"昨天" is the second word in the Chinese sentence, but in English, it is placed at the end of the sentence. The model does not translate by matching words one-to-one based on position: when it reaches "yesterday," it re-scores each word in the source text; "yesterday" receives about 85%, while "book," "bought," and others are left with only a few percent --- the so-called "jumping back" occurs because the weights automatically concentrate on "yesterday." Therefore, it doesn't matter if the positions don't match: the English "yesterday" is at the end of the sentence, while the Chinese "昨天" is in the second position; the model finds it based on weights, not position.\
These weights aren't taught by humans; the training data consists only of paired source and target texts, with no human annotation indicating that "bought" corresponds to "买了." During training, every time the model writes a word incorrectly, the system fine-tunes its internal parameters (including the components that determine the weights). The next time it "looks at the correct position" in the same spot, it scores higher; after repeating this process millions of times, the model gradually understands and learns "where to look."\
If we visualize these weights as a graph --- with darker colors representing higher weights --- we get what is commonly known as an attention heatmap, which intuitively shows where in the source text the model is "looking" when translating each word. With the introduction of attention, the quality of long-sentence translation no longer deteriorates as the sentence progresses. Soon, it was applied to tasks such as text summarization, speech recognition, and image captioning, becoming a standard component in natural language processing.
IV. The Significance of Looking Back
In 2014, attention was still just an auxiliary module attached to RNNs. Models still had to read word by word in sequence, which made training slow and made it difficult to handle very long texts. In 2017, the paper "Attention Is All You Need" took this idea a giant leap forward: it removed the RNN and made attention the core of the model, allowing every word in a sentence to directly see all other words. This architecture is called the Transformer, and today's large models --- such as GPT and Claude --- have all evolved from it.\
Looking back on this journey, attention allows the model to look back at the source text every time it generates a word, selecting the necessary parts based on weights --- and it learns on its own "where to look." It may not truly "understand" human language, but for the first time, it has learned to glance at the right places, just like a human.
