Prediction
Confidence
Runner-up
Strokes
Drawing
Playback
View

More information about the digit recognizer

Draw a digit, watch the network read it

Handwriting recognition was one of the first jobs neural networks did for real, reading postcodes and cheques. Here you can watch one at work: draw a digit on the pad and it is scaled into a 28 × 28 grid exactly as the famous MNIST digits were, then a small network of 64 and 32 units weighs the ink and gives the probability of every digit. The first layer's units appear as the patterns they learned, lighting up as they fire, so you can see which strokes push the answer towards a 4 or a 9. Draw a digit from 0 to 9 on the pad with the mouse or a finger, and a small neural network reads it. Your drawing is prepared the way the MNIST digits were: the dashed box round the ink is scaled to fit 20 × 20 pixels and the result is moved so that its centre of mass sits in the middle of a 28 × 28 grid. Those 784 grey values go into the network. Each of the 64 units of the first hidden layer is drawn as the picture of its weights, orange where ink makes it more active and blue where ink holds it back, and it lights up as much as it fires; the 32 units of the second layer combine them, the lines show the connections that carry the most, and the ten bars give the probability of each digit. Pick an example, including the hard ones (a closed 4 that could be a 9, a 1 with a long flag that could be a 7), press Randomize for an example in another hand, or draw over the pad: a new stroke on an example starts your own drawing, Undo removes the last stroke and Clear empties the pad. Play replays the strokes and lights the network up layer by layer; Step and Back stop at each stroke and layer. Save the picture, record a video with a pencil scratch and a chime on the answer, or open it in the photo editor with the pencil, where the drawing, the grid, the activations, the bars and the labels are layers of their own.

Як це працює

  1. Draw a digit on the pad with the mouse, a pen or a finger; the answer follows the pen, and a new stroke on an example starts a drawing of your own. Undo removes the last stroke, Clear empties the pad.
  2. Watch the input: the dashed box round your ink is scaled into the 20 × 20 box of the 28 × 28 grid and centred by the centre of mass, as MNIST was made.
  3. Read the network: the 64 tiles of the first hidden layer are the patterns their weights learned, brighter when they fire; the 32 dots of the second layer combine them, and the ten bars give each digit's probability.
  4. Pick an example, including the tricky 4 or 9 and 1 or 7, or press Randomize for an example in another hand; Play replays the strokes and lights the network up layer by layer.

From a drawing to 784 numbers

A network needs its input in a fixed form. The MNIST digits, collected by the US National Institute of Standards and Technology and arranged by Yann LeCun, Corinna Cortes and Christopher Burges in 1998, were made like this: each handwritten digit was scaled so that its ink fits a 20 × 20 pixel box without changing its shape, smoothed so its edges become shades of grey, and placed in a 28 × 28 image with its centre of mass in the middle. The app does the same to your strokes, so a digit drawn small in a corner or large across the pad gives the network the same picture: 784 grey values between 0 for paper and 1 for ink.

Two hidden layers and a softmax

Each of the 64 units of the first hidden layer multiplies the 784 inputs by its own weights, adds them up with a bias and passes the sum through a ReLU, which keeps positive values and turns negative ones into zero. Its weights form a 28 × 28 picture: orange where ink makes the unit fire, blue where ink holds it back, so a unit acts as a detector for a stroke in a certain place. The 32 units of the second layer do the same with the 64 activations, and the ten outputs turn their sums into probabilities with the softmax, eᶻ divided by the sum of all eᶻ, which are positive and add up to 1.

How it learned

The network started with random weights and was trained on the 60,000 training digits of MNIST, plus copies turned, slanted, stretched and drawn thicker or thinner and digits typed in 84 typefaces, so it copes with handwriting that is not like the samples. For each small batch, backpropagation computed how the cross-entropy loss, minus the log of the probability given to the right digit, changes with every weight, and the Adam optimiser moved all 52,650 weights and biases a little downhill. After 14 passes it reads 98.1 % of the 10,000 test digits it had never seen. The weights are stored as 8-bit integers, about 52 KB, and the network runs in your browser.

