Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Perceptron and multilayer perceptron

Perceptron is a single-layer linear classifier that learns a separating hyperplane; multilayer perceptrons (MLPs) add hidden layers and use backpropagation to learn non-linear decision boundaries.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two-sentence summary: The perceptron is the simplest learnable classifier an engineer uses to separate data that are linearly separable. A multilayer perceptron (MLP) enables modelling of non-linear relations (e.g. XOR) and is the fundamental building block of deep neural networks used in vision, speech and system-control tasks.

Key ideas

  • Perceptron model: The perceptron computes the net input z = w·x + b and a binary output ŷ = sign(z). Here sign(z) = +1 if z > 0 and -1 if z ≤ 0 (define your exam convention: some use 0 for negative class — state which you use).

  • Perceptron learning rule (Rosenblatt): For a training pair (x, y) with y ∈ {+1, -1}, if the perceptron misclassifies x, update weights by w ← w + η · y · x and b ← b + η · y, where η > 0 is the learning rate (a scalar). If correctly classified, no update. This is an online update done per example.

  • Perceptron convergence theorem (exact statement students are expected to state): If a training set {(x_i, y_i)}_{i=1..m} is linearly separable (there exists w* and b* such that y_i (w*·x_i + b*) > 0 for all i) and if the learning rate η is fixed and positive, then the perceptron learning algorithm performs only a finite number of weight updates and finally finds a set of weights that classify all examples correctly. Common additional statement: number of updates is bounded by (R/γ)^2 where R = max_i ||x_i|| (Euclidean norm) and γ is the margin min_i y_i (w*·x_i + b*) / ||w*||.

    • Assumptions to state in exam answers: finite training set, data are linearly separable, fixed η > 0 (often set to 1), and feature vectors have finite Euclidean norm.

    • Proof sketch students are asked to derive: show that the dot product w_k·w* increases by at least η γ ||w*|| on each mistake (or by ηγ if you take ||w*|| = 1), and that ||w_k||^2 increases at most by η^2 R^2 per mistake; combine these to bound the number of mistakes. In exams, give the stepwise algebraic inequalities.

  • Limitations of perceptron: cannot solve non-linearly-separable problems (e.g. XOR). No probabilistic outputs and no hidden representation.

  • Multilayer perceptron (MLP): A feedforward network with one or more hidden layers. Each layer computes z^{(l)} = W^{(l)} · a^{(l-1)} + b^{(l)} and activation a^{(l)} = φ^{(l)}(z^{(l)}), where a^{(0)} = x is the input and φ is an activation function (non-linear) such as sigmoid σ(u)=1/(1+e^{-u}), tanh, or ReLU max(0,u). By the universal approximation theorem, with a non-polynomial nonlinearity (e.g., sigmoid, tanh, ReLU), a single hidden layer can approximate any continuous function on a compact set to arbitrary accuracy.

  • Backpropagation: algorithm to compute gradients ∂L/∂W^{(l)} and ∂L/∂b^{(l)} for a loss L (e.g. mean squared error or cross-entropy). Use the chain rule: compute output-layer error terms δ^{(L)} then propagate backward with δ^{(l)} = (W^{(l+1)})^T δ^{(l+1)} * φ'(z^{(l)}). Weight updates (gradient descent) are W^{(l)} ← W^{(l)} - η · ∂L/∂W^{(l)}, where ∂L/∂W^{(l)} = δ^{(l)} (a^{(l-1)})^T.

  • Activation derivative: For sigmoid σ(u), σ'(u) = σ(u)·(1-σ(u)). For tanh, d/du tanh(u) = 1 - tanh^2(u). For ReLU, d/du ReLU(u) = 1 if u > 0 and 0 if u < 0 (state a convention at u = 0). For softmax with cross-entropy the derivative at the output layer simplifies to ∂L/∂z_j = y_j - t_j (useful in multiclass outputs).

  • Diagram instructions (what to draw and label):

    1. For a perceptron in R^2: draw the xy plane, plot two sets of points (e.g. +1 as blue circles, -1 as red crosses) separated by a straight line. Draw the decision boundary line labelled w·x + b = 0. Draw an arrow from origin to weight vector w and project one sample x onto w to illustrate sign(w·x + b). Label margin γ as perpendicular distance from nearest point to the line. 2) For a one-hidden-layer MLP: draw input layer (n nodes), one hidden layer (m nodes) and output node(s). Label weights W^{(1)}, W^{(2)}, activations a^{(1)}, a^{(2)}, net inputs z^{(1)}, z^{(2)}, and show arrow flow forward for prediction and arrows backward for δ signals in backprop.

