solulubeta
Sign in
Machine Learning / Deep learning
03.01 · Lesson

Neural Networks and Backpropagation

Neurons, layers and activations, why non-linearity matters, the forward pass, backpropagation derived for a two-layer network, and the universal approximation idea.

4 min Quiz Free

A neural network is a stack of simple units that together can learn very complex functions. It is the foundation of image recognition, speech and language models.

A neuron

A neuron computes a weighted sum of its inputs plus a bias, then applies an activation:

a=g(w⊤x+b)a = g(\mathbf{w}^\top\mathbf{x} + b)

With a sigmoid activation, a single neuron is exactly logistic regression.

Layers

Neurons are organised in layers. A layer with weight matrix WW (shape out × in) transforms its input vector:

h=g(Wx+b)\mathbf{h} = g(W\mathbf{x} + \mathbf{b})

A multi-layer perceptron (MLP) chains layers:

flowchart LR
  subgraph Input
    x1((x1)) & x2((x2)) & x3((x3))
  end
  subgraph Hidden["Hidden layer (ReLU)"]
    h1((h1)) & h2((h2)) & h3((h3)) & h4((h4))
  end
  subgraph Output["Output (softmax)"]
    o1((ŷ1)) & o2((ŷ2))
  end
  x1 & x2 & x3 --> h1 & h2 & h3 & h4
  h1 & h2 & h3 & h4 --> o1 & o2
h=ReLU(W1x+b1),y^=softmax(W2h+b2)\mathbf{h} = \text{ReLU}(W_1\mathbf{x} + \mathbf{b}_1), \qquad \hat{\mathbf{y}} = \text{softmax}(W_2\mathbf{h} + \mathbf{b}_2)

Why non-linearity

Without activations, two layers collapse into one: W2(W1x)=(W2W1)xW_2(W_1\mathbf{x}) = (W_2W_1)\mathbf{x}, still linear. Non-linear activations let each layer bend the space, and stacking them builds complicated decision boundaries.

ActivationFormulaNotes
Sigmoid11+e−z\frac{1}{1+e^{-z}}Output in (0,1); saturates, so gradients vanish; used for binary outputs
Tanhez−e−zez+e−z\frac{e^z - e^{-z}}{e^z + e^{-z}}Zero-centred, but still saturates
ReLUmax⁡(0,z)\max(0, z)Default for hidden layers: cheap, no saturation for z>0z > 0; units can “die” at 0
Leaky ReLUmax⁡(0.01z,z)\max(0.01z, z)Avoids dead units
GELU / SiLUsmooth ReLU-likeUsed in transformers
Softmaxezk∑jezj\frac{e^{z_k}}{\sum_j e^{z_j}}Output layer for multi-class probabilities

Universal approximation: a network with one hidden layer and enough units can approximate any continuous function on a bounded region. In practice, depth is far more efficient than width: deep networks reuse features hierarchically (edges → textures → parts → objects).

Training: forward pass, loss, backward pass

  1. Forward: compute activations layer by layer and store them.
  2. Loss: e.g. cross-entropy between y^\hat{\mathbf{y}} and the true label.
  3. Backward (backpropagation): use the chain rule to get ∂L/∂W\partial L / \partial W for every layer, from the output back to the input.
  4. Update with SGD or Adam.

Backprop for a two-layer network

Forward: z1=W1x+b1\mathbf{z}_1 = W_1\mathbf{x} + \mathbf{b}_1, a1=ReLU(z1)\mathbf{a}_1 = \text{ReLU}(\mathbf{z}_1), z2=W2a1+b2\mathbf{z}_2 = W_2\mathbf{a}_1 + \mathbf{b}_2, y^=softmax(z2)\hat{\mathbf{y}} = \text{softmax}(\mathbf{z}_2), L=−log⁡y^trueL = -\log \hat{y}_{\text{true}}.

Backward (the softmax + cross-entropy gradient is famously clean):

δ2=∂L∂z2=y^−y\boldsymbol{\delta}_2 = \frac{\partial L}{\partial \mathbf{z}_2} = \hat{\mathbf{y}} - \mathbf{y} ∂L∂W2=δ2 a1⊤,∂L∂b2=δ2\frac{\partial L}{\partial W_2} = \boldsymbol{\delta}_2\,\mathbf{a}_1^\top, \qquad \frac{\partial L}{\partial \mathbf{b}_2} = \boldsymbol{\delta}_2 δ1=∂L∂z1=(W2⊤δ2)⊙1[z1>0]\boldsymbol{\delta}_1 = \frac{\partial L}{\partial \mathbf{z}_1} = (W_2^\top\boldsymbol{\delta}_2) \odot \mathbb{1}[\mathbf{z}_1 > 0] ∂L∂W1=δ1 x⊤,∂L∂b1=δ1\frac{\partial L}{\partial W_1} = \boldsymbol{\delta}_1\,\mathbf{x}^\top, \qquad \frac{\partial L}{\partial \mathbf{b}_1} = \boldsymbol{\delta}_1

The pattern repeats for any depth: error signal × input to the layer gives the weight gradient, and Wᵀ × error, gated by the activation’s derivative passes the error back one layer. The cost of the backward pass is about twice the forward pass.

Parameters vs hyperparameters

  • Parameters: weights and biases, learned by gradient descent.
  • Hyperparameters: chosen by you, such as the number of layers and units, activation, learning rate, batch size, epochs, regularisation and optimiser. Tune them on validation data (grid, random or Bayesian search).

Check yourself

Question 1 of 3

Why do neural networks need non-linear activation functions?

Question 2 of 3

Why is ReLU usually preferred over sigmoid in hidden layers?

Question 3 of 3

In backpropagation, the gradient for a layer's weights is the upstream gradient multiplied by…

Mark this as done when you are finished.

Item 14 of 24 · updated 2026-10-12