Multi-Layer Perceptron (MLP) Neural Network Numeral Image Recognition Demo
Draw a digit. The network beside the grid is the one that classifies it.
Drawing
Recognized
?
0.95
No pixel is on, so every weight drops out. Only the biases remain.
Network
Weights and biases
Inputs are row-major: four pixels per row, top row at the left. An empty grid shows each weight and bias. Once a pixel is on, color shows activation instead: edges are input times weight, hidden nodes are their tanh output, and output nodes are their output before softmax. Zero stays white. ±1 is half saturated, and the lowest and highest values are full red and full green.
Hidden Unit (Neuron) Detail
- 1.0 × −1.5−1.50
- 0.5 × 00.00
- 0.8 × 21.60
- sum0.10
- + bias 0.50.60
- tanh0.537
Every arrow carries that same output. At the next layer, each edge multiplies it by a different weight. Those weights are not drawn here.
How this works
The grid is a 20-dimensional vector, read left to right, top to bottom. This can be thought of like a list of 20 input numbers, though in this simple case the inputs are always 0 or 1. An empty cell is 0 and a black cell is 1.
This simple neural network has three layers which are each relatively small.
The first layer is just the twenty inputs. (Its neurons don’t have their own separate bias values, but you could think of the inputs themselves as being like biases that change.) The connections from this layer to the next one all have fixed weights that were learned in training the network.
The second layer is called the hidden layer. It contains eight neurons called hidden units. A smaller number is used here to keep the net smaller but a larger number of units would actually work better.
The third layer is the output layer which, in this case, has eleven neurons called output units — one for each of the digits 0–9 and ?.
Each hairline connection is one weight. There are 248 of them in total. Weights are multiplied by their inputs. They can be negative or positive, with an absolute value smaller than 1 or larger than 1. So they can either magnify or diminish their inputs, flip positive inputs into “inhibitory” negative ones and vice versa, or completely cancel out their inputs when 0. (A weight of 0 can be considered “not connected”.)
For color coding: gray is zero. The color coded scale reaches pure red at the most negative value and pure green at the most positive. The maximum and minimum values among the weights and biases were learned in training.
After multiplying all the inputs by their weights, they are all summed together.
Each neuron except for the input units has a bias, which is kind of like a weight except that it is added to the sum of all the inputs. (The bias is shown in the diagram using the same color coding as the weights in the fill color of the neuron.)
Finally, the value is passed through a transfer function that acts sort of like a cap on the minimum and maximum output value of the neuron. In this case the transfer function is tanh(). So the final output of the neuron (unit) is tanh(sum of inputs × weights + bias). This function has the effect of “squishing” numbers greater than 0.5 or less than −0.5, which makes it harder to get a result close to −1 or 1.
The output vector is run through the softmax() function, so the eleven output numbers under the output units are scaled such that they’re non-negative and all sum to 1. This converts them into normalized probabilities between 0 and 1 where the sum of all the probabilities can’t be higher than 100%.
The large “recognized” numeral is the class (output neuron) with the highest probability given the numeral you drew in the grid, and thus the input vector to the neural net.
Clear the grid, and every weight is multiplied by 0, each hidden unit equals tanh() of its bias, and the softmax is normalizing biases alone. That empty (all 0’s) input vector was trained to come out as ? with a probability near 1.
Training data
The cells show the hand-built 4×5 glyphs, including shifts that still fit, and the designed unknowns under ?: a blank grid, a solid fill, bars, diagonals, and checkers. One-pixel edits and distant random patterns were trained too, and are not drawn here.
Each pixel is on or off, so these 20 bits make 2^20 = 1,048,576 possible grids. The training set is only a few of them. A one-pixel edit was kept with its digit only when it stayed closer to that digit than to any other. Ties went to ?. So did specks and random patterns far from every glyph. A grid that was never shown can still be recognized: if it is close to a learned glyph, it drives the same hidden units, and the softmax can name that digit. That is generalization, and it is not perfect. A careful digit, including a one-pixel slip, should land correctly. Other unseen grids will be claimed by the wrong digit.
Eight hidden units memorize a small neighborhood. They do not read handwriting in general. If a much larger number of hidden units and a larger more detailed grid were used with more input units, then this design could recognize handwriting reasonably well with sufficient training.