Level 5 — MasteryCalculus & Optimization Basics

Calculus & Optimization Basics

90 minutes60 marksprintable — key stays hidden on paper

Level 5 Mastery Examination

Time limit: 90 minutes Total marks: 60 Instructions: Answer all three questions. Show full working. You may write pseudocode/Python where requested. Use ...... for inline math.


Question 1 — Backpropagation from first principles (physics-flavoured energy descent) [22 marks]

A single "neuron" models a damped oscillator's steady-state error. Given input xRx\in\mathbb{R}, parameters w,bRw,b\in\mathbb{R}, define

z=wx+b,a=tanh(z),L=12(ay)2z = wx + b, \qquad a = \tanh(z), \qquad L = \tfrac12(a - y)^2

where yy is a target. This is one node of a computational graph.

(a) Using the multivariate chain rule, derive closed-form expressions for Lw\dfrac{\partial L}{\partial w}, Lb\dfrac{\partial L}{\partial b} in terms of x,z,a,yx, z, a, y. State the intermediate "upstream gradient" δ=L/z\delta = \partial L/\partial z explicitly. [6]

(b) Prove that tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z) starting from the definition tanh(z)=ezezez+ez\tanh(z) = \dfrac{e^z - e^{-z}}{e^z + e^{-z}}. [4]

(c) For x=2, y=0, w=0.5, b=0x=2,\ y=0,\ w=0.5,\ b=0, compute the numerical values of z,a,δ,L/w,L/bz, a, \delta, \partial L/\partial w, \partial L/\partial b. Give at least 4 significant figures. [6]

(d) One gradient-descent update is applied with learning rate η=1.0\eta=1.0. Compute the new w,bw,b. Then argue physically/analytically whether the loss decreased, and explain what an excessively large η\eta would do to convergence, linking it to the curvature (second derivative) of LL w.r.t. ww. [6]


Question 2 — Constrained optimization on a manifold [20 marks]

Consider minimizing f(x,y)=x2+2y2f(x,y) = x^2 + 2y^2 subject to the constraint g(x,y)=x+y3=0g(x,y) = x + y - 3 = 0.

(a) Set up the Lagrangian and derive the stationarity conditions. Solve for the constrained minimizer (x,y)(x^*,y^*) and multiplier λ\lambda^*. [7]

(b) Compute the Hessian of ff and verify the point found is a minimum along the constraint (use the bordered Hessian OR argue via convexity of ff restricted to the line). [6]

(c) Give the geometric interpretation: state the relationship between f\nabla f and g\nabla g at (x,y)(x^*,y^*) and verify it numerically. [4]

(d) Is ff convex on R2\mathbb{R}^2? Justify using its Hessian eigenvalues, and state why this guarantees the constrained solution is the global constrained minimum. [3]


Question 3 — Landscape analysis: critical points, saddles, and Taylor [18 marks]

Let f(x,y)=x33xy2f(x,y) = x^3 - 3xy^2 (the "monkey saddle").

(a) Find all critical points by solving f=0\nabla f = \mathbf{0}. [4]

(b) Compute the Hessian H(x,y)H(x,y) and evaluate it at the critical point(s). Classify the point using the eigenvalue / determinant test. Explain why the standard second-derivative test is inconclusive here and what that implies about the local geometry. [6]

(c) Write the second-order Taylor expansion of ff about (1,1)(1,1). Then, using only the gradient at (1,1)(1,1), give the unit direction of steepest ascent and the directional derivative magnitude along it. [6]

(d) A gradient-descent iterate sits exactly at the critical point of part (a) with zero gradient. Explain in one or two sentences why plain gradient descent stalls there and name one mechanism that escapes it. [2]

Answer keyMark scheme & solutions

Question 1

(a) [6 marks] Chain rule through the graph Laz(w,b)L\leftarrow a\leftarrow z\leftarrow (w,b).

  • La=(ay)\dfrac{\partial L}{\partial a} = (a-y) [1]
  • az=1a2\dfrac{\partial a}{\partial z} = 1-a^2 (since a=tanhza=\tanh z) [1]
  • Upstream gradient: δ=Lz=(ay)(1a2)\delta = \dfrac{\partial L}{\partial z} = (a-y)(1-a^2) [2]
  • zw=x, zb=1\dfrac{\partial z}{\partial w}=x,\ \dfrac{\partial z}{\partial b}=1, hence Lw=δx=(ay)(1a2)x,Lb=δ=(ay)(1a2).\frac{\partial L}{\partial w}=\delta\, x=(a-y)(1-a^2)x,\qquad \frac{\partial L}{\partial b}=\delta=(a-y)(1-a^2). [2]