Formulas

z = w · x + b • z is the net input (dimensionless) • w ∈ R^n, x ∈ R^n, b ∈ R • applies to perceptron and linear neuron

ŷ = sign(z) • ŷ ∈ {+1,-1} is predicted class (dimensionless)

w ← w + η · y · x • perceptron weight update when misclassified • η is learning rate (dimensionless)

b ← b + η · y • perceptron bias update (dimensionless)

z^{(l)} = W^{(l)} a^{(l-1)} + b^{(l)} • pre-activation at layer l • W^{(l)} ∈ R^{n_l × n_{l-1}}, a^{(l-1)} ∈ R^{n_{l-1}}

a^{(l)} = φ^{(l)}(z^{(l)}) • activation at layer l, φ elementwise (dimensionless)

L = 1/2 ∑_j (t_j - y_j)^2 • squared error loss (dimensionless) for outputs y_j and targets t_j; if you average over outputs/examples, include the appropriate 1/N factor

δ_j = ∂L/∂z_j = (y_j - t_j) · φ'(z_j) • output unit error term for MSE (dimensionless)

δ^{(l)} = (W^{(l+1)})^T δ^{(l+1)} * φ'(z^{(l)}) • backpropagate error to layer l (elementwise multiply “*”)

∂L/∂W^{(l)} = δ^{(l)} (a^{(l-1)})^T • gradient of loss w.r.t. weight matrix at layer l ∂L/∂b^{(l)} = δ^{(l)} • gradient of loss w.r.t. bias vector at layer l

W^{(l)} ← W^{(l)} - η · ∂L/∂W^{(l)} • gradient descent weight update (dimensionless) b^{(l)} ← b^{(l)} - η · ∂L/∂b^{(l)} • gradient descent bias update (dimensionless)

Note: For classification with softmax outputs and cross-entropy loss, gradient for output layer simplifies: ∂L/∂z_j = y_j - t_j.

