DEV Community

How does a computer know this is a 3?

A neural network, explained with one handwritten digit and a few simple numbers.

You look at a scribbled 3 and know it in a fraction of a second. A computer has no eyes and no idea what a "3" is. It only has numbers. So how does it get from a grid of pixels to the right answer?

The answer is a neural network. Its core idea is simpler than it sounds, and you can follow it with nothing more than multiplication and addition.

To a computer, an image is just numbers

A small grayscale image of a digit is a grid of 28 by 28 pixels. That's 784 pixels, and each one is stored as a number: 0 for white, 1 for full ink, and values in between for the gray edges of the stroke.

So the computer never sees a "3". It sees a list of 784 numbers.

The digit becomes a 28 × 28 grid of pixel values between 0 and 1
The digit becomes a 28 × 28 grid. Zoom in and every pixel is a number between 0 (white) and 1 (ink).

One neuron: multiply, add, and add a bias

The basic unit of a neural network is the neuron. A neuron does one simple job:

  1. It takes all of the input numbers.
  2. It multiplies each one by its own weight.
  3. It adds up the results.
  4. It adds one more number, called the bias.

Written as math: z = w₁x₁ + w₂x₂ + … + b. A positive weight means "this pixel counts in favor." A negative weight means "this pixel counts against." The weights and the bias are the parts the network learns.

A worked example

Let's try it with just two pixels.

  • This pixel is 1 (full ink), and its weight is 0.9. That gives 1 × 0.9 = 0.9.
  • That pixel is 0.5 (gray), and its weight is −0.5. That gives 0.5 × (−0.5) = −0.25.

Multiply and add, and we get 0.9 − 0.25 = 0.65. Then add a bias of −0.2. The total is 0.45.

Two pixels, two weights, and a bias give a total of 0.45
The same sum with real numbers. Two pixels, two weights, and a bias give a total of 0.45.

ReLU: the neuron's on and off switch

Next, the total goes through an activation function. The most common one is ReLU, short for "rectified linear unit." Its rule fits in one line:

  • If the total is positive, like our 0.45, it passes straight through.
  • If the total is negative, ReLU outputs zero.

So each neuron is either quiet (zero) or active (some positive value). That small bend is what lets a network learn more than straight lines.

ReLU passes the positive total 0.45 straight through, so the neuron fires
ReLU passes a positive total, like our 0.45, straight through. The neuron fires (blue).

Stack neurons into layers

One neuron is not very smart. The power comes from many neurons, arranged in layers.

The first layer looks at the raw pixels. Early layers can pick up simple patterns, like edges. Later layers combine those edges into bigger shapes, like loops and strokes.

Even a small digit network has a lot of knobs. One with 784 inputs, two hidden layers of 16 neurons, and 10 outputs has about 13,000 weights to tune.

Ten outputs, one answer

The last layer has ten neurons, one for each digit from 0 to 9. The numbers flow forward through the network, layer by layer, and each output ends up with a score. The brightest output is the answer. Here, it's the 3.

Numbers flow forward into ten outputs, one per digit, and the brightest is the 3
Numbers flow forward into ten outputs, one per digit. The brightest output, here the 3, is the network's answer.

Quick check

An inked pixel meets a negative weight. Does the neuron become more active, or less active?

Answer: less active. The pixel's value is positive and its weight is negative, so their product pulls the total down. After ReLU, the neuron fires less.

Key takeaways

  • To a computer, an image is a grid of numbers.
  • A neuron multiplies each input by a weight, adds them up, and adds a bias.
  • ReLU keeps positive totals and turns negative ones into zero.
  • Layers pass numbers forward until ten outputs vote. The highest one wins.
  • The weights aren't written by hand. They're learned from examples, which is what training does.

This article is part of Deep Learning, Visually by Logit Solutions AI: short animated explainers that cover one idea at a time, each with a written version like this one.

Top comments (0)