AttentionL'attention

TRA6008 S2 · in classS2 · en classe

But which context?

Reading in both directions settled one question, whether the word that decides comes before or after (see also the Contextual embeddings chapter) (voir aussi le chapitre Plongements contextuels). It leaves another. Once every word is connected to every other, not all of those connections are worth the same, and the more context there is, the less of it counts equally.

Reading the bank was steep, you connect bank with steep and settle on the riverside sense without noticing you did it. The attention mechanism is the transformer’s version of that reflex: for each word, attention weights say how much every other word in the text should influence its representation.

Too tired, too wide

The classic demonstration comes from the transformer paper itself (Vaswani et al., 2017; visualization by Vig, 2019). Compare:

  • the animal didn’t cross the street because it was too tired
  • the animal didn’t cross the street because it was too wide

Changing a single word at the very end changes the meaning of the whole sentence, by changing which word we link it to: a tired it is the animal, a wide it is the street. Inside a trained transformer, the attention weights from it visibly shift between the two sentences, toward animal in the first and toward street in the second.

Two copies of the sentence 'the animal didn't cross the street because it was too ...', ending in tired and in wide. In each, an arc runs from it to every other word, thicker where the attention weight is higher. In the tired version the heaviest arc lands on animal; in the wide version it lands on street.
Figure 4. Attention from the pronoun to every word of the sentence, thicker where the weight is higher. One changed word at the end moves the heaviest arc from the animal to the street. Drawn after the visualization in Vig (2019).

Nobody programmed the fact that streets are wide and animals get tired. That came out of patterns across millions of sentences: the distributional idea again, now applied to relationships inside a sentence rather than to which words resemble each other.

Learned once, worked out every time

Two different things are called weights here, and they are worth telling apart.

So the arcs in the figure above were not stored anywhere and looked up. They were computed for that sentence, and the other sentence gives other arcs.

This is the machinery the contextual-embeddings chapter left unexplained: the vector for this occurrence of bank is assembled from the words its attention selected, in the proportions it learned (see also the Contextual embeddings chapter) (voir aussi le chapitre Plongements contextuels).

So what is self-attention?

Attention always runs between two sets of words: the ones being given a representation, and the ones they draw on. Self-attention is the case where both sets are the same text. That happens everywhere above, each word of the sentence weighted against the other words of that same sentence.

The other case runs between two different texts, a source and the translation being written from it (see also the Encoders and decoders page) (voir aussi la page Encodeurs et décodeurs). That use came first: attention was built for translation (Bahdanau et al., 2015), and the step taken in 2017 was to use it inside a single text as well (Vaswani et al., 2017).

Three panels, one colour each. Cross-attention in terracotta: two rows, a French source above and its English translation below, with an arrow from each word of the translation up to the source. Self-attention in green: one row of four words with arcs joining every pair, an arrowhead at each end. Masked self-attention in plum: the same row and arcs, but each arrow points only forwards, to the later word of the pair.
Figure 5. The same mechanism in the three configurations a translator is likely to come across, in the order they appeared. Each keeps its colour wherever it turns up again. Others exist and are left aside here.

Masking starts to matter once a model writes rather than reads (see also the Encoders and decoders page) (voir aussi la page Encodeurs et décodeurs).

Written in English. The French version was automatically generated and I have not revised it. Rédigé en anglais. La version française a été générée automatiquement et je ne l'ai pas révisée. Updated 2 October 2026

Mais quel contexte ?

Lire dans les deux sens a réglé une question, celle de savoir si le mot décisif vient avant ou après (see also the Contextual embeddings chapter) (voir aussi le chapitre Plongements contextuels). Il en reste une autre. Dès que chaque mot est relié à tous les autres, toutes ces liaisons ne valent pas la même chose, et plus il y a de contexte, moins toutes ses parties comptent autant.

En lisant the bank was steep, vous rapprochez bank de steep et vous retenez le sens du bord de rivière sans vous en apercevoir. Le mécanisme d’attention est la version qu’en donne le transformeur : pour chaque mot, des poids d’attention disent à quel point chacun des autres mots du texte doit influencer sa représentation.

Trop fatigué, trop large

