What Is Deep Learning?
A straight line, a threshold or a small tree is enough for many problems. But some patterns, such as the shapes in an image or the meaning of a sentence, are far too complex to describe with a handful of parameters. Deep learning handles them by stacking many simple computing units into layers and letting the data decide what each layer should compute.
From one neuron to a network
An artificial neuron takes several inputs , multiplies each by a weight, adds a bias and passes the sum through a non-linear activation function :
Popular activations are the sigmoid , which squeezes any number into , , which squeezes into , and ReLU, . One neuron with a sigmoid is exactly logistic regression: it can only draw a straight boundary.
A layer is many neurons reading the same inputs, written compactly with a weight matrix and a bias vector : . A neural network feeds the output of one layer into the next:
The layers between input and output are hidden layers. A network with several of them is deep, and deep learning is machine learning with such multi-layer networks.
Why depth helps: XOR
The XOR problem has four points: and belong to class 0, and to class 1. No straight line separates them, so a single neuron must fail. With one hidden layer of four neurons, the network can bend the space so that a line separates the classes in the hidden layer's coordinates.
The example builds that network in plain NumPy: a forward pass computes the prediction, the loss measures the error, and a backward pass uses the chain rule to compute how the loss changes with every weight. Then each weight takes a small step downhill. This is how all neural networks train, and the Deep Learning course derives it step by step.
import numpy as np
# XOR: no single straight line separates the 1s from the 0s, so one neuron is not enough.
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y = np.array([[0], [1], [1], [0]], dtype=float)
rng = np.random.default_rng(1)
W1, b1 = rng.normal(0, 1, (2, 4)), np.zeros(4) # layer 1: 2 inputs -> 4 hidden neurons
W2, b2 = rng.normal(0, 1, (4, 1)), np.zeros(1) # layer 2: 4 hidden -> 1 output
sigmoid = lambda z: 1 / (1 + np.exp(-z))
lr = 0.5
for step in range(3001):
h = np.tanh(X @ W1 + b1) # forward pass: layer 1
p = sigmoid(h @ W2 + b2) # forward pass: layer 2 (a probability)
loss = -np.mean(y * np.log(p) + (1 - y) * np.log(1 - p))
if step % 1000 == 0:
print(f"step {step:4}: loss {loss:.4f}")
# backward pass: the chain rule, layer by layer
dz2 = (p - y) / len(X)
dW2, db2 = h.T @ dz2, dz2.sum(axis=0)
dz1 = (dz2 @ W2.T) * (1 - h ** 2)
dW1, db1 = X.T @ dz1, dz1.sum(axis=0)
W1 -= lr * dW1; b1 -= lr * db1; W2 -= lr * dW2; b2 -= lr * db2
print("predictions:", p.ravel().round(3))
print("parameters: ", W1.size + b1.size + W2.size + b2.size)step 0: loss 0.6712
step 1000: loss 0.0051
step 2000: loss 0.0024
step 3000: loss 0.0016
predictions: [0. 0.998 0.998 0.003]
parameters: 17
The loss (binary cross-entropy) falls from 0.6712, about what random guessing gives, to 0.0016, and the four predictions are close to the targets 0, 1, 1, 0. The network has 17 parameters: weights and 4 biases in the hidden layer, weights and 1 bias in the output.
Note: Change the seed to
default_rng(0)and run again. The loss gets stuck near 0.35 and two predictions stay at 0.5. Training a network is a search in a landscape with flat regions and poor local minima, and the starting weights decide where it ends. Deep-learning practice is full of techniques (careful initialisation, better optimisers, more neurons) that make such failures rare.
The same idea with a library
scikit-learn's MLPClassifier (multi-layer perceptron) trains small networks with the same forward and backward passes. On the curved "moons" data, a network with two hidden layers of 16 neurons beats logistic regression, which can only draw a line:
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
X, y = make_moons(n_samples=400, noise=0.2, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
linear = LogisticRegression().fit(X_train, y_train)
net = MLPClassifier(hidden_layer_sizes=(16, 16), solver="lbfgs", max_iter=3000, random_state=0)
net.fit(X_train, y_train)
print("logistic regression (one linear unit):", round(linear.score(X_test, y_test), 3))
print("neural network, 2 hidden layers of 16: ", round(net.score(X_test, y_test), 3))
print("weight matrices:", [w.shape for w in net.coefs_])
print("parameters:", sum(w.size for w in net.coefs_) + sum(b.size for b in net.intercepts_))logistic regression (one linear unit): 0.85
neural network, 2 hidden layers of 16: 0.95
weight matrices: [(2, 16), (16, 16), (16, 1)]
parameters: 337
On 200 test points the network scores 0.95 against 0.85. It has 337 parameters: in the first layer, in the second and in the output.
What a deep-learning model looks like in PyTorch
Real deep-learning work uses frameworks such as PyTorch, which provide layers, automatic gradients and GPU support. The same 2-16-16-1 architecture takes a few lines. PyTorch cannot run in the browser, so this example is recorded: its output was produced by running exactly this code on a CPU.
import torch
from torch import nn
torch.manual_seed(0)
model = nn.Sequential( # the same idea as the NumPy network, written with PyTorch layers
nn.Linear(2, 16), nn.ReLU(),
nn.Linear(16, 16), nn.ReLU(),
nn.Linear(16, 1), nn.Sigmoid(),
)
print(model)
print("trainable parameters:", sum(p.numel() for p in model.parameters()))
x = torch.tensor([[0.5, -0.2], [1.5, 0.3]]) # a batch of two inputs
with torch.no_grad():
print("output shape:", tuple(model(x).shape))
print("untrained outputs:", [round(v, 3) for v in model(x).squeeze(1).tolist()])Sequential(
(0): Linear(in_features=2, out_features=16, bias=True)
(1): ReLU()
(2): Linear(in_features=16, out_features=16, bias=True)
(3): ReLU()
(4): Linear(in_features=16, out_features=1, bias=True)
(5): Sigmoid()
)
trainable parameters: 337
output shape: (2, 1)
untrained outputs: [0.545, 0.533]
Again 337 parameters. Before training, the outputs are near 0.5: the random weights know nothing yet. Training would follow the same loop as the NumPy example, with PyTorch computing the gradients automatically.
What makes deep learning different
- Representation learning. Classical ML usually needs people to design the input features (for an image: edge counts, colour histograms). Deep networks learn useful features themselves from raw pixels, audio samples or text tokens: early layers detect simple patterns, later layers combine them into complex ones.
- Scale. Deep models have from thousands to hundreds of billions of parameters. They need much more data and computation than classical models, and they keep improving with more of both for longer.
- Architectures for each kind of data. Convolutional networks for images, recurrent networks and transformers for sequences, graph networks for graphs. You will meet them in Deep Learning, Computer Vision and NLP, Transformers & LLMs.
For a lighter, hands-on introduction to neural networks, see Neural Networks in the Machine Learning course and Neural Networks in the ai-llm course.
Pitfalls
- Reaching for deep learning first. On small tabular data sets, gradient-boosted trees or logistic regression are usually as good or better, faster and easier to explain.
- Forgetting that training can fail silently. A stuck loss, as with seed 0 above, still returns a model. Always watch the loss and check predictions on held-out data.
- Counting on the network to fix bad data. More layers do not repair wrong labels or unrepresentative samples.
Practice
- How many parameters does a fully connected network with layer sizes 10, 32, 32 and 3 have (weights plus biases)?
Answer
, then , then . In total .
- Why does a network whose hidden layers have no activation function (just ) gain nothing from depth?
Answer
A linear function of a linear function is linear: . Without non-linear activations any stack of layers collapses into a single linear layer and still cannot solve XOR.
Test yourself
What makes a neural network "deep"?
- It uses a GPU
- It has several hidden layers between input and output
- It has more than a million parameters
- It is trained on images
Why can a single neuron with a sigmoid not solve XOR?
- It has too few inputs
- It can only draw a straight decision boundary, and no line separates XOR's classes
- Sigmoid outputs are always 0.5
- XOR needs a GPU
What does the backward pass compute?
- The predictions
- The gradient of the loss with respect to every weight, by the chain rule
- The training data
- The number of layers
What is a key practical difference between deep learning and classical ML?
- Deep networks learn useful features from raw data instead of relying on hand-designed ones
- Deep learning needs no data
- Classical ML cannot classify
- Deep learning never overfits