FIELD GUIDE 04
Build a neural
network from scratch.
A small Python and NumPy network, explained from inputs to backpropagation.
01 01 / Start here
A neural network is
math with a learning loop.
Each layer combines numbers using weights and biases, then applies an activation. Training nudges those weights so predictions fit examples better.
What this tutorial builds
We will implement a small feed-forward neural network in Python with NumPy. It learns the XOR truth table, a compact example a single perceptron cannot solve with one straight decision boundary.
Make a prediction
Inputs move through a hidden layer and then to one output probability.
Measure mismatch
Binary cross-entropy summarizes how far predicted probabilities are from the labels.
Find the direction
The chain rule carries the error signal backward to each weight and bias.
You need basic Python and the idea of a function. Install NumPy with python -m pip install numpy. NumPy’s quickstart explains the arrays and matrix multiplication used below.
02 02 / The simple model
Perceptron first,
then a hidden layer.
A perceptron calculates a weighted sum and applies a decision rule. It is a useful starting point, but its boundary is linear.
Why a single perceptron misses XOR
For XOR, the output is 1 when exactly one input is 1: (0, 1) and (1, 0). It is 0 for (0, 0) and (1, 1). No single straight line separates those two positive examples from the two negative ones. A perceptron can learn linearly separable patterns such as AND or OR, but it needs hidden units to express XOR.
[0, 0]0same bits[0, 1]1different bits[1, 0]1different bits[1, 1]0same bitsThe network below has two input values, one hidden layer and one output. It is still small enough to follow by hand.
03 03 / Run the model
From arrays
to XOR predictions.
Every matrix shape follows the layer it represents: two inputs, four hidden units and one output.
One compact NumPy implementation
import numpy as np
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y = np.array([[0], [1], [1], [0]], dtype=float)
def sigmoid(z):
z = np.clip(z, -500, 500)
return 1.0 / (1.0 + np.exp(-z))
rng = np.random.default_rng(7)
W1 = rng.normal(0.0, 0.5, size=(2, 4))
b1 = np.zeros((1, 4))
W2 = rng.normal(0.0, 0.5, size=(4, 1))
b2 = np.zeros((1, 1))
learning_rate = 0.5
samples = len(X)
for epoch in range(30000):
# Forward pass
hidden = sigmoid(X @ W1 + b1)
probabilities = sigmoid(hidden @ W2 + b2)
# Backward pass: sigmoid output + binary cross-entropy
output_error = (probabilities - y) / samples
grad_W2 = hidden.T @ output_error
grad_b2 = output_error.sum(axis=0, keepdims=True)
hidden_error = (output_error @ W2.T) * hidden * (1.0 - hidden)
grad_W1 = X.T @ hidden_error
grad_b1 = hidden_error.sum(axis=0, keepdims=True)
# Gradient-descent update
W2 -= learning_rate * grad_W2
b2 -= learning_rate * grad_b2
W1 -= learning_rate * grad_W1
b1 -= learning_rate * grad_b1
predictions = sigmoid(sigmoid(X @ W1 + b1) @ W2 + b2)
print((predictions >= 0.5).astype(int).ravel())For the four teaching rows, the final line should classify the inputs as [0 1 1 0]. The exact probabilities depend on the learned weights; the threshold turns them into labels for this demonstration.
X @ W1 is matrix multiplication: NumPy’s @ operator combines each row of inputs with the first layer’s weights. Biases are then added, and sigmoid maps each value to the 0–1 range. See the official NumPy quickstart for array shapes and matrix products.
04 04 / Read the update
Backpropagation,
in four moves.
The update is a repeated calculation, not a special neural-network trick.
- 01 / forwardCalculate activations.
The hidden values are
sigmoid(X @ W1 + b1); the output is another weighted sum followed by sigmoid. - 02 / compareFind output error.
With sigmoid plus binary cross-entropy, the derivative at the output simplifies to
(prediction - label) / samples. - 03 / chainCarry the error back.
The hidden-layer derivative multiplies by the output weights and by the sigmoid derivative,
a × (1 - a). - 04 / updateMove parameters against the gradient.
Subtract learning rate times each gradient. Repeating the loop should reduce the loss for this small, well-defined example.
W1 has shape 2 × 4, b1 has shape 1 × 4, W2 has shape 4 × 1, and b2 has shape 1 × 1. A shape mismatch usually means an input, layer width or transpose does not match the equation.
05 05 / Keep the limits visible
A toy example
is not a deployment plan.
Four rows can explain a mechanism, but they cannot tell you how a model behaves in a real setting.
Common beginner mistakes
- Confusing element-wise multiplication
*with matrix multiplication@. - Updating weights before computing all gradients for the current step.
- Assuming a low training loss proves performance on new examples.
- Changing the threshold without saying what decision it represents.
- Applying a tutorial network to people or consequential decisions without suitable data and review.
For a next step, compare this hand-coded model with the neural-network field lesson, then explore gradient descent or choose a carefully scoped idea from beginner machine learning projects. The beginner course explains the difference between training, validation and test data.
FAQ Python / neural networks
Questions,
answered.
Short answers with the model’s limits left visible.
01 / answerCan I build a neural network from scratch with only Python?
Yes. You can write the math with built-in Python lists, but NumPy makes vector and matrix operations easier to express. This guide uses NumPy and no neural-network framework.
02 / answerIs a perceptron the same as a neural network?
A perceptron is a simple one-layer model often used to introduce neural networks. A multilayer network adds hidden transformations and can represent patterns a single linear decision boundary cannot, such as XOR.
03 / answerWhy does XOR need a hidden layer?
The XOR positives cannot be separated from the negatives by one straight line in the two-input plane. A hidden layer creates intermediate nonlinear features that let the output combine them.
04 / answerDoes this tutorial produce a production-ready model?
No. XOR is a four-row teaching example. Real applications need appropriate data, evaluation, privacy and security review, and evidence that errors are acceptable for the intended use.