Learn machine learning · part 8 · session 4
Positional information
Every position in an attention layer is computed from the same weighted average over the same set of positions. Unless something says where each token sits, the tokens are a bag.
After this session you will be able to show that a transformer sees a set until position is added, and name the two ways it is added.
The gate is three calls on three switch settings, and two of the three have to hold.
01 · The question
A transformer does not run along the sequence from left to right. It computes every position from the same weighted average over the same set of positions, all at once. So unless something tells the layer where each token sits, the tokens are a bag. A token here is one symbol from the input.
The signal that says where a token sits is called a , and this layer adds it as one extra row per position.
Numbered seats
illustrativesix symbols, drawn for the lessonSix symbols in six chairs, and no chair is marked.
Below is one attention layer with a switch for the positional rows and a button that shuffles the input. With the switch off, shuffle the tokens and the pooled output does not move at all. Turn it on and the same shuffle changes it. Twenty five minutes. Order is not read, it is added.
02 · Predict
Before anything runs
Positional rows off, no mask. Shuffle the six tokens. What happens to the pooled output vector?
Choose something first.
03 · Try it
The bench
Six tokens, one attention layer of 336 parameters that has never been trained, and two switches. The pooled output is the average of the six rows, column by column, one summary of the whole sequence, which is what the gate reads: the six rows themselves permute with the tokens.
One attention layer, six tokens
illustrative336 parameters, seed 7computed in this browserThis layer has fixed random weights and has never been trained, so the output numbers mean nothing on their own. What matters is whether they change.
The input
- position 1symbol 1
- position 2symbol 4
- position 3symbol 7
- position 4symbol 2
- position 5symbol 9
- position 6symbol 5
Permutation 1, 2, 3, 4, 5, 6, from a seeded generator at seed 7, so the same shuffle can be repeated.
The six rows before the shuffle
| row | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| 1 | 0.098149 | -0.091440 | -0.124509 | 0.027464 | -0.007835 | 0.012231 | 0.041330 | 0.023440 |
| 2 | 0.092257 | -0.134583 | -0.174032 | 0.054962 | -0.009041 | -0.014970 | 0.076080 | 0.051377 |
| 3 | 0.088738 | -0.114211 | -0.133164 | 0.038596 | -0.009473 | 0.004720 | 0.044290 | 0.035295 |
| 4 | 0.101888 | -0.105841 | -0.091765 | 0.024844 | 0.019678 | 0.034134 | 0.058526 | -0.028198 |
| 5 | 0.077906 | -0.095045 | -0.114261 | 0.037024 | -0.034929 | 0.002292 | 0.012606 | 0.050732 |
| 6 | 0.091838 | -0.065217 | -0.085452 | 0.017688 | -0.022835 | 0.023264 | 0.008953 | 0.014969 |
The six rows after the shuffle
| row | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| 1 | 0.098149 | -0.091440 | -0.124509 | 0.027464 | -0.007835 | 0.012231 | 0.041330 | 0.023440 |
| 2 | 0.092257 | -0.134583 | -0.174032 | 0.054962 | -0.009041 | -0.014970 | 0.076080 | 0.051377 |
| 3 | 0.088738 | -0.114211 | -0.133164 | 0.038596 | -0.009473 | 0.004720 | 0.044290 | 0.035295 |
| 4 | 0.101888 | -0.105841 | -0.091765 | 0.024844 | 0.019678 | 0.034134 | 0.058526 | -0.028198 |
| 5 | 0.077906 | -0.095045 | -0.114261 | 0.037024 | -0.034929 | 0.002292 | 0.012606 | 0.050732 |
| 6 | 0.091838 | -0.065217 | -0.085452 | 0.017688 | -0.022835 | 0.023264 | 0.008953 | 0.014969 |
Same or different
computed in this browserthis bench, at its current switchesThe pooled output, which is the average of the six rows, column by column
Before: [0.091796087559, -0.101056318868, -0.120530615043, 0.033429441393, -0.010739228640, 0.010278374990, 0.040297544027, 0.024602594718]
After: [0.091796087559, -0.101056318868, -0.120530615043, 0.033429441393, -0.010739228640, 0.010278374990, 0.040297544027, 0.024602594718]
Shuffle the tokens to compare the two.
The tokens are in their original order.
Set the bench to a variant
Each button puts the two switches where the gate's variant puts them, so you can run any of the three before or after you call it.
One of the three catches most people. The causal mask is itself positional: row i is allowed columns 0 to i and nothing later, so the layer already treats position 1 and position 6 differently even with the positional rows removed.
Ask for a hint
Rungs open as you run the session, not as time passes. You have run it 0 times.
1. A question: available after 3 evaluated runs.
2. A narrowing: available after 5 evaluated runs.
3. A worked fragment, with two tokens: available after 7 evaluated runs.
4. The answer: available after 9 evaluated runs.
04 · Compare
05 · Check your understanding
Your check: predict results on new examples
not yetThree switch settings. Say for each whether shuffling the six tokens leaves the pooled output where it was. Two of the three passes, and every variant can be re-run on the bench afterwards.
Variant 1 of 3
Variant A. Positional rows off, mask off, then shuffle the six tokens.
Call it before you run it.
Variant 2 of 3
Variant B. Positional rows on, mask off, then shuffle the six tokens.
Call it before you run it.
Variant 3 of 3
Variant C. Positional rows off, mask on, then shuffle the six tokens.
Call it before you run it.
Called 0 of 3 variants so far.
Save your takeaway
Your takeaway
Edit it into your own words, or keep the line as it is.