← Back to the track

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.

pos.shuffleSession 8.4About 25 minutesYour check: predict results on new examples

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 lesson

Six symbols in six chairs, and no chair is marked.

Without seat numbers, a shuffle changes nothing. Position is added, not read.

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?

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 browser

This 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.

Two switches

Both start off in this session. The rest of the track runs with the mask on, which is why that is said here rather than assumed.

The six rows before the shuffle

Output rows before the shuffle
row12345678
10.098149-0.091440-0.1245090.027464-0.0078350.0122310.0413300.023440
20.092257-0.134583-0.1740320.054962-0.009041-0.0149700.0760800.051377
30.088738-0.114211-0.1331640.038596-0.0094730.0047200.0442900.035295
40.101888-0.105841-0.0917650.0248440.0196780.0341340.058526-0.028198
50.077906-0.095045-0.1142610.037024-0.0349290.0022920.0126060.050732
60.091838-0.065217-0.0854520.017688-0.0228350.0232640.0089530.014969

The six rows after the shuffle

Output rows after the shuffle
row12345678
10.098149-0.091440-0.1245090.027464-0.0078350.0122310.0413300.023440
20.092257-0.134583-0.1740320.054962-0.009041-0.0149700.0760800.051377
30.088738-0.114211-0.1331640.038596-0.0094730.0047200.0442900.035295
40.101888-0.105841-0.0917650.0248440.0196780.0341340.058526-0.028198
50.077906-0.095045-0.1142610.037024-0.0349290.0022920.0126060.050732
60.091838-0.065217-0.0854520.017688-0.0228350.0232640.0089530.014969

Same or different

computed in this browserthis bench, at its current switches
The question is only whether anything moved.

The 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 yet

Three 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.