← Can a computer learn to read your numbers?
game.js
| 1 | // The handwriting game: five short stages, played one step at a time with the lesson kit. |
| 2 | // Scores come from results.csv. Digit pictures are cut from the run's own examples.png and mistakes.png. |
| 3 | // The answers below are copied from the labels printed above those pictures, and the mix-up counts from the post's table. |
| 4 | const scores = VIBE.data['results.csv']; |
| 5 | const EXAMPLES = [5, 0, 4, 1, 9, 2, 1, 3, 1, 4, 3, 5, 3, 6, 1, 7]; |
| 6 | const MISTAKES = [[9, 8], [4, 2], [5, 3], [4, 8], [8, 2], [1, 8], [5, 3], [7, 2], [0, 6], [5, 8], [4, 9], [9, 8], [7, 9], [9, 7], [8, 9], [6, 5]]; |
| 7 | |
| 8 | // Cut one digit out of a sheet of 8 columns and 2 rows. |
| 9 | const SHEETS = { examples: { file: 'examples.png', rows: [115, 286] }, mistakes: { file: 'mistakes.png', rows: [135, 335] } }; |
| 10 | const CROP = 116; |
| 11 | function digit(sheetName, index, size = 150) { |
| 12 | const sheet = SHEETS[sheetName], column = index % 8, row = index < 8 ? 0 : 1; |
| 13 | const canvas = h('canvas', { width: CROP, height: CROP, role: 'img', 'aria-label': 'A handwritten digit', style: `width:${size}px;height:${size}px;border-radius:8px;background:#fff;display:block;margin:8px auto` }); |
| 14 | const image = new Image(); |
| 15 | image.onload = () => canvas.getContext('2d').drawImage(image, 82 + 141 * column - CROP / 2, sheet.rows[row] - CROP / 2, CROP, CROP, 0, 0, CROP, CROP); |
| 16 | image.src = VIBE.data[sheet.file]; |
| 17 | return canvas; |
| 18 | } |
| 19 | |
| 20 | const atRound = (round) => scores.filter((r) => r.epoch === round).map((r) => r.test_accuracy * 100); |
| 21 | const average = (list) => list.reduce((a, b) => a + b, 0) / list.length; |
| 22 | const FINAL = average(atRound(5)); |
| 23 | const WRONG = Math.round((10000 * (100 - FINAL)) / 100); |
| 24 | const numberOptions = (real, said) => [real, said].sort((a, b) => a - b).map(String); |
| 25 | const detective = (index) => { |
| 26 | const [real, said] = MISTAKES[index], options = numberOptions(real, said); |
| 27 | return { ask: 'What digit is this?', show: () => digit('mistakes', index), options, right: options.indexOf(String(real)), why: `The label says ${real}. The computer said ${said}.` }; |
| 28 | }; |
| 29 | const guessExample = (index, others) => { |
| 30 | const options = [EXAMPLES[index], ...others].sort((a, b) => a - b).map(String); |
| 31 | return { ask: 'What digit is this?', show: () => digit('examples', index), options, right: options.indexOf(String(EXAMPLES[index])), why: 'The **label** on this picture tells us the right answer.' }; |
| 32 | }; |
| 33 | |
| 34 | lesson({ |
| 35 | stages: [ |
| 36 | { title: 'You be the computer', icon: '🔢', steps: [ |
| 37 | { say: 'People write numbers in lots of different ways. Can a computer learn to read them? First, **you** try.' }, |
| 38 | guessExample(1, [6, 8]), |
| 39 | guessExample(4, [4, 7]), |
| 40 | guessExample(8, [4, 7]), |
| 41 | { say: 'You did that without thinking! A **digit** is one written sign for a number. Each of our pictures has a **label** that gives the right answer.' }, |
| 42 | ] }, |
| 43 | { title: 'Pictures are made of squares', icon: '🟦', steps: [ |
| 44 | { say: 'To a computer, a picture is a grid of tiny squares called **pixels**. Tap squares to draw a 7.', play(box, done) { |
| 45 | const SIZE = 8; let lit = 0; |
| 46 | const note = h('p', { class: 'say', role: 'status' }, 'Tap at least 6 squares.'); |
| 47 | const grid = h('div', { style: `display:grid;grid-template-columns:repeat(${SIZE},1fr);gap:2px;max-width:280px;margin:8px auto` }, Array.from({ length: SIZE * SIZE }, () => { |
| 48 | const cell = h('button', { 'aria-label': 'Pixel', 'aria-pressed': 'false', style: 'aspect-ratio:1;min-height:0;padding:0;border-radius:3px', onclick: () => { |
| 49 | const on = cell.getAttribute('aria-pressed') !== 'true'; |
| 50 | cell.setAttribute('aria-pressed', String(on)); cell.style.background = on ? 'var(--fg)' : ''; |
| 51 | lit += on ? 1 : -1; |
| 52 | note.textContent = `You lit ${lit} of ${SIZE * SIZE} pixels. The computer only sees which squares are dark.`; |
| 53 | if (lit >= 6) done(); |
| 54 | } }); |
| 55 | return cell; |
| 56 | })); |
| 57 | box.append(grid, note); |
| 58 | } }, |
| 59 | { ask: 'What is a pixel?', options: ['One tiny square in a picture', 'A kind of computer', 'A written number'], right: 0, why: 'Lots of pixels together make the picture.' }, |
| 60 | { say: 'Our pictures come from a real collection called **MNIST**. Each one is just 28 squares wide and 28 tall.', show: () => digit('examples', 15) }, |
| 61 | ] }, |
| 62 | { title: 'Practice and the test', icon: '📚', steps: [ |
| 63 | { say: `A **neural network** is a program with lots of adjustable settings, like knobs. It guesses, checks the label, and turns its knobs a little. That is **training**, or practice.` }, |
| 64 | { say: 'We gave it **60,000** pictures to practise on, and kept **10,000** more hidden for a test.' }, |
| 65 | { ask: 'Why keep the test pictures hidden during practice?', options: ['To see if it can read new pictures', 'To save computer memory', 'Because they are harder'], right: 0, why: 'It is like practising spelling with one list, then being tested on a different list.' }, |
| 66 | { ask: 'The knobs start at random settings, so we ran 3 separate tries. Why?', options: ['Different starts might give different results', 'The first try always fails'], right: 0, why: 'Comparing tries shows how much chance matters.' }, |
| 67 | ] }, |
| 68 | { title: 'How well did it do?', icon: '📈', steps: [ |
| 69 | { ask: 'Your guess: after 5 practice rounds, how many pictures out of 100 does it read correctly?', options: ['about 60', 'about 80', 'about 99'], predict: 'guess' }, |
| 70 | { say: 'Move the slider through the 5 practice rounds. These are the real scores from all 3 tries.', play(box, done) { |
| 71 | const label = h('p', { class: 'ask' }), list = h('div'); |
| 72 | const draw = (round) => { |
| 73 | const now = atRound(round); |
| 74 | label.textContent = `After round ${round}: ${average(now).toFixed(2)}% correct on average`; |
| 75 | list.replaceChildren(...now.map((score, i) => h('p', { class: 'say' }, `Try ${i + 1}: ${score.toFixed(2)}%`))); |
| 76 | if (round === 5) done(); |
| 77 | }; |
| 78 | box.append(label, slider(1, 5, 1, draw, 'Practice round'), list); |
| 79 | draw(1); |
| 80 | } }, |
| 81 | { ask: `It got ${FINAL.toFixed(2)}% right. About how many of the 10,000 test pictures were wrong?`, options: ['About 12', `About ${WRONG}`, 'About 1,000'], right: 1, why: `${(100 - FINAL).toFixed(2)}% of 10,000 is about ${WRONG} pictures.` }, |
| 82 | { ask: 'Two of the three tries got slightly worse in the last round. So does more practice always help?', options: ['Yes, always', 'Not every time'], right: 1, why: 'Small ups and downs happen. We can’t tell why from these results alone.' }, |
| 83 | ] }, |
| 84 | { title: 'Mistake detective', icon: '🔍', steps: [ |
| 85 | { say: 'Now the hard ones. The computer got **all** of these next pictures wrong. Can you beat it?' }, |
| 86 | detective(2), detective(7), detective(9), detective(12), detective(15), |
| 87 | { ask: 'What was its most common mix-up across all 3 tries?', options: ['A real 1 read as 7', 'A real 5 read as 3', 'A real 0 read as 8'], right: 1, why: 'A real 5 was read as 3 a total of 23 times.' }, |
| 88 | { ask: 'Does our test show it can read a whole page of writing?', options: ['Yes', 'No, only single digits'], right: 1, why: 'The pictures were single digits, centred and all one size. Reading a digit isn’t the same as understanding a number, either.' }, |
| 89 | ] }, |
| 90 | ], |
| 91 | finish: () => h('div', {}, h('p', { class: 'ask' }, '🎉 You finished all the stages!'), h('p', {}, `After practice, the computer read ${FINAL.toFixed(2)}% of new digits correctly, about ${WRONG} mistakes in 10,000. It learned from examples, but it still mixes up some shapes.`)), |
| 92 | }); |