Context and applications / 5 of 7

Attention mechanisms

8 minOpening your notebook…

What you’ll learn

  • Explain the main idea of “Attention mechanisms” in your own words.
  • Trace a concrete example and identify the assumptions behind it.
  • Apply the principle in a practice problem and recognize a common failure.

The idea, intuitively

Attention forms a weighted combination of information selected by relevance scores. Queries ask what is needed, keys describe what is available, and values provide the content to combine. These are learned projections, not human questions and answers.

Language varies by speaker, setting, and purpose. A text representation is useful only in relation to a task. Keep the original source when possible so you can inspect what preprocessing discarded. English performance does not establish Indonesian performance, especially for slang, dialect, or mixed-language inputs.

Under the hood

Scaled dot-product attention computes query-key scores, divides by the square root of key dimension, normalizes with softmax, and weights values. Multiple heads can learn different relations.

Attention⁡(Q,K,V)=softmax⁡(QKT/dk)V\operatorname{Attention}(Q,K,V)=\operatorname{softmax}(QK^T/\sqrt{d_k})V
Queries Q are compared with keys K. The scale uses key width dₖ. Softmax turns each row of scores into weights for mixing the value vectors V.
A useful caution

Attention weights alone are not a complete explanation of a model decision.

A worked example

To resolve a pronoun, a position may combine information from earlier nouns and verbs rather than only the immediately preceding token.

How to use this way of thinking

Trace text through normalization, tokenization, representation, and prediction. Include language variation in evaluation and inspect examples that share words but differ in meaning. For generated language, assess faithfulness and task success separately from grammatical fluency.

Try it yourself

Two values are 2 and 10 with attention weights 0.75 and 0.25. Find the output.

Key takeaways

  • Values
  • Attention weights alone are not a complete explanation of a model decision.
  • Check the assumptions with a small example before applying this idea to a larger system.

Check your understanding

0/2 checked

Use feedback to learn. Quiz scores and lesson completion are tracked separately.

1Which attention component is combined into the output?
2True or false: Attention forms a weighted combination of values.
Go to lesson completion

Further reading

Original papers and reference material for this school. External resources may be in English and require internet access.

Hugging Face NLP course (opens a new tab)Attention Is All You Need (opens a new tab)

Finish this lesson

Mark this lesson complete when you’re ready to continue.

Start with a question

Introduction to AIAI FoundationsA brief history of AIAI FoundationsTypes of AIAI FoundationsAI, ML, and deep learningAI FoundationsUnderstanding generative AIAI FoundationsEthics, limitations, and responsibilityAI FoundationsLinear algebra for learning systemsMathematics for AIVectors, matrices, and similarityMathematics for AICalculus: measuring changeMathematics for AIDerivatives and gradientsMathematics for AIProbability and statisticsMathematics for AIOptimization and objectivesMathematics for AI
K to open · Escape to close