
CleanShot 2024-10-21 at 7 .39.13@2x.png
Self-attention is a mechanism used in transformers that computes more effective input representations by allowing each position in a sequence to interact with all other positions in the same sequence and weigh their importance.
"Self" in Self-Attention
In self-attention, "self" refers to the mechanism's ability to compute attention weights by relating different positions within a single input sequence. It evaluates and learns the relationships and dependencies among the parts of the input itself, such as words in a sentence or pixels in an image.
This contrasts with traditional attention mechanisms, where the focus is on relationships between elements of two different sequences—for example, in sequence-to-sequence models, attention may be focused between the input sequence and the output sequence.
Breaking Down the Context Vector

CleanShot 2024-10-21 at 8 .42.42@2x.png
Consider the sentence "Your journey starts with one step." Each word is processed by a three-dimensional embedding and then multiplied by a coefficient a for every other input value to compute our context vector . Each context vector can in turn be interpreted as a more meaningful embedding vector.
Context vectors play a key role in self-attention. Their purpose is to create a rich representation of each element in an input sequence (such as a sentence) by combining information from all the other elements in the sequence. This is crucial in LLMs, which need to understand the relationships and relevance among words in a sentence.
Computing the Attention Score

CleanShot 2024-10-22 at 7 .39.28@2x.png
The figure above shows the attention score between the query and every other element, computed as a dot product.
A higher dot product indicates greater alignment or similarity between vectors. In the context of self-attention, the dot product determines how much each element in the sequence attends to, or "pays attention to," any other element: the higher the dot product, the higher the similarity and attention score between the two elements.
Normalizing the Attention Score
The main goal of normalization is to obtain attention weights that sum to 1. This normalization is a convention that is useful for interpretation and for maintaining training stability in large language models (LLMs).

CleanShot 2024-10-22 at 7 .54.19@2x.png
After normalization, the result is as follows:
Attention weights: tensor([0.1455, 0.2278, 0.2249, 0.1285, 0.1077, 0.1656]) Sum: tensor(1.0000)
Computing the Context Vector

CleanShot 2024-10-22 at 8 .13.30@2x.png
After computing and normalizing the attention scores to obtain the attention weights for query , the next step is to compute the context vector . This context vector is a weighted combination of all input vectors through according to the attention weights.
Computing Attention Weights for All Input Tokens

CleanShot 2024-10-23 at 7 .48.40@2x.png
The second row is the attention weight we computed for .
Let's review the previous calculation method.
CleanShot 2024-10-23 at 7 .50.30@2x.png