Skip to content
StackDecode
AI & ML Atlas
Deep LearningtechniqueInteractive simulation

Attention Mechanism

Lets a model focus on the most relevant parts of its input for each output.

The idea

For every output step, score how relevant each input position is, turn the scores into weights with a softmax, and take the weighted average of the input representations. The model can then "look at" the right words, however far away they are, instead of relying on one compressed summary.

weights = softmax(scores), context = Σ weightsᵢ · valueᵢ

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

thecatsatonthemat
  • positive
  • negative
  • attention weight

Take the sentence. We will compute what the word "cat" (highlighted) should pay attention to.

1/4

The vectors here are random, so the pattern of attention is arbitrary. In a trained model these weights are learned. Pick another query word with the slider.

Step by step

  1. 1Compare the current query with every input key to get scores.
  2. 2Apply softmax so the scores become weights that sum to 1.
  3. 3Take the weighted sum of the values.
  4. 4Use that context for the next prediction.

Where it is used

Translation, captioning, and the core of transformers.

Watch out for

Attention weights are only a partial explanation of why a model decided something.

Related