The Big Question
The perceptron gave us the simplest artificial neuron: compute a weighted sum, then make a decision. That was powerful enough for linearly separable data, but it failed on problems like XOR.
If one neuron can only draw one simple boundary, what happens when neurons help other neurons?
This is the key idea of a neural network. A single neuron is limited. A layer of neurons can detect several simple patterns at once. Another layer can combine those patterns into more useful patterns. Repeating this process creates representation learning.
The Limitation We Are Escaping
A single perceptron sees the original inputs and draws one linear decision boundary. For XOR, the positive examples are diagonal from each other, so no single straight line can separate the classes.
| x1 | x2 | XOR | Interpretation |
|---|---|---|---|
| 0 | 0 | 0 | neither input is on |
| 0 | 1 | 1 | exactly one input is on |
| 1 | 0 | 1 | exactly one input is on |
| 1 | 1 | 0 | both inputs are on |
The answer is not to search harder for the perfect single line. The answer is to change the representation. A hidden layer can create useful intermediate features such as OR and AND. Once those features exist, XOR becomes easy.
Core Intuition
Imagine asking one friend whether Jessie will order a burger. That friend tries to combine hunger, price, and social pressure into one decision. This is like one perceptron.
Now imagine asking a team. One friend estimates hunger. Another estimates whether the burger feels affordable. Another watches what the group is ordering. A final friend combines those opinions into the actual prediction.
That is a neural network. Hidden neurons do not directly output the final answer. They compute intermediate signals that make the final answer easier.
A Concrete Burger Network
Suppose the raw input is:
The entries mean Jessie has not eaten for 8 hours, the burger costs 15 dollars, and 4 friends are ordering burgers. A first hidden layer might transform these raw numbers into:
| Hidden neuron | Possible meaning | Activation |
|---|---|---|
| h1 | very hungry | 0.91 |
| h2 | affordable enough | 0.63 |
| h3 | strong social influence | 0.88 |
A second hidden layer could combine those signals into a broader concept such as high burger intent. The output layer then turns that representation into the final prediction.
Layers Build Representations
| Domain | Raw input | Hidden representation | Prediction |
|---|---|---|---|
| Restaurant order | hours since meal, price, friends ordering | hunger, affordability, social pressure | burger likelihood |
| Image | pixels | edges, corners, textures | face, car, tumor, digit |
| Language | tokens | phrases, syntax, topic signals | next word or intent |
This is why neural networks belong after representation learning. A network is not merely a bigger predictor. It is a system that repeatedly changes the representation until the prediction becomes simpler.
Interactive Demo
Original input space
XOR is not separable by one straight line in the original two-input space.
Hidden representation
After the hidden layer, the positive XOR examples gather near the lower-right region.
| x1 | x2 | h_OR | h_AND | Network output | XOR |
|---|---|---|---|---|---|
| 0 | 0 | 0.02 | 0.00 | 0.02 | 0 |
| 0 | 1 | 0.98 | 0.02 | 0.98 | 1 |
| 1 | 0 | 0.98 | 0.02 | 0.98 | 1 |
| 1 | 1 | 1.00 | 0.98 | 0.02 | 0 |
detects x1 OR x2
detects x1 AND x2
keeps OR but subtracts AND
The hidden layer builds two new features. The output neuron makes a simple decision from those learned features.
The demo uses two hidden neurons. One behaves like OR and one behaves like AND. The output neuron then predicts XOR by keeping OR but subtracting AND.
Mathematics
One Neuron
A neuron still starts with the same weighted sum from the perceptron:
Then it applies an activation function. For now, use the sigmoid:
The value is called the activation of the neuron. It becomes an input to the next layer.
One Layer
A layer contains several neurons. If the layer has three neurons, it computes three weighted sums:
Instead of writing each neuron separately, collect all weights into one matrix:
Then apply the activation function element by element:
This is the first major mental shift. A neural-network layer is a vectorized group of neurons computing many feature detectors at the same time.
Multiple Layers
A network stacks these transformations. Each layer receives the previous layer representation:
The first layer sees the raw input. The second layer sees the first hidden representation. The output layer sees the final hidden representation.
Worked Forward Pass
Suppose the input has two features:
Use a hidden layer with two neurons:
The hidden pre-activation is:
Apply sigmoid to each entry:
These two numbers are not the final prediction. They are the hidden representation passed to the next layer. This computation from input to output is called the forward pass.
XOR With a Hidden Layer
A useful way to solve XOR is to build two hidden features:
The first hidden neuron activates when at least one input is on. The second activates when both inputs are on. The output can then compute:
In plain English: predict 1 when OR is true but AND is false. The hidden layer made the right representation, so the output neuron only needs a simple boundary.
Why Depth Helps
A single hidden layer with enough neurons can approximate any continuous function on a bounded region. This is the universal approximation idea. It says neural networks are expressive enough in principle.
It does not say the network will learn the function easily, that one hidden layer is always efficient, or that more parameters automatically generalize better. In practice, depth often helps because later layers can reuse features learned by earlier layers.
Parameter Count
If a layer has inputs and neurons, it has weights and biases.
| Input size | n | Number of features entering the layer |
| Neurons | m | Number of separate weighted sums in the layer |
| Weights | mn | One weight for every input-neuron connection |
| Biases | m | One bias per neuron |
This is why modern neural networks can contain millions or billions of parameters: every connection has a learnable weight.
The Missing Ingredient
If every layer were only linear, stacking layers would not help. A product of linear transformations is still a linear transformation:
Activation functions are what prevent the whole network from collapsing into one linear model. That is why the next lesson focuses on non-linearity.
Implementation
This implementation builds a neural network with one hidden layer. It does not train the weights yet. The goal is to understand the architecture and the forward pass.
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
class NeuralNetwork:
def __init__(self, input_dim, hidden_dim, output_dim):
rng = np.random.default_rng(seed=0)
self.W1 = rng.normal(0, 0.1, size=(hidden_dim, input_dim))
self.b1 = np.zeros((hidden_dim, 1))
self.W2 = rng.normal(0, 0.1, size=(output_dim, hidden_dim))
self.b2 = np.zeros((output_dim, 1))
def forward(self, x):
# x has shape (input_dim, batch_size)
z1 = self.W1 @ x + self.b1
a1 = sigmoid(z1)
z2 = self.W2 @ a1 + self.b2
y_hat = sigmoid(z2)
return y_hat, {
"z1": z1,
"a1": a1,
"z2": z2,
}
X = np.array([
[0, 0, 1, 1],
[0, 1, 0, 1],
])
network = NeuralNetwork(input_dim=2, hidden_dim=3, output_dim=1)
predictions, cache = network.forward(X)
print(predictions)A Hand-Built XOR Network
To see the architecture solve something concrete, we can set the weights by hand. Training will come later with backpropagation.
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
X = np.array([
[0, 0, 1, 1],
[0, 1, 0, 1],
])
k = 12
# Hidden neuron 1 approximates OR.
# Hidden neuron 2 approximates AND.
W1 = np.array([
[k, k],
[k, k],
])
b1 = np.array([
[-0.5 * k],
[-1.5 * k],
])
H = sigmoid(W1 @ X + b1)
# Output keeps OR and subtracts AND.
W2 = np.array([[k, -k]])
b2 = np.array([[-0.5 * k]])
y_hat = sigmoid(W2 @ H + b2)
print(np.round(H, 2))
print(np.round(y_hat, 2))The important part is not the exact numbers. The important part is the structure: hidden neurons create useful features, and the output neuron combines those features.
Interview Discussion
What is a neural network?
A neural network is a composition of layers. Each layer computes weighted sums, applies activation functions, and passes a new representation to the next layer.
What is a hidden layer?
A hidden layer is a layer between the input and output. It does not directly produce the final prediction; it produces intermediate features used by later layers.
Why can a neural network solve XOR when one perceptron cannot?
A hidden layer can transform the original inputs into new features such as OR and AND. In that hidden representation, a simple output neuron can separate the classes.
What happens during the forward pass?
The model computes predictions by moving information from inputs through hidden layers to the output. No weights are updated during the forward pass.
Why do activation functions matter?
Without non-linear activations, stacked linear layers collapse into a single linear transformation. Activation functions let networks model non-linear relationships.
What does the universal approximation theorem say?
It says a neural network with a sufficiently large hidden layer can approximate any continuous function on a bounded region, under suitable conditions.
Active Recall
1. Why can a single perceptron only learn a linear decision boundary?
2. What does a hidden neuron compute before applying its activation function?
3. Write the equation for one neural-network layer.
4. In the XOR example, what do the OR and AND hidden neurons make possible?
5. What is the difference between the forward pass and training?
6. Why would a network without activation functions collapse into one linear model?
Common Mistakes
- Thinking hidden layers are manually named features. They are learned during training, even if we can sometimes interpret them afterward.
- Confusing a neuron with a whole layer. A layer is many neurons computed together.
- Thinking the forward pass trains the network. It only computes predictions.
- Believing deeper networks always perform better. Depth increases expressive power but can make optimization harder.
- Forgetting that activation functions are essential for non-linear behavior.
Connection To Activation Functions
The perceptron showed how one artificial neuron makes a decision. Neural networks show what happens when many neurons are connected into layers: each layer transforms the representation, and later layers make predictions from those transformed features. The next question is why the activation function is so important.