Neural Network Fundamentals
Level: 5 (Mastery — cross-domain: math + coding + proof) Time limit: 90 minutes Total marks: 60
Notation: for a layer, , . Vectors are columns. Use natural log for cross-entropy. Show all derivations; state assumptions.
Question 1 — Backpropagation from first principles + autograd (24 marks)
Consider a 2-layer network for multiclass classification with classes and input :
with loss (one-hot target ).
(a) Prove that . Show the softmax Jacobian step explicitly. (6)
(b) Derive , , and the backpropagated signal in terms of , , and . (6)
(c) Take . A concrete forward pass gives (numbers exact): Given , compute , then numerically. (8)
(d) Explain, referring to a computational graph, why reverse-mode autodiff computes all these gradients in one backward sweep at cost , whereas forward-mode would cost passes. (4)
Question 2 — Activations, gradient flow, and initialization (22 marks)
(a) For sigmoid , prove and find . Using this bound, show that in a deep chain of sigmoid layers with weights of magnitude , the gradient magnitude is bounded by , and state the condition on for vanishing gradients. (7)
(b) Compare ReLU, Leaky ReLU (), ELU, and GELU: give each function, its derivative, and one sentence on how each mitigates the vanishing-gradient problem or the "dying ReLU" problem. (7)
(c) Xavier vs He. For a linear layer with i.i.d. zero-mean inputs of variance and i.i.d. zero-mean weights of variance , derive . Hence justify why He initialization uses for ReLU networks (account for ReLU halving the variance), while Xavier uses (or ). (8)
Question 3 — Universal approximation, coding, and bias (14 marks)
(a) State the Universal Approximation Theorem for a single hidden layer. Then constructively show that a 1-hidden-layer network with step (or sigmoid) activations can approximate the "bump" indicator on , by specifying weights/biases of 2 hidden units and 1 output. Explain the essential role of the bias terms here. (7)
(b) Write a NumPy function forward(X, W1, b1, W2, b2) implementing forward propagation for one hidden layer with ReLU and a softmax output, returning class probabilities for a batch X of shape (N, d). Show the code and briefly justify the numerically-stable softmax. (7)
Answer keyMark scheme & solutions
Question 1
(a) (6 marks) Softmax: . Jacobian: (1 mark). (2). (2). Since (one-hot): , i.e. . (1)
(b) (6 marks) Let .
- (outer product) — from . (2)
- . (1)
- Backprop: ; then . (3)
(c) (8 marks) . (2) . . (3) ; so . (1) . (2)
(d) (4 marks) The network is a DAG of primitive ops. Reverse-mode: one forward pass caches intermediate values; one backward pass propagates (adjoints) from the scalar loss using the chain rule, reusing shared subexpressions — each edge traversed once, so cost forward cost, independent of parameter count (2). Forward-mode propagates one input-direction derivative per pass, needing passes to get all input sensitivities; since #outputs (scalar loss) #inputs, reverse mode is optimal for gradients (2).
Question 2
(a) (7 marks) , . (2) Maximize : max at (i.e. ), value . (2) Chain of layers: multiplies factors each ; , , so bound . (2) Vanishing when ; gradient exponentially. (1)
(b) (7 marks) (1 mark each function+derivative, ~0.75 explanation)
- ReLU: , . Derivative is 1 for positive → no vanishing on active units; dying ReLU when stuck at .
- Leaky ReLU: if else ; or . Nonzero negative slope prevents dead units.
- ELU: if else ; or . Smooth, negative saturation with nonzero gradient near 0, pushes mean activations toward zero.
- GELU: (Gaussian CDF); . Smooth, differentiable everywhere, allows small gradients for slightly negative inputs. (mitigation sentences = 3)
(c) (8 marks) , independent zero-mean terms: (using for zero-mean indep). (3) To preserve variance (): need (Xavier, forward). Balancing forward+backward gives . (2) ReLU zeroes ~half the units, halving output variance: effective . To keep it need (He). (3)
Question 3
(a) (7 marks) UAT: a feedforward net with a single hidden layer of finitely many neurons and a non-constant, bounded (or non-polynomial) activation can approximate any continuous function on a compact set to arbitrary accuracy in sup norm. (2) Construction of bump with sigmoids (steep gain ):
- Hidden unit 1: (rises ~0→1 at ).
- Hidden unit 2: (rises ~0→1 at ).
- Output: as . (3) Bias role: the bias (resp. ) shifts the step's location to (resp. ). Without bias the step is fixed at the origin and the bump cannot be positioned; bias sets the threshold/offset that translates the activation along the input axis. (2)
(b) (7 marks)
import numpy as np
def forward(X, W1, b1, W2, b2):
# X: (N, d); W1: (h, d); b1: (h,); W2: (K, h); b2: (K,)
Z1 = X @ W1.T + b1 # (N, h)
A1 = np.maximum(0, Z1) # ReLU
Z2 = A1 @ W2.T + b2 # (N, K)
Z2 = Z2 - Z2.max(axis=1, keepdims=True) # numerical stability
E = np.exp(Z2)
return E / E.sum(axis=1, keepdims=True) # softmax probs (N, K)Marks: correct linear+ReLU (2), correct softmax over class axis (2), stable-softmax subtraction of row max (2), correct shapes/vectorization (1).
Stability: subtracting the max makes the largest exponent , avoiding exp overflow; softmax is shift-invariant so the result is unchanged.
[
{"claim": "delta2 = yhat - y = [0.2, -0.7, 0.5]", "code": "yhat=Matrix([0.2,0.3,0.5]); y=Matrix([0,1,0]); d2=yhat-y; result = (d2==Matrix([0.2,-0.7,0.5]))"},
{"claim": "W2^T delta2 = [-0.3, 0.2]", "code": "W2=Matrix([[1,2],[0,1],[-1,1]]); d2=Matrix([0.2,-0.7,0.5]); v=W2.T*d2; result = (simplify(v-Matrix([-0.3,0.2]))==zeros(2,1))"},
{"claim": "delta1 = [-0.3, 0.072]", "code": "W2=Matrix([[1,2],[0,1],[-1,1]]); d2=Matrix([0.2,-0.7,0.5]); a1=Matrix([0,0.8]); v=W2.T*d2; deriv=Matrix([1-a1[0]**2,1-a1[1]**2]); d1=Matrix([v[0]*deriv[0], v[1]*deriv[1]]); result = (simplify(d1-Matrix([-0.3,0.072]))==zeros(2,1))"},
{"claim": "max of sigma(1-sigma) is 1/4 at sigma=1/2", "code": "s=symbols('s'); f=s*(1-s); crit=solve(diff(f,s),s)[0]; result = (crit==Rational(1,2)) and (f.subs(s,crit)==Rational(1,4))"}
]