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.
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.
- The model’s weights weightpoids machine learningapprentissage automatique A real number attached to one input of a computing unit, learned during training, that sets how much that input counts towards the unit's output.Nombre réel attaché à une entrée d'une unité de calcul, appris pendant l'entraînement, qui détermine l'importance de cette entrée dans la sortie de l'unité. are learned. Everything the model picked up from its training text is in them, and every step of the computation runs through them. Training adjusts them over a large amount of text, and they stop changing when it ends (see also the From neural networks to large language models chapter, in the Foundations of AI & NLP module) (voir aussi le chapitre Des réseaux de neurones aux grands modèles de langue, dans le module Fondements de l'IA et du TAL).
- The attention weights are worked out. They say how much each word of the text at hand should draw on each other word. The model computes them from those words, using the weights it learned, and computes them again for the next text. Nothing about the model changes while it does.
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).
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).
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.
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 poids weightpoids machine learningapprentissage automatique A real number attached to one input of a computing unit, learned during training, that sets how much that input counts towards the unit's output.Nombre réel attaché à une entrée d'une unité de calcul, appris pendant l'entraînement, qui détermine l'importance de cette entrée dans la sortie de l'unité. du modèle sont appris. Tout ce que le modèle a retenu de ses textes d’entraînement s’y trouve, et chaque étape du calcul passe par eux. L’entraînement les ajuste sur une grande quantité de texte, et ils cessent de changer quand il se termine (see also the From neural networks to large language models chapter, in the Foundations of AI & NLP module) (voir aussi le chapitre Des réseaux de neurones aux grands modèles de langue, dans le module Fondements de l'IA et du TAL).
- Les poids d’attention sont établis. Ils disent à quel point chaque mot du texte en cours doit puiser dans chacun des autres. Le modèle les calcule à partir de ces mots, avec les poids qu’il a appris, et les calcule de nouveau pour le texte suivant. Rien du modèle ne change pendant ce temps.
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).
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).
Sources
- Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural Machine Translation by Jointly Learning to Align and Translate. Third International Conference on Learning Representations (ICLR 2015), oral presentation; arXiv:1409.0473. https://arxiv.org/abs/1409.0473
- Stanford HAI (n.d.). What is Attention Mechanism?. Stanford HAI, AI definitions. https://hai.stanford.edu/ai-definitions/what-is-attention-mechanism
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., et al. (2017). Attention Is All You Need. Advances in Neural Information Processing Systems 30 (NIPS 2017). https://papers.nips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html
- Vig, J. (2019). A Multiscale Visualization of Attention in the Transformer Model. Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics: System Demonstrations, 37–42. https://aclanthology.org/P19-3007/
Sources
- Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural Machine Translation by Jointly Learning to Align and Translate. Third International Conference on Learning Representations (ICLR 2015), oral presentation; arXiv:1409.0473. https://arxiv.org/abs/1409.0473
- Stanford HAI (n.d.). What is Attention Mechanism?. Stanford HAI, AI definitions. https://hai.stanford.edu/ai-definitions/what-is-attention-mechanism
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., et al. (2017). Attention Is All You Need. Advances in Neural Information Processing Systems 30 (NIPS 2017). https://papers.nips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html
- Vig, J. (2019). A Multiscale Visualization of Attention in the Transformer Model. Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics: System Demonstrations, 37–42. https://aclanthology.org/P19-3007/