Level 4 — ApplicationMachine Learning (Aerospace Applications)

Machine Learning (Aerospace Applications)

60 minutes60 marksprintable — key stays hidden on paper

Level 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60

Answer all questions. Show all working. Calculators permitted. Where numeric answers are requested, keep at least 4 significant figures unless told otherwise.


Question 1 — System Identification via Ridge Regression (12 marks)

A quadrotor's pitch-rate dynamics are approximated by the linear model y=w0+w1xy = w_0 + w_1 x, where xx is elevator deflection (deg) and yy is measured pitch acceleration (deg/s²). You collect three flight-test samples:

(x,y)=(1,2), (2,2), (3,4)(x,y) = (1,\,2),\ (2,\,2),\ (3,\,4)

(a) Write the ridge-regularized cost J(w)J(\mathbf{w}) that penalizes only w1w_1 (not the bias w0w_0) with strength λ\lambda. (2 marks)

(b) Derive the closed-form normal equation for this ridge problem and state the modified matrix that must be inverted. (3 marks)

(c) With λ=1\lambda = 1, solve for w0w_0 and w1w_1 numerically. (5 marks)

(d) Explain physically why regularizing w1w_1 but not w0w_0 is the standard convention. (2 marks)


Question 2 — Backpropagation Through a Tiny Network (14 marks)

Consider a single-input network for a fault-detection classifier:

z1=w1x+b1,a1=tanh(z1),z2=w2a1+b2,y^=σ(z2)z_1 = w_1 x + b_1,\quad a_1 = \tanh(z_1),\quad z_2 = w_2 a_1 + b_2,\quad \hat{y} = \sigma(z_2)

Loss is binary cross-entropy L=[ylny^+(1y)ln(1y^)]L = -[y\ln\hat{y} + (1-y)\ln(1-\hat{y})].

Given: x=1, w1=0.5, b1=0, w2=1, b2=0, y=1x = 1,\ w_1 = 0.5,\ b_1 = 0,\ w_2 = 1,\ b_2 = 0,\ y = 1.

(a) Perform the forward pass: compute z1,a1,z2,y^z_1, a_1, z_2, \hat{y} and the loss LL. (4 marks)

(b) Show that Lz2=y^y\dfrac{\partial L}{\partial z_2} = \hat{y} - y and evaluate it. (3 marks)

(c) Using the chain rule, derive and evaluate Lw1\dfrac{\partial L}{\partial w_1}. (Recall tanh(z)=1tanh2z\tanh'(z) = 1 - \tanh^2 z.) (5 marks)

(d) One gradient-descent step with learning rate η=0.1\eta = 0.1 updates w1w_1. Give the new w1w_1. (2 marks)


Question 3 — Scaled Dot-Product Self-Attention (12 marks)

A transformer processes two token embeddings. After projection you obtain:

Q=[1001],K=[1001],V=[2003]Q = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix},\quad K = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix},\quad V = \begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix}

with key dimension dk=2d_k = 2.

(a) Write the self-attention formula and compute the scaled score matrix QKdk\dfrac{QK^\top}{\sqrt{d_k}}. (3 marks)

(b) Apply row-wise softmax to obtain the attention weight matrix. Give entries to 4 decimals. (5 marks)

(c) Compute the output matrix (attention weights times VV). (3 marks)

(d) State one reason the dk\sqrt{d_k} scaling is used. (1 mark)


Question 4 — Q-Learning for a Guidance MDP (12 marks)

An interceptor operates in a 2-state MDP (states S0,S1S_0, S_1; S1S_1 is terminal). From S0S_0, action fire gives reward r=+10r=+10 and transitions to S1S_1; action wait gives reward r=1r=-1 and stays in S0S_0. Discount γ=0.9\gamma = 0.9. Learning rate α=0.5\alpha = 0.5. All Q-values initialised to 0.

(a) Write the Q-learning update rule. (2 marks)