(b) [4 marks] Let u=ezezu=e^z-e^{-z}, v=ez+ezv=e^z+e^{-z}, so tanhz=u/v\tanh z=u/v. Note u=vu'=v, v=uv'=u. [1] Quotient rule: tanhz=uvuvv2=v2u2v2=1u2v2=1tanh2z.\tanh'z=\dfrac{u'v-uv'}{v^2}=\dfrac{v^2-u^2}{v^2}=1-\dfrac{u^2}{v^2}=1-\tanh^2 z. [3] (Full marks for showing v2u2v^2-u^2 formation.)

(c) [6 marks]

  • z=wx+b=0.52+0=1z=wx+b=0.5\cdot 2+0=1 [1]
  • a=tanh(1)=0.76159a=\tanh(1)=0.76159 [1]
  • ay=0.76159a-y=0.76159; 1a2=10.58002=0.419981-a^2=1-0.58002=0.41998 [1]
  • δ=(ay)(1a2)=0.761590.41998=0.31985\delta=(a-y)(1-a^2)=0.76159\cdot0.41998=0.31985 [1]
  • L/w=δx=0.319852=0.63970\partial L/\partial w=\delta x=0.31985\cdot2=0.63970 [1]
  • L/b=δ=0.31985\partial L/\partial b=\delta=0.31985 [1]

(d) [6 marks] Update (η=1\eta=1): w=wηL/w=0.50.63970=0.13970w'=w-\eta\,\partial L/\partial w=0.5-0.63970=-0.13970; b=00.31985=0.31985b'=0-0.31985=-0.31985. [2] New z=0.1397020.31985=0.59925z'=-0.13970\cdot2-0.31985=-0.59925, a=tanh(0.59925)=0.5363a'=\tanh(-0.59925)=-0.5363, new L=12(a)2=0.1438L'=\tfrac12(a')^2=0.1438; old L=12(0.76159)2=0.2900L=\tfrac12(0.76159)^2=0.2900. Loss decreased. [2] Curvature/large-η\eta argument: LL as a function of ww has second derivative (local curvature) 2L/w2\partial^2 L/\partial w^2; gradient descent is stable when η<2/κ\eta<2/\kappa where κ\kappa is the local curvature. Too large η\eta overshoots the minimum, causing oscillation or divergence (the step jumps past the valley bottom and climbs the opposite wall). Full marks for linking overshoot to η>2/curvature\eta>2/\text{curvature}. [2]


Question 2

(a) [7 marks] L=x2+2y2λ(x+y3)\mathcal{L}=x^2+2y^2-\lambda(x+y-3). [1] Stationarity:

  • x:2xλ=0x=λ/2\partial_x: 2x-\lambda=0\Rightarrow x=\lambda/2 [1]
  • y:4yλ=0y=λ/4\partial_y: 4y-\lambda=0\Rightarrow y=\lambda/4 [1]
  • constraint: x+y=3x+y=3 [1] Sub: λ/2+λ/4=33λ/4=3λ=4\lambda/2+\lambda/4=3\Rightarrow 3\lambda/4=3\Rightarrow \lambda^*=4. [1] So x=2, y=1x^*=2,\ y^*=1. [2]

(b) [6 marks] Hf=(2004)H_f=\begin{pmatrix}2&0\\0&4\end{pmatrix}, positive definite (eigenvalues 2,4>02,4>0). [3] Restricting to line y=3xy=3-x: ϕ(x)=x2+2(3x)2=3x212x+18\phi(x)=x^2+2(3-x)^2=3x^2-12x+18, ϕ=6>0\phi''=6>0 ⇒ minimum; ϕ(x)=6x12=0x=2\phi'(x)=6x-12=0\Rightarrow x=2 confirms (2,1)(2,1). [3]

(c) [4 marks] At optimum f=λg\nabla f=\lambda\nabla g: f=(2x,4y)=(4,4)\nabla f=(2x,4y)=(4,4); g=(1,1)\nabla g=(1,1); indeed (4,4)=4(1,1)=λg(4,4)=4(1,1)=\lambda^*\nabla g. Gradients are parallel (level set of ff tangent to constraint line). [4]

(d) [3 marks] HfH_f has eigenvalues 2,4>02,4>0ff is (strictly) convex on R2\mathbb{R}^2. [2] A convex objective over an affine (hence convex) constraint set has a unique global minimum, so (2,1)(2,1) is the global constrained minimizer. [1]


Question 3

