Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Self-attention is a key idea behind many modern AI systems, including transformer models used for translation, search, chatbots, and image tools. It helps a model decide which words or data points are most important to focus on when making a prediction. Instead of reading a sentence only from left to right, self-attention lets every word compare itself with every other word.

This matters because meaning often depends on relationships between words that may be far apart.

Understanding AI & Machine Learning: Self-Attention Explained

Before attention can work, the input is changed into lists of numbers called embeddings. These numbers represent patterns learned from huge amounts of training data. The model then creates three new versions of each embedding.

One version acts like a request for useful context. Another acts like a label that says what kind of information the token offers. The third carries the information that may be passed onward.

These versions are made by learned weight tables. During training, the tables slowly change so that useful links receive stronger weights for the task.

The model compares a request with the labels from the whole input. A strong match produces a larger raw score. Very large raw scores can make learning unstable, so the scores are reduced by a scale based on the size of the number lists.

Next, a process called softmax changes the scores into a set of positive weights. Their total is one. This makes the result behave like a weighted mixture.

Information from highly weighted tokens has a large effect on the new representation, while weakly weighted tokens have little effect. Each token leaves this step with a context-aware representation rather than its original fixed embedding.

Word order needs extra help. Attention by itself does not know whether one word came before another. Transformer models therefore add position information to each token embedding.

This can be a fixed pattern or a learned set of position values. Text generators use another important rule called a causal mask. When predicting the next token, the model is blocked from viewing later tokens in the training sentence.

Without this rule, it could copy answers from the future and appear far better than it really is. Other models, such as those built to understand a complete passage, may examine both earlier and later context.

Several attention heads work in parallel within one layer. Their outputs are combined, then passed through further learned layers. One head may become sensitive to nearby grammar.

Another may link a pronoun to a noun earlier in the text. A different head may notice punctuation, repeated terms, or the structure of computer code. These roles are not assigned by programmers.

They emerge from training, and they are not always neat or easy to interpret. Students should avoid treating an attention weight as a perfect explanation of a model decision.

A model can make mistakes because its training examples were incomplete, biased, or misleading. Attention helps it use context, but it does not give it human understanding or a built in method for checking whether a claim is true.

Key Facts

  • Self-attention lets each token compare itself with all other tokens in the input.
  • A token can be a word, part of a word, a symbol, or another piece of data.
  • Attention score = similarity(query, key), often using a dot product.
  • Scaled dot-product attention uses Attention(Q, K, V) = softmax(QK^T / sqrt(dk))V.
  • Softmax turns raw attention scores into weights that add to 1.
  • Multi-head attention runs several attention patterns at once so the model can track different relationships.

Vocabulary

Self-attention
A method that lets each token in a sequence decide how strongly it should use information from the other tokens.
Token
A small piece of input data, such as a word, part of a word, number, or symbol, that a model processes.
Query
A vector that represents what a token is looking for when it compares itself with other tokens.
Key
A vector that represents what information a token offers for comparison with queries.
Value
A vector that contains the information passed forward after attention weights are applied.

Common Mistakes to Avoid

  • Thinking self-attention is the same as human attention is wrong because it is a mathematical weighting process, not awareness or understanding.
  • Ignoring word order is wrong because transformers usually add position information so the model can tell the difference between sentences with the same words in different orders.
  • Treating the largest attention weight as the only important one is wrong because the final output is usually a weighted mix of many values.
  • Forgetting to scale the dot product is wrong because large scores can make softmax too extreme and can make learning less stable.

Practice Questions

  1. 1 A token has attention weights 0.50, 0.30, and 0.20 for three value numbers 10, 4, and 7. What weighted output does it produce?
  2. 2 For a sentence with 6 tokens, each token compares with every token including itself. How many query-key comparisons are made in one self-attention layer?
  3. 3 In the sentence 'The robot moved the box because it was heavy,' explain why self-attention can help the model decide what the word 'it' refers to.