(b) Starting from Q=0Q=0, apply the update for the transition (S0,wait,r=1,S0)(S_0,\text{wait},\,r=-1,\,S_0), then for (S0,fire,r=+10,S1)(S_0,\text{fire},\,r=+10,\,S_1) where S1S_1 is terminal (maxaQ(S1,)=0\max_a Q(S_1,\cdot)=0). Give the updated Q-values. (5 marks)

(c) Determine the exact converged optimal values Q(S0,fire)Q^*(S_0,\text{fire}) and Q(S0,wait)Q^*(S_0,\text{wait}) using the Bellman optimality equation, and state the optimal policy. (5 marks)


Question 5 — Bias–Variance and Cross-Validation (10 marks)

You fit polynomial models of degree d=1,3,9d = 1, 3, 9 to noisy aerodynamic drag data using 5-fold cross-validation.

(a) Sketch (describe) the expected trend of training error and validation error as dd increases, and identify which regime each degree likely occupies (underfit / good / overfit). (4 marks)

(b) Define bias and variance in one sentence each, and state which dominates the error for d=1d=1 and for d=9d=9. (3 marks)

(c) In 5-fold CV, a dataset of 200 samples is split. State how many samples are used for training and validation in each fold, and give one reason to prefer k-fold CV over a single train/test split. (3 marks)

Answer keyMark scheme & solutions

Question 1 (12 marks)

(a) Cost with bias unpenalized: (2) J(w)=i=13(w0+w1xiyi)2+λw12J(\mathbf{w}) = \sum_{i=1}^{3}(w_0 + w_1 x_i - y_i)^2 + \lambda w_1^2 (1 mark squared-error term, 1 mark for λw12\lambda w_1^2 only on w1w_1.)

(b) With design matrix X=[111213]X = \begin{bmatrix}1&1\\1&2\\1&3\end{bmatrix}, the normal equation is (XX+λR)w=Xy(X^\top X + \lambda R)\mathbf{w} = X^\top y, where the penalty matrix R=[0001]R = \begin{bmatrix}0&0\\0&1\end{bmatrix} (zero in bias slot). (3) Solution: w=(XX+λR)1Xy\mathbf{w} = (X^\top X + \lambda R)^{-1}X^\top y.

(c) Compute: (5) XX=[36614]X^\top X = \begin{bmatrix}3 & 6\\ 6 & 14\end{bmatrix}, Xy=[2+2+42+4+12]=[818]X^\top y = \begin{bmatrix} 2+2+4\\ 2+4+12\end{bmatrix}=\begin{bmatrix}8\\18\end{bmatrix}.

With λ=1\lambda=1: A=XX+R=[36615]A = X^\top X + R = \begin{bmatrix}3 & 6\\ 6 & 15\end{bmatrix}, detA=4536=9\det A = 45-36 = 9.

A1=19[15663]A^{-1} = \frac{1}{9}\begin{bmatrix}15 & -6\\ -6 & 3\end{bmatrix}.

w=A1[818]=19[15861868+318]=19[126]\mathbf{w} = A^{-1}\begin{bmatrix}8\\18\end{bmatrix} = \frac{1}{9}\begin{bmatrix}15\cdot8 - 6\cdot18\\ -6\cdot8 + 3\cdot18\end{bmatrix} = \frac{1}{9}\begin{bmatrix}12\\ 6\end{bmatrix}.

w0=12/9=1.3333,w1=6/9=0.6667\boxed{w_0 = 12/9 = 1.3333,\quad w_1 = 6/9 = 0.6667}

(2 for XX,XyX^\top X, X^\top y; 1 for adding RR; 2 for inversion & solve.)

(d) The bias w0w_0 merely sets the output offset (mean level); shrinking it toward zero would introduce systematic error and makes the fit depend on the arbitrary origin of yy. Penalizing only slopes keeps the model shift-invariant and controls model complexity without biasing the intercept. (2)