(a) [4 marks] f=(3x23y2, 6xy)\nabla f=(3x^2-3y^2,\ -6xy). Set =0=0: 6xy=0x=0-6xy=0\Rightarrow x=0 or y=0y=0.

  • If x=0x=0: 3y2=0y=0-3y^2=0\Rightarrow y=0.
  • If y=0y=0: 3x2=0x=03x^2=0\Rightarrow x=0. Unique critical point (0,0)(0,0). [4]

(b) [6 marks] H=(6x6y6y6x)H=\begin{pmatrix}6x & -6y\\ -6y & -6x\end{pmatrix}. At (0,0)(0,0): H=(0000)H=\begin{pmatrix}0&0\\0&0\end{pmatrix}. [3] Determinant =0=0, both eigenvalues 00 ⇒ second-derivative test inconclusive. [2] The quadratic term vanishes; behaviour is governed by the cubic terms, giving a degenerate "monkey saddle" (three ascending, three descending sectors) — neither max, min, nor ordinary saddle. [1]

(c) [6 marks] At (1,1)(1,1): f=13=2f=1-3=-2. f(1,1)=(33,6)=(0,6)\nabla f(1,1)=(3-3,\,-6)=(0,-6). [2] H(1,1)=(6666)H(1,1)=\begin{pmatrix}6&-6\\-6&-6\end{pmatrix}. Second-order Taylor with Δ=(x1,y1)\Delta=(x-1,y-1): f2+(0,6) ⁣ ⁣Δ+12Δ ⁣(6666) ⁣Δf\approx -2 + (0,-6)\!\cdot\!\Delta + \tfrac12\Delta^\top\!\begin{pmatrix}6&-6\\-6&-6\end{pmatrix}\!\Delta =26(y1)+3(x1)26(x1)(y1)3(y1)2.= -2 -6(y-1) +3(x-1)^2 -6(x-1)(y-1) -3(y-1)^2. [2] Steepest ascent direction = f/f=(0,6)/6=(0,1)\nabla f/\|\nabla f\|=(0,-6)/6=(0,-1); magnitude of directional derivative =f=6=\|\nabla f\|=6. [2]

(d) [2 marks] Gradient is exactly zero, so the update θθηf\theta\leftarrow\theta-\eta\nabla f leaves θ\theta unchanged — GD stalls at the flat critical point. Escape mechanisms: stochastic-gradient noise (SGD), momentum, or a random perturbation / second-order (Newton/saddle-free) step. [2]

[
  {"claim":"Q1c: delta=(a-y)(1-a^2)=0.31985 for z=1,y=0","code":"z=Rational(1); a=tanh(z); delta=(a-0)*(1-a**2); result = abs(float(delta)-0.31985)<1e-4"},
  {"claim":"Q1c: dL/dw = delta*x = 0.63970 with x=2","code":"z=Rational(1); a=tanh(z); delta=a*(1-a**2); dLdw=delta*2; result = abs(float(dLdw)-0.63970)<1e-4"},
  {"claim":"Q1d: loss decreased after eta=1 update (L'<L)","code":"a=tanh(1); delta=a*(1-a**2); w=0.5-1.0*float(delta)*2; b=0-1.0*float(delta); import sympy as sp; zp=w*2+b; ap=sp.tanh(zp); Lp=0.5*ap**2; L=0.5*a**2; result = float(Lp)<float(L)"},
  {"claim":"Q2a: constrained minimizer x*=2,y*=1,lambda*=4","code":"x,y,lam=symbols('x y lam'); sol=solve([2*x-lam,4*y-lam,x+y-3],[x,y,lam],dict=True)[0]; result = sol[x]==2 and sol[y]==1 and sol[lam]==4"},
  {"claim":"Q2c: grad f = lambda* grad g at optimum: (4,4)=4*(1,1)","code":"gf=Matrix([2*2,4*1]); gg=Matrix([1,1]); result = gf==4*gg"},
  {"claim":"Q3a: unique critical point (0,0)","code":"x,y=symbols('x y'); f=x**3-3*x*y**2; sol=solve([diff(f,x),diff(f,y)],[x,y],dict=True); result = sol==[{x:0,y:0}]"},
  {"claim":"Q3b: Hessian at origin is zero matrix (test inconclusive)","code":"x,y=symbols('x y'); f=x**3-3*x*y**2; H=hessian(f,(x,y)).subs({x:0,y:0}); result = H==zeros(2,2)"},
  {"claim":"Q3c: grad f at (1,1) = (0,-6), norm 6","code":"x,y=symbols('x y'); f=x**3-3*x*y**2; g=Matrix([diff(f,x),diff(f,y)]).subs({x:1,y:1}); result = g==Matrix([0,-6]) and g.norm()==6"}
]