Why some digits are hard

The network only knows the 784 grey values, not how the digit was written. A 4 whose top is closed has much the same ink as a 9, a 1 with a long flag looks like a 7 without its bar, and a sloppy 3 is close to an 8, so the probabilities split between the two and the runner-up tile shows by how much. Strokes far from anything in the training data, letters or scribbles, still get a confident answer for some digit, a reminder that a classifier always picks one of the classes it knows. Bigger networks, convolutional layers that look for the same stroke anywhere, and more varied data push the error on MNIST below 0.3 %.

Поширені запитання

How does the digit recognizer work?

Your strokes are scaled so the ink fits a 20 × 20 pixel box, drawn with soft edges and moved so their centre of mass is the middle of a 28 × 28 grid, the format of the MNIST digits. The 784 grey values go through a neural network with two hidden layers of 64 and 32 ReLU units and a softmax output that gives the probability of each digit from 0 to 9. The digit with the highest probability is the prediction.

What is MNIST?

A collection of 70,000 handwritten digits, 60,000 for training and 10,000 for testing, made by Yann LeCun, Corinna Cortes and Christopher Burges in 1998 from forms filled in by American census employees and high-school students. Each digit is a 28 × 28 grey image, centred by its centre of mass. It became the standard first test of machine learning methods; good networks today get fewer than 3 in 1,000 test digits wrong.

Why does it get my digit wrong?

The network learned from the way the MNIST writers wrote, and it sees only the grey pixels. Digits that share most of their ink are close for it: a 4 with a closed top and a 9, a 1 with a flag and a 7, a 3 and an 8. Very thin or very long strokes, a digit written in several separate pieces, or a style rare in the training data lower its confidence. Look at the runner-up tile: when it is close, the drawing is ambiguous to the network too.

What do the tiles of the first hidden layer show?

Each tile is one unit's 784 weights drawn as a 28 × 28 picture: orange where ink in that pixel makes the unit more active, blue where it makes it less active. The brighter a tile, the more strongly that unit fires for your drawing. Many units look like parts of strokes in certain places, a loop at the top, a bar across the middle, an edge on the left, because those are useful clues to tell digits apart.

Is my drawing sent to a server?

No. The network's weights are part of the page, about 80 KB of text, and the scaling, the network and the drawing all run in your browser. Nothing you draw is uploaded or stored anywhere except, for your own convenience, the last drawing in your browser's local storage.

Can I use the picture?

Yes. Save gives a PNG at the size you choose, Save JSON writes your strokes, the 28 × 28 input and the ten probabilities, Record films the strokes being drawn and the network lighting up with a pencil scratch and a chime, and the pencil opens the picture in the PictorX photo editor with the background, the drawing, the downsampled grid, the activations, the probability bars and the labels on layers of their own. In the editor itself it is under Apps ▸ Science ▸ Computing ▸ Digit Recognizer, where it can also read a digit from the open image.

Digit Recognizer: як записати відео?

Натисніть червону кнопку «Запис» на панелі відтворення: вона знімає те, що показує сцена. Натисніть її ще раз, щоб зупинити, і відео збережеться на вашому пристрої. Запис безкоштовний, потрібно лише увійти. Без підписки на відео в куті буде невеликий логотип PictorX. Кнопка поруч із «Запис» відкриває параметри відео: формат (як показано, квадрат, сторіз 9:16 або широкий 16:9), тривалість (доки ви не зупините, або 5, 10, 15 чи 30 секунд для соцмереж) і файл: MP4, WebM або анімований GIF. Для звуку виберіть власні звуки застосунку або одну з трьох музичних доріжок: спокійну, яскраву чи ритмічну.

Готові спробувати?

Відкрийте PictorX у браузері — без встановлення і без кредитної картки.

Перейти до редактора