Worked examples

  1. Numerical perceptron update (exam-style, step-by-step) Problem: Train a perceptron with η = 1 on two labelled 2D points: x1 = (2,1), y1 = +1; x2 = (-1,-1), y2 = -1. Start with w = (0,0) and b = 0. Show updates until convergence. Solution steps:
  • Step 0: w = (0,0), b = 0.
  • Present (x1,y1): compute z = w·x1 + b = 0. sign(z) = -1 by convention for z ≤ 0, so misclassified. Update: w ← w + η·y1·x1 = (0,0) + 1·(+1)·(2,1) = (2,1). b ← b + η·y1 = 0 + 1·(+1) = 1. After update: w = (2,1), b = 1.
  • Present (x2,y2): compute z = w·x2 + b = (2·(-1) + 1·(-1)) + 1 = (-2 -1) +1 = -2. sign(z) = -1, correct (y2 = -1). No update.
  • Present (x1,y1) again: z = (2·2 + 1·1) + 1 = 4 + 1 + 1 = 6 > 0, correct. No update. All points now correctly classified. Converged after one update. Final answer: w = (2,1), b = 1.
  1. Model-answer derivation: backprop for one hidden layer with sigmoid and MSE (long-answer type) Question: Show derivation of ∂L/∂W^{(2)} and ∂L/∂W^{(1)} for an MLP with one hidden layer. Answer outline (what examiners expect):
  • Define forward pass: z^{(1)} = W^{(1)} x + b^{(1)}, a^{(1)} = σ(z^{(1)}); z^{(2)} = W^{(2)} a^{(1)} + b^{(2)}, a^{(2)} = ŷ = σ(z^{(2)}).
  • Loss L = 1/2 ∑_j (t_j - a^{(2)}_j)^2.
  • Compute output layer error δ^{(2)}_j = ∂L/∂z^{(2)}_j = (a^{(2)}_j - t_j) · σ'(z^{(2)}_j). (Derive using chain rule: ∂L/∂a · ∂a/∂z.)
  • Then ∂L/∂W^{(2)} = δ^{(2)} (a^{(1)})^T.
  • For hidden layer: δ^{(1)} = (W^{(2)})^T δ^{(2)} * σ'(z^{(1)}).
  • Then ∂L/∂W^{(1)} = δ^{(1)} x^T.
  • State weight update: W^{(l)} ← W^{(l)} - η ∂L/∂W^{(l)} and similarly for biases.
  • Mention assumptions: using elementwise sigmoid and MSE; for softmax+cross-entropy, output δ simplifies to (a^{(2)} - t).
  1. Programming perceptron (short program) — Python (important syntax to remember) Code (examiners accept short runnable code):
# Simple perceptron training on two points
import numpy as np

X = np.array([[2,1], [-1,-1]])  # shape (2,2)
Y = np.array([1, -1])           # labels
w = np.zeros(2)
b = 0.0
eta = 1.0

for epoch in range(10):
    changed = False
    for x, y in zip(X, Y):
        z = np.dot(w, x) + b
        yhat = 1 if z > 0 else -1
        if yhat != y:
            w = w + eta * y * x
            b = b + eta * y
            changed = True
    if not changed:
        break

print('Final w =', w)
print('Final b =', b)

Sample exact output when run (this matches the numerical worked example above):

Final w = [2. 1.] Final b = 1.0

(Explain that students should set a random seed if using randomized initialisation.)

Common mistakes

  • Not stating linear separability as an assumption when invoking the perceptron convergence theorem.
  • Mixing up derivative signs in backprop: for MSE with output ŷ, ∂L/∂z = (ŷ - t)·φ'(z) (not t - ŷ).
  • Forgetting elementwise vs matrix multiplication when deriving vector formulas (use outer product δ (a^{(l-1)})^T for ∂W).
  • Applying perceptron to non-linearly separable data and expecting convergence.

For your exam

Types of questions and what full-marks answers include:

  • Definitions: define perceptron, linear separability, MLP, activation function — concise definitions and symbols.
  • Short notes: describe perceptron learning rule and its limitations — include update equation and a short note about convergence theorem.
  • Derivations: prove perceptron convergence theorem sketch (show the two inequalities and combine them) or derive backprop equations for one hidden layer — give chain-rule steps and final gradients.
  • Numericals: perform perceptron updates on given small data (show each update) — list initial weights, compute z, state prediction, show updated weights and bias.
  • Programs: write a small perceptron training loop in Python — include data, initialisation, update loop and final printouts.
  • Diagrams: draw a 2D dataset with decision boundary and label weight vector and margin — mention what labels should be and what the boundary equation is.

Full-marks answers must state assumptions, show steps (algebra or code), and give final values clearly.

Quick check

  1. State the perceptron weight update for a misclassified example (x,y).
  2. Give one reason why a single perceptron cannot learn XOR.
  3. For a sigmoid activation σ(u), write σ'(u).

Answers: 1) w ← w + η·y·x (and b ← b + η·y). 2) XOR is not linearly separable, so no single hyperplane separates classes. 3) σ'(u) = σ(u)·(1 - σ(u)).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?