La démonstration classique vient de l’article même du transformeur (Vaswani et al., 2017 ; visualisation de Vig, 2019). Comparez :

  • the animal didn’t cross the street because it was too tired
  • the animal didn’t cross the street because it was too wide

Changer un seul mot tout à la fin change le sens de la phrase entière, en changeant le mot auquel on rattache it : un it fatigué est l’animal, un it large est la rue. Dans un transformeur entraîné, les poids d’attention depuis it se déplacent visiblement entre les deux phrases, vers animal dans la première et vers street dans la seconde.

Deux copies de la phrase « the animal didn't cross the street because it was too… », l'une finissant par tired, l'autre par wide. Dans chacune, un arc va de it vers chaque autre mot, plus épais là où le poids d'attention est plus élevé. Dans la version tired, l'arc le plus épais aboutit à animal ; dans la version wide, il aboutit à street.
Figure 4. L'attention depuis le pronom vers chaque mot de la phrase, plus épaisse là où le poids est plus élevé. Un seul mot changé à la fin déplace l'arc le plus épais de l'animal vers la rue. D'après la visualisation de Vig (2019).

Personne n’a programmé le fait que les rues sont larges et que les animaux se fatiguent. Cela est ressorti de motifs relevés dans des millions de phrases : l’idée distributionnelle de nouveau, appliquée cette fois aux relations à l’intérieur d’une phrase plutôt qu’à la ressemblance entre les mots.

Appris une fois, établi chaque fois

Deux choses différentes s’appellent ici des poids, et elles valent la peine d’être distinguées.

Les arcs de la figure ci-dessus n’étaient donc rangés nulle part avant d’être consultés. Ils ont été calculés pour cette phrase, et l’autre phrase donne d’autres arcs.

C’est le mécanisme que le chapitre sur les plongements contextuels laissait inexpliqué : le vecteur de cette occurrence de bank est assemblé à partir des mots que son attention a retenus, dans les proportions apprises (see also the Contextual embeddings chapter) (voir aussi le chapitre Plongements contextuels).

Alors, qu’est-ce que l’autoattention ?

L’attention s’exerce toujours entre deux ensembles de mots : ceux à qui l’on donne une représentation, et ceux dans lesquels ils puisent. L’autoattention est le cas où les deux ensembles sont le même texte. C’est ce qui se passe partout ci-dessus, chaque mot de la phrase pesé par rapport aux autres mots de cette même phrase.

Dans l’autre cas, elle s’exerce entre deux textes différents, un texte de départ et la traduction qu’on en écrit (see also the Encoders and decoders page) (voir aussi la page Encodeurs et décodeurs). Cet usage est venu le premier : l’attention a été conçue pour la traduction (Bahdanau et al., 2015), et le pas franchi en 2017 a été de s’en servir aussi à l’intérieur d’un seul texte (Vaswani et al., 2017).

Trois panneaux, une couleur chacun. L'attention croisée en terre cuite : deux rangées, un texte de départ français au-dessus et sa traduction anglaise au-dessous, avec une flèche de chaque mot de la traduction vers le texte de départ. L'autoattention en vert : une rangée de quatre mots avec des arcs joignant chaque paire, une pointe de flèche à chaque extrémité. L'autoattention masquée en prune : la même rangée et les mêmes arcs, mais chaque flèche ne pointe que vers l'avant, vers le mot le plus tardif de la paire.
Figure 5. Le même mécanisme dans les trois configurations qu'une traductrice ou un traducteur est susceptible de rencontrer, dans l'ordre de leur apparition. Chacune garde sa couleur partout où elle réapparaît. D'autres existent et sont laissées de côté ici.

Le masquage commence à compter dès qu’un modèle écrit au lieu de lire (see also the Encoders and decoders page) (voir aussi la page Encodeurs et décodeurs).

Written in English. The French version was automatically generated and I have not revised it. Rédigé en anglais. La version française a été générée automatiquement et je ne l'ai pas révisée. Mise à jour 10 septembre 2026

Sources

Sources

Spotted an error? Une erreur ?

Language, content, a broken link: e-mail me what you saw and I will fix it. Langue, contenu, lien brisé : écrivez-moi ce que vous avez vu et je corrige.

ayla.rigouts.terryn@umontreal.ca

Please mention the page: Mentionnez la page :