Learning path: Neural Networks from Scratch to Embeddings · Step 1 of 9
Why Neural Networks: Pixels, One Neuron and Layers
Why pictures need neural networks: a handwritten digit is 784 numbers, one neuron is a weighted sum plus a bias and one small rule, and neurons stack into layers. Worked out on MNIST with NumPy.
What you will learn
- Explain why pictures need a neural network instead of a column-by-column model
- Turn a 28 x 28 MNIST digit into 784 numbers with reshape
- Work out one neuron by hand: weighted sum, bias and the negative-becomes-zero rule
- Name the input, hidden and output layers of a digit network and what makes a network deep
Before you start
- Basic Python and NumPy
- Linear Regression and the Cost Function
A house table is easy for a model. Size, rooms, city: every column means one clear thing. Now look at a picture of a handwritten digit. Pixel number 407 is 253. On its own, that tells you nothing. And if the writer shifts the digit a little to the left, every pixel changes.
Pictures, sound and text keep their meaning in patterns, not in one column. A neural network is a model that learns those patterns. In this article you'll see a digit as numbers, work out one neuron by hand, and then line neurons up into layers.
Can you read these?
The running example for this whole topic is MNIST, a famous dataset of handwritten digits. There are 60,000 training pictures, each only 28 x 28 pixels.
import numpy as np
import matplotlib.pyplot as plt
data = np.load("mnist.npz")
X, y = data["x_train"], data["y_train"]
print(X.shape, y.shape)
print("first six labels:", y[:6])
(60000, 28, 28) (60000,)
first six labels: [5 0 4 1 9 2]
X holds the pictures and y the labels. Let's draw the first six:
fig, axes = plt.subplots(1, 6)
for i, ax in enumerate(axes):
ax.imshow(X[i], cmap="gray")
ax.set_title(y[i])
ax.axis("off")
plt.show()

Easy for you. Nobody had to write rules for what a 5 looks like, and that's exactly what we want the computer to learn too.
A picture is a grid of numbers
How does a computer see a picture? As numbers. 0 is black and 255 is the brightest white.
digit = X[0]
print("label:", y[0])
print("shape:", digit.shape)
print("darkest:", digit.min(), " brightest:", digit.max())
label: 5
shape: (28, 28)
darkest: 0 brightest: 255
Zoom into a small 6 x 6 piece of the 5:
print(digit[6:12, 8:14]) # a 6 x 6 piece of the picture
[[ 30 36 94 154 170 253]
[238 253 253 253 253 253]
[219 253 253 253 253 253]
[ 80 156 107 253 253 205]
[ 0 14 1 154 253 90]
[ 0 0 0 139 253 190]]
Where the pen went, the numbers are big. Where the paper is empty, they're 0.
A network takes one long row of numbers as input, so we lay the 28 rows end to end. reshape(-1) does that:
digit = X[0]
flat = digit.reshape(-1) # 28 rows laid end to end
print(digit.shape, "->", flat.shape)
print("pixel 407:", flat[407])
(28, 28) -> (784,)
pixel 407: 253
28 x 28 = 784. Every picture is simply 784 numbers.
Interns and a manager
How does a network read those 784 numbers? Imagine an office with four interns. Each intern looks at one small part of the picture and writes a note: "flat line on top", "line on the left", "round belly", "curve at the bottom". All the notes go up to the manager, who weighs them and says: "It's a 5."
No single intern knew the answer, but the team did. The interns are neurons, the notes are numbers, and the whole team is a neural network.
One neuron, step by step
So what does one neuron actually do? Three things:
- Multiply each input by its weight (how much that input matters) and add them up.
- Add one more number, the bias. The result is called
z. - Apply one small rule, the activation: if
zis negative, pass on 0; otherwise pass onz.
The same neuron in Python:
x = np.array([0.0, 0.5, 1.0])
w = np.array([0.4, -0.2, 0.9])
b = -0.3
z = np.sum(x * w) + b
out = max(0, z)
print("x * w :", x * w)
print("z :", round(z, 2))
print("output:", round(out, 2))
x * w : [ 0. -0.1 0.9]
z : 0.5
output: 0.5
The products are 0, -0.1 and 0.9. They add up to 0.8, and with the bias, z is 0.5. Since z isn't negative, the neuron passes on 0.5. Exactly what we worked out by hand.
A quiet neuron
Same weights, same bias, a different input:
x = np.array([1.0, 1.0, 0.0]) # a different input
z = np.sum(x * w) + b
print(round(z, 2), max(0, z))
-0.1 0
0.4 - 0.2 + 0 - 0.3 = -0.1. That's negative, so the neuron passes on 0. It stays quiet. This rule has a name, ReLU, and you'll meet it properly in The Forward Pass and Activation Functions.
Neurons work in layers
One neuron is small. The power comes from many of them, arranged in layers:
- Input layer: the 784 pixel numbers.
- Hidden layer: our interns. Here, 128 neurons, and every one looks at all 784 pixels with its own weights.
- Output layer: 10 neurons, one score for each digit from 0 to 9. The highest score wins.
inputs, hidden, outputs = 28 * 28, 128, 10
print("input layer :", inputs, "numbers")
print("hidden layer:", hidden, "neurons")
print("output layer:", outputs, "scores, one per digit")
input layer : 784 numbers
hidden layer: 128 neurons
output layer: 10 scores, one per digit
Stack more hidden layers, one after another, and the network becomes deep. That's the "deep" in deep learning.
Common mistakes
- Thinking one pixel means something on its own. The meaning is in the pattern of many pixels.
- Forgetting the bias. Without it,
zwould be 0.8 here, not 0.5. - Mixing up
zand the output.zis the weighted sum plus bias; the output is what's left after the activation rule.
Quick recap
| Idea | In our example |
|---|---|
| A picture is numbers | 28 x 28 = 784 values from 0 to 255 |
| A neuron is a weighted sum plus a bias | 0 - 0.1 + 0.9 - 0.3 = 0.5 |
| Activation: negative becomes zero | z = -0.1 passes on 0 |
| Neurons sit in layers | 784 inputs, 128 hidden, 10 outputs |
| More hidden layers | A deep network |
Try it yourself
Change the input in the neuron cell to [1, 0, 1]. What is z, and what does the neuron pass on?
The notebook and mnist.npz are in the StudyEasy AI Community, free to join.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
The Forward Pass and Activation Functions: ReLU, Sigmoid, Tanh and Softmax
Follow numbers through a tiny 3-2-3 network in NumPy: one matrix product per layer, ReLU inside, softmax at the end, and a count of every weight and bias.
Loss and Backpropagation: How Wrong, and Who Is to Blame
The loss says how wrong the network was; backpropagation says how much each weight is to blame. MSE and cross-entropy, the chain rule by hand on a two-weight chain, then the same gradients from PyTorch autograd.
The Training Loop: Epochs, Mini-Batches, Learning Rate and Optimisers
How training is organised: shuffle, cut into mini-batches, take one step per batch, repeat per epoch. Real MNIST runs compare three learning rates, SGD against Adam, and show the overfitting signal in the validation loss.
Your First Neural Network with Keras: MNIST Digits at 97.7%
Build the 784-128-10 digit network in Keras in five steps: load and scale, Sequential with Flatten and Dense, compile, fit for ten epochs, and test once. It reads handwritten digits with 97.72% test accuracy.