Question 2 (14 marks)

(a) Forward pass: (4) z1=0.51+0=0.5z_1 = 0.5\cdot1 + 0 = 0.5 a1=tanh(0.5)=0.46212a_1 = \tanh(0.5) = 0.46212 z2=10.46212+0=0.46212z_2 = 1\cdot0.46212 + 0 = 0.46212 y^=σ(0.46212)=1/(1+e0.46212)=0.61350\hat{y} = \sigma(0.46212) = 1/(1+e^{-0.46212}) = 0.61350 L=ln(0.61350)=0.48865L = -\ln(0.61350) = 0.48865 (1 each for a1,y^,La_1,\hat y, L + zz values.)

(b) Ly^=yy^+1y1y^=y^yy^(1y^)\dfrac{\partial L}{\partial \hat y} = \dfrac{-y}{\hat y}+\dfrac{1-y}{1-\hat y}=\dfrac{\hat y - y}{\hat y(1-\hat y)}; and y^z2=σ(z2)=y^(1y^)\dfrac{\partial \hat y}{\partial z_2}=\sigma'(z_2)=\hat y(1-\hat y). Product gives Lz2=y^y\dfrac{\partial L}{\partial z_2}=\hat y - y. (3) Value: 0.613501=0.386500.61350 - 1 = -0.38650.

(c) Chain rule: (5) Lw1=Lz2w2(1a12)x\frac{\partial L}{\partial w_1} = \frac{\partial L}{\partial z_2}\cdot w_2 \cdot (1 - a_1^2)\cdot x 1a12=10.462122=10.21356=0.786441 - a_1^2 = 1 - 0.46212^2 = 1 - 0.21356 = 0.78644. Lw1=(0.38650)(1)(0.78644)(1)=0.30396\dfrac{\partial L}{\partial w_1} = (-0.38650)(1)(0.78644)(1) = -0.30396.

(d) Update: w1new=0.50.1(0.30396)=0.5+0.030396=0.53040w_1^{new} = 0.5 - 0.1(-0.30396) = 0.5 + 0.030396 = 0.53040. (2)


Question 3 (12 marks)

(a) Attn(Q,K,V)=softmax ⁣(QKdk)V\text{Attn}(Q,K,V)=\text{softmax}\!\left(\dfrac{QK^\top}{\sqrt{d_k}}\right)V. (3) QK=IQK^\top = I, so scaled scores =12[1001]=[0.7071000.7071]= \dfrac{1}{\sqrt2}\begin{bmatrix}1&0\\0&1\end{bmatrix}=\begin{bmatrix}0.7071&0\\0&0.7071\end{bmatrix}.

(b) Row softmax of [0.7071,0][0.7071, 0]: (5) e0.7071=2.0281, e0=1e^{0.7071}=2.0281,\ e^0=1, sum =3.0281=3.0281. Weight on diagonal =2.0281/3.0281=0.6698=2.0281/3.0281 = 0.6698; off-diagonal =1/3.0281=0.3302=1/3.0281 = 0.3302. W=[0.66980.33020.33020.6698]W = \begin{bmatrix}0.6698 & 0.3302\\ 0.3302 & 0.6698\end{bmatrix}

(c) Output =WV= WV, V=[2003]V=\begin{bmatrix}2&0\\0&3\end{bmatrix}: (3) Row 1: [0.66982, 0.33023]=[1.3396, 0.9906][0.6698\cdot2,\ 0.3302\cdot3] = [1.3396,\ 0.9906]. Row 2: [0.33022, 0.66983]=[0.6604, 2.0094][0.3302\cdot2,\ 0.6698\cdot3] = [0.6604,\ 2.0094]. O=[1.33960.99060.66042.0094]O = \begin{bmatrix}1.3396 & 0.9906\\ 0.6604 & 2.0094\end{bmatrix}

