Self-Attention and Multi-Head Attention in NLP
In the last blog, we learn about attention and why it became such an important idea in sequence modeling. It solved a real problem in…
Self-Attention and Multi-Head Attention in NLP
In the last blog, we learn about attention and why it became such an important idea in sequence modeling. It solved a real problem in encoder–decoder models instead of forcing the decoder to depend only on one fixed-length vector, attention allowed it to look back at the most relevant parts of the input while generating each output token. That already made sequence models much better.
Researchers started asking a new question: what if attention is not just something used between the encoder and decoder? What if it can also work inside the sequence itself? That question leads directly to self-attention.
Why attention needed one more step
In encoder–decoder attention, the decoder is usually trying to find the most relevant parts of the encoder output. That is already powerful. But inside a sentence itself, words also depend on other words in the same sentence. A word does not only need a decoder to look back at it. Sometimes a word needs to directly “see” other words in the same input sequence to be represented properly.
self-attention let the model build better representations by allowing every token to look at other tokens in the same sequence.
What self-attention really means
The self-attention means For each word in the input, let the model decide which other words in the same input matter most for understanding it.
If the sentence is long, not every word matters equally for every other word. Some words are strongly connected. Some are not. Self-attention allows the model to aggregate information from all the other words and generate a new representation for each word that is informed by the entire context.
So a token is no longer represented only by itself. It becomes represented by itself plus the parts of the sentence that matter most for it.
A simple sentence intuition
for example, The animal did not cross the street because it was too tired.
When the model processes the word it, the important question is what does it refer to?
A self-attention mechanism can help the model look across the sequence and assign stronger weight to the words that matter for that decision.
This is one reason self-attention became so powerful. It gave every token access to the full sequence context.
Query, key, and value
Once self-attention is introduced more formally, three words appear everywhere
- the query is what the current token is looking for
- the keys are what the available tokens offer for comparison
- the values are the actual information that gets combined in the output
And in self-attention, all of these come from the same input sequence.
Why self-attention felt different from RNNs
With RNNs, information flows step by step through time. That means if a word at the beginning of the sentence influences a word at the end, the signal has to travel through many recurrent steps.
RNNs say process one step after another and carry memory forward
Self-attention says let every token directly look at every other relevant token
So what is multi-head attention?
Once self-attention makes sense, the next natural question is Why stop at one attention mechanism?
What if one attention pattern is good at learning one kind of relationship, but another attention pattern could focus on a different kind of relationship in the same sentence?
multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.
That sentence is one of the most important descriptions of multi-head attention.
In simpler words instead of having just one attention view of the sentence, let the model have several attention views at the same time.
Each head can learn something slightly different. Suppose one attention head becomes good at finding grammatical links. Another head becomes good at looking for long-distance dependencies. Another head becomes good at finding words related to sentiment. Another head becomes good at focusing on entities or references.
It gives the model multiple ways to look at the same sequence.
Why multi-head attention is better than a single attention head
A single attention mechanism can still be useful. But if one head only learns one style of relationship, that can be limiting.
Multi-head attention gives the model more flexibility
- different heads can specialize differently
- attention can happen in different learned subspaces
- the model can combine several useful relational patterns at once
Why this mattered so much for NLP
Self-attention and multi-head attention did more than improve one model.
Earlier neural NLP relied heavily on recurrence. But once self-attention showed that every token could directly gather information from the full sequence, and multi-head attention showed that this could be done in several ways at once, the field had the pieces needed for a new architecture.
Self-attention was not just a useful add-on. It became the core mechanism that replaced recurrence in Transformers.
Final thoughts
Self-attention and multi-head attention were some of the most important ideas in the history of NLP.
Self-attention gave every token a way to look across the whole sequence and build a richer representation from the words that matter most. Multi-head attention made that even stronger by letting the model attend in several different ways at once.
메타데이터
- post_id
- ce2682c6668c
- slug
- self-attention-and-multi-head-attention-in-nlp-ce2682c6668c
- url
- https://medium.com/@emurugayathri/self-attention-and-multi-head-attention-in-nlp-ce2682c6668c
- canonical_url
- https://medium.com/@emurugayathri/self-attention-and-multi-head-attention-in-nlp-ce2682c6668c
- author_url
- https://medium.com/@emurugayathri
- status
- ok
- fetched_at
- 2026-06-09 15:37:30