Now a real digit. Point at or tap a patch to find its token, or a token to find its patch. Change the patch size and watch the token count.
Cut a picture into patches: each patch becomes one token
Pick a patch size. Point at, tap or arrow-key through the patches to see which token each one becomes.
| picture | (28, 28) | 784 numbers |
| cut into patches | (16, 49) | 16 tokens of 49 numbers |
| @ W_in | (49, 96) → (?, ?) | each patch becomes a token vector |
| + position | (?, ?) | which patch is where |
| + step t and label | (96) added to every token | like D1–D2, but as a vector |
| Transformer blocks | (?, ?) → (?, ?) | attention scores per head: ? (answer the question below) |
| @ W_out | (96, 49) → (16, 49) | a noise guess for every patch |
| put patches back | (28, 28) | the noise guess for the picture |
Write the cutting yourself, in two steps. First, reshape can split each axis of the picture into
(which block, which pixel inside the block). For the 4 × 4 picture and patch size 2:
x4 = img.reshape(2, 2, 2, 2) # axes: (block row, row in block, block column, column in block)
x4[0, :, 1, :] # block row 0, block column 1 → [[2, 3], [6, 7]], the top-right patchWrite the sizes for any picture and patch size:
Enter keeps the indent · Tab indents · Esc then Tab leaves the editor · ⌘/Ctrl + Enter runs
Now one patch is x4[i, :, j, :], but the two block axes, i and j, sit in positions 0 and 2.
To list patches in order they must come first. transpose reorders axes: a.transpose(1, 0) swaps the two axes of a table,
and x4.transpose(0, 2, 1, 3) gives the new order axis 0, axis 2, axis 1, axis 3, so the shape becomes
(block row, block column, row in block, column in block). After that, a reshape to (number of patches, p · p) flattens each patch.
Enter keeps the indent · Tab indents · Esc then Tab leaves the editor · ⌘/Ctrl + Enter runs