(d) Scaling by dk\sqrt{d_k} keeps the dot-product magnitudes from growing with dimension, preventing softmax saturation (tiny gradients). (1)


Question 4 (12 marks)

(a) (2) Q(s,a)Q(s,a)+α[r+γmaxaQ(s,a)Q(s,a)]Q(s,a)\leftarrow Q(s,a)+\alpha\Big[r+\gamma\max_{a'}Q(s',a')-Q(s,a)\Big]

(b) (5) Update 1 — (S0,wait,1,S0)(S_0,\text{wait},-1,S_0), maxaQ(S0,)=0\max_a Q(S_0,\cdot)=0: Q(S0,wait)=0+0.5[1+0.900]=0.5Q(S_0,\text{wait}) = 0 + 0.5[-1 + 0.9\cdot0 - 0] = -0.5.

Update 2 — (S0,fire,+10,S1)(S_0,\text{fire},+10,S_1), terminal max=0\Rightarrow \max=0: Q(S0,fire)=0+0.5[10+0.900]=5.0Q(S_0,\text{fire}) = 0 + 0.5[10 + 0.9\cdot0 - 0] = 5.0.

Result: Q(S0,wait)=0.5Q(S_0,\text{wait})=-0.5, Q(S0,fire)=5.0Q(S_0,\text{fire})=5.0. (2.5 each)

(c) Bellman optimality. (5) Fire is terminal-transition: Q(S0,fire)=10+γ0=10Q^*(S_0,\text{fire}) = 10 + \gamma\cdot0 = 10. Wait: Q(S0,wait)=1+γmaxaQ(S0,a)Q^*(S_0,\text{wait}) = -1 + \gamma \max_a Q^*(S_0,a). Since fire gives 10, max=10\max = 10: Q(S0,wait)=1+0.910=8Q^*(S_0,\text{wait}) = -1 + 0.9\cdot10 = 8. Optimal policy: choose fire in S0S_0 (10 > 8). (2 + 2 + 1)


Question 5 (10 marks)

(a) Training error decreases monotonically as dd increases (more flexibility fits data). Validation error is U-shaped: high at d=1d=1 (underfit), minimum around d=3d=3 (good fit), rising again at d=9d=9 (overfit, high variance). (4)

  • d=1d=1: underfit; d=3d=3: good; d=9d=9: overfit.

(b) (3) Bias = error from wrong/too-simple model assumptions (systematic). Variance = sensitivity of the fit to particular training samples (fluctuation). d=1d=1: bias dominates; d=9d=9: variance dominates.

(c) (3) 5-fold on 200 samples: each fold uses 200/5=40200/5 = 40 for validation and 160160 for training. Prefer k-fold because every sample serves in validation exactly once, giving a lower-variance, more reliable performance estimate than one arbitrary split (and uses data efficiently).


[
  {"claim":"Ridge Q1 solution w0=4/3, w1=2/3","code":"X=Matrix([[1,1],[1,2],[1,3]]); y=Matrix([2,2,4]); R=Matrix([[0,0],[0,1]]); A=X.T*X+R; w=A.inv()*(X.T*y); result = (w[0]==Rational(4,3)) and (w[1]==Rational(2,3))"},
  {"claim":"Q2 dL/dw1 approx -0.30396","code":"import math; a1=math.tanh(0.5); z2=a1; yh=1/(1+math.exp(-z2)); g=(yh-1)*1*(1-a1**2)*1; result = abs(g-(-0.30396))<1e-3"},
  {"claim":"Q3 softmax diagonal weight approx 0.6698","code":"import math; e=math.exp(1/math.sqrt(2)); w=e/(e+1); result = abs(w-0.6698)<1e-3"},
  {"claim":"Q4 Bellman Q*(wait)=8, Q*(fire)=10","code":"g=Rational(9,10); qfire=10; qwait=-1+g*max(qfire,0); result = (qfire==10) and (qwait==8)"}
]