Taking Apart a Digit Reader
Write a digit in the box below. Try a
Before we explain anything, spend a minute trying to confuse it! You can also
choose one of the three examples above the box. All three were written as
The first example seems reassuring: the reader assigns
The middle example is interesting for a different reason. The reader chooses
We are going to build this reader ourselves. There will be some programming involved, but the ideas that make it work come from the mathematics we are learning in this course: coordinates, dot products, functions of many variables, and eventually derivatives. To find out where they enter, let's start with the working machine and take it apart.
28.1What Goes into the Function?
From the outside, the reader takes a picture and produces probabilities. This
sounds like a function! For example, we could ask just for the probability
it assigns to
The output is a real number. But what is the input? We know how to give a function numbers; how do we give it handwriting?
Look closely at one of the supplied images. It is a square array of tiny
pixels, with
Our reader uses a smaller list. Divide the picture into blocks, each containing
There are
Here
This measurement forgets something. Suppose two ink pixels move to different
positions within the same block. The original picture changes, but the count
is still
For a freehand drawing there is one earlier step: the pad centers and resizes
the strokes and adjusts their thickness to make a bitmap like the supplied
examples. Once that bitmap is made, the block counts are calculated in exactly
the same way. From here on, our mathematical input is the resulting list of
28.2One Image Is One Point
How many dimensions does it take to describe this input?
It is tempting to say two: the image has a horizontal direction and a vertical direction. But those two directions locate a pixel within an image. We want to describe the entire image. This is the same distinction we encountered when a whole can became a point in the space of possible cans: the coordinates describe the object we are choosing.
Let's make a very small picture with just two pixels. We can specify it by giving the amount of ink in its first pixel and the amount in its second:
The picture with ink amounts
Now add a third pixel. We need a third coordinate, and the square becomes a cube. Add a fourth pixel, and we need a fourth coordinate. Nothing about the description stops working when the number of pixels exceeds three; we simply run out of room to draw its coordinate axes on the page!
For our actual images, read the top row from left to right to obtain
The possible brightness lists fill the
Notice that writing the array as a list discards no additional information.
If I give you the list and tell you the row order, you can put every entry
back where it belongs. The loss of information happened when we replaced each
We can now state the input to our function using familiar mathematical
language. The reader's probability of three is a function of
It is a function of many variables because changing any one of these pixel measurements can affect the answer. This is a rather concrete reason to care about functions on spaces we cannot draw.
28.3One Input, Ten Scores
Let's follow our point a little farther into the machine.
The reader first calculates ten numbers, one for each possible digit. These are called scores, and we will write them as
For now, leave the calculation of each score inside its box. What matters is
that the same image goes into all ten calculations. The score for
Scores are ordinary real numbers. They can be positive or negative, and they need not add to anything in particular. So they cannot yet be the probabilities we saw in the opening display. We need one more operation.
First take the exponential of each score. This makes all ten numbers positive, and preserves their order: a larger score gives a larger exponential. Then divide each exponential by their total:
Each fraction is positive, and the ten fractions add to
To see the arithmetic without a long list of decimals, imagine an image with
score
Each of the other digits receives
Three is the clear winner among the ten answers, but the other nine
possibilities together receive more probability than three does. That
explains how our hesitant three can win with only
There is another useful experiment in the figure. Add the same number
The common factor cancels. Thus a score's absolute size cannot by itself tell us a probability. The scores must be compared with one another. In particular, the probability assigned to three can change even if its own score stays fixed: changing the other scores changes the shared denominator.
We have now taken apart the reader into two stages:
The complete probability output is the list
28.4What Is Stored Inside?
Let's open the calculation for three. Inside, we find a long list of numbers. Here is how the beginning of that list might look in Python, with the actual stored numbers rounded to six decimal places:
weights = [0.000000, 0.331608, 0.286236, 0.073579, ...]
bias = 0.013382
The dots stand for the rest of the list: there are
What happens next is remarkably simple. Multiply each image entry by the
weight underneath it, add all
In Python, the whole calculation can be written as
score = sum(xi * wi for xi, wi in zip(image, weights)) + bias
Here zip pairs the first image entry with the first weight, the second with
the second, and so on. The expression xi * wi multiplies one pair, and sum
adds the resulting products. There is a loop hiding in that line, but the
arithmetic is just multiplication and addition.
Do you recognize the sum? It is a dot product! If we collect the weights
for three into the vector
where
It is worth watching these operations happen to the actual image. Put its
Multiplication rescales each weight's bar by the corresponding image value.
If the image value is
Adding the bars means adding their signed heights. Bars above zero increase
the total; bars below zero decrease it. For example, an image entry of
The height plots are a way to inspect the entries of our two lists. Their horizontal grid records which pixel an entry belongs to; their vertical height records its value. We have rearranged the numbers to make the arithmetic visible, just as we earlier rearranged the image's coordinates into a list.
Now do the same thing for each digit. Zero has a stored vector
Ten dot products and ten biases give our ten scores. Those scores go through the exponential normalization we already investigated, and out come the ten probabilities. The stored part of the whole reader consists of
numbers.
There are two different kinds of numbers in play. When you draw a new image, its coordinates change, so the products, sums, and probabilities change. The stored vectors and biases stay fixed. Changing those stored numbers would change the reader itself: it would change how the machine responds to images. Later, we will choose those numbers ourselves using labeled examples.
But first there is a more immediate question. We now know what the reader computes. Why should a dot product with a particular vector tell us anything about whether an image looks like a three? How can a sum of pixel values, scaled by some stored numbers, distinguish one kind of handwriting from another?
That is the question for the next chapter. We can carry out the arithmetic; now we need to understand its geometry.
Exercise 28.1 (Follow the numbers). Suppose two different
Exercise 28.2 (Change an output without changing its score). An image has score
Exercise 28.3 (Which numbers change?). Explain the difference between replacing an input image and replacing the reader's stored weights. In each case, which numbers stay fixed and which calculations must be performed again?
The supplied handwriting comes from E. Alpaydin and C. Kaynak's Optical Recognition of Handwritten Digits dataset, distributed through the UCI Machine Learning Repository under CC BY 4.0. The examples used here come from its held-out test set; they were not used to fit the reader.