Attention playgroundLaboratorio dell'attention

A transformer of one or two layers, written here with its own backward pass, is trained in the page to reverse, sort or recall a short sequence of digits, and one can watch its attention form. In every head each position emits a query \(q\) that is compared with the keys \(k\) of the positions it may see, and the softmax of the scores weights their values \(v\); on the reverse task each position of the answer learns to look at its mirror image in the prompt, and therefore the matrix becomes anti-diagonal. The recall task (a b … a → b) needs two layers, since the token that follows the earlier copy of the query must first be found and then read.

Un transformer di uno o due strati, scritto qui con il suo passo all'indietro, è addestrato nella pagina a invertire, ordinare o richiamare una breve sequenza di cifre, e si può osservare il formarsi della sua attention. In ogni testa ciascuna posizione emette una query \(q\) che è confrontata con le chiavi \(k\) delle posizioni che può vedere, e la softmax dei punteggi pesa i loro valori \(v\); nel compito di inversione ogni posizione della risposta impara a guardare la sua immagine speculare nel prompt, e quindi la matrice diventa antidiagonale. Il compito di richiamo (a b … a → b) richiede due strati, poiché il token che segue la copia precedente della query deve prima essere trovato e poi letto.

The sequence read by the model (top: the prompt, then its own earlier answers, fainter) and its prediction at each position of the answer (bottom, gold when wrong). Hover a prediction: the lines show how much it attends to each token in the selected head.La sequenza letta dal modello (in alto: il prompt, poi le sue risposte precedenti, più tenui) e la sua previsione in ogni posizione della risposta (in basso, in oro quando è sbagliata). Passa sopra una previsione: le linee mostrano quanta attention essa rivolge a ciascun token nella testa selezionata.
prompt

Inside the selected head for the selected query: its \(q\), then for every key it may see \(k_j\), \(v_j\), the score \(q\cdot k_j/\sqrt{d_k}\) and its weight after the softmax; the last row is the output \(\sum_j a_j v_j\).L'interno della testa selezionata per la query selezionata: il suo \(q\), poi per ogni chiave che può vedere \(k_j\), \(v_j\), il punteggio \(q\cdot k_j/\sqrt{d_k}\) e il suo peso dopo la softmax; l'ultima riga è l'uscita \(\sum_j a_j v_j\).

\(\class{fo}{\class{fa}{\mathrm{softmax}\Big(\class{fs}{\frac{\class{fq}{Q}\,\class{fk}{K^\top}}{\sqrt{d_k}}}\Big)}\,\class{fv}{V}}\)

The attention matrix of the selected head, queries as rows and keys as columns, darker for larger weights; the cells hidden by the mask are blank (with the open prompt the prompt sees itself in both directions) and the selected query is framed. Below, every head of every layer: click one to select it.La matrice di attention della testa selezionata, con le query sulle righe e le chiavi sulle colonne, più scura per pesi maggiori; le celle nascoste dalla maschera sono vuote (con il prompt aperto il prompt vede sé stesso in entrambe le direzioni) e la query selezionata è incorniciata. Sotto, ogni testa di ogni strato: cliccane una per selezionarla.
Token accuracy (violet) and loss (gold, log scale) on 200 held-out examples, against the steps of Adam.Accuratezza sui token (viola) e loss (oro, scala logaritmica) su 200 esempi tenuti da parte, in funzione dei passi di Adam.