Exercises · 89

Exercises

Every exercise in the portal, with hints and worked solutions. Filter by type or level.

Type
Filter by level
1ProofFrom Real numbers

Prove that 2\sqrt 2 is irrational.

Hint

Assume 2=p/q\sqrt 2 = p/q in lowest terms and look at parity.

Solution

If p2=2q2p^2 = 2q^2 then p2p^2 is even, so pp is even: p=2kp = 2k. Then 4k2=2q24k^2 = 2q^2, so q2=2k2q^2 = 2k^2 and qq is even too, contradicting that p/qp/q was in lowest terms.

2ComputingFrom Real numbers

In most languages 0.1 + 0.2 == 0.3 is false. Explain why in terms of real numbers.

Solution

0.10.1, 0.20.2 and 0.30.3 have infinite binary expansions, so each is rounded to the nearest double. The rounded 0.10.1 plus the rounded 0.20.2, rounded again, lands on a different double than the rounded 0.30.3. Compare with a tolerance: ∣a−b∣≤εmax⁡(∣a∣,∣b∣)|a - b| \le \varepsilon \max(|a|, |b|).

1ComputationFrom Absolute value

Solve ∣2x−3∣<5|2x - 3| < 5.

Solution

−5<2x−3<5  ⟺  −1<x<4-5 < 2x - 3 < 5 \iff -1 < x < 4, i.e. x∈(−1,4)x \in (-1, 4).

1ComputationFrom Functions

Find the domain and range of f(x)=ln⁡(4−x2)f(x) = \ln(4 - x^2).

Solution

Domain: 4−x2>0  ⟺  x∈(−2,2)4 - x^2 > 0 \iff x \in (-2, 2). On it 4−x2∈(0,4]4 - x^2 \in (0, 4], so the range is (−∞,ln⁡4](-\infty, \ln 4].

2ComputingFrom Functions

Is a function that reads the system clock a function in the mathematical sense? What is missing?

Solution

No: the same (empty) input gives different outputs. It becomes a mathematical function if the time is made an explicit input, f(t)f(t) — which is exactly what makes code testable.

1ComputingFrom Polynomial functions

How many multiplications does evaluating p(x)=3x4−2x3+x−7p(x) = 3x^4 - 2x^3 + x - 7 take naively, and with Horner?

Solution

Naively 4+3+1=84 + 3 + 1 = 8 (computing each power from scratch). Horner: ((3x−2)x+0)x+1)x−7((3x - 2)x + 0)x + 1)x - 7, 4 multiplications.

A dataset doubles every 18 months. How long until it is 100 times larger?

Solution

2t/1.5=100⇒t=1.5log⁡2100≈1.5⋅6.64≈102^{t/1.5} = 100 \Rightarrow t = 1.5 \log_2 100 \approx 1.5 \cdot 6.64 \approx 10 years.

1ComputingFrom Logarithmic functions

Why do ML libraries compute log⁡∑iezi\log \sum_i e^{z_i} as m+log⁡∑iezi−mm + \log\sum_i e^{z_i - m} with m=max⁡izim = \max_i z_i?

Solution

Both are equal since ezi=emezi−me^{z_i} = e^m e^{z_i - m}. But e1000e^{1000} overflows a double, while every ezi−m≤1e^{z_i - m} \le 1 and at least one equals 1, so the sum is in [1,n][1, n] and safe.

Prove from the definition that 3n+1n→3\frac{3n + 1}{n} \to 3.

Solution

∣3n+1n−3∣=1n<ε|\frac{3n+1}{n} - 3| = \frac1n < \varepsilon whenever n>1/εn > 1/\varepsilon; take N=⌊1/ε⌋+1N = \lfloor 1/\varepsilon \rfloor + 1.

A training loss goes 2.0,1.0,0.5,0.25,…2.0, 1.0, 0.5, 0.25, \dots (halving each epoch). After how many epochs is it below 10−310^{-3}? What kind of convergence is this?

Solution

2⋅2−k<10−3  ⟺  k>log⁡22000≈10.972 \cdot 2^{-k} < 10^{-3} \iff k > \log_2 2000 \approx 10.97, so 11 epochs. The error shrinks by a constant factor: linear (geometric) convergence.

1ComputationFrom Limit of a function

Compute lim⁡x→01+x−1x\lim_{x\to 0}\frac{\sqrt{1 + x} - 1}{x}.

Hint

Multiply and divide by 1+x+1\sqrt{1+x} + 1.

Solution

(1+x)−1x(1+x+1)=11+x+1→12\frac{(1+x) - 1}{x(\sqrt{1+x} + 1)} = \frac{1}{\sqrt{1+x}+1} \to \frac12.

2GraphicalFrom Limit of a function

Why does lim⁡x→0sin⁡(1/x)\lim_{x\to 0}\sin(1/x) not exist? Describe the graph.

Solution

The graph oscillates between −1-1 and 11 infinitely often near 0. Along xn=12πnx_n = \frac{1}{2\pi n} the values are 00; along xn=12πn+π/2x_n = \frac{1}{2\pi n + \pi/2} they are 11. Two sequences, two different limits.

1ComputationFrom Continuity

For which kk is f(x)=kx+1f(x) = kx + 1 for x<1x < 1, f(x)=x2+3f(x) = x^2 + 3 for x≥1x \ge 1 continuous?

Solution

Left limit k+1k + 1, value f(1)=4f(1) = 4. Continuity requires k+1=4k + 1 = 4, so k=3k = 3.

2AIFrom Continuity

Why can't you train a network whose activation is the step function HH with gradient descent?

Solution

HH is constant except at 0, so its derivative is 0 almost everywhere (and undefined at 0). Every gradient flowing through it is zero: the weights never move.

1ComputationFrom Derivative

Using the definition, compute f′(x)f'(x) for f(x)=1xf(x) = \frac1x.

Solution

1h(1x+h−1x)=−hh x(x+h)=−1x(x+h)→−1x2\frac{1}{h}\left(\frac{1}{x+h} - \frac1x\right) = \frac{-h}{h\,x(x+h)} = \frac{-1}{x(x+h)} \to -\frac{1}{x^2}.

2GraphicalFrom Derivative

Sketch f(x)=x3−3xf(x) = x^3 - 3x and, below it, f′f'. Where is f′f' zero, positive, negative?

Solution

f′(x)=3x2−3f'(x) = 3x^2 - 3: zero at x=±1x = \pm1 (a local max at −1-1, a local min at 11), negative on (−1,1)(-1,1) where ff decreases, positive outside.

3AIFrom Derivative

A one-parameter model has loss L(w)=(w−3)2+1L(w) = (w - 3)^2 + 1. Starting at w0=0w_0 = 0 with learning rate η=0.1\eta = 0.1, compute two steps of gradient descent.

Solution

L′(w)=2(w−3)L'(w) = 2(w - 3). w1=0−0.1⋅(−6)=0.6w_1 = 0 - 0.1\cdot(-6) = 0.6; w2=0.6−0.1⋅(−4.8)=1.08w_2 = 0.6 - 0.1\cdot(-4.8) = 1.08. Each step closes 20% of the gap to the minimum w=3w = 3.

1ComputationFrom Differentiation rules

Differentiate f(x)=x2ex1+xf(x) = \frac{x^2 e^x}{1 + x}.

Solution

f′(x)=(2x+x2)ex(1+x)−x2ex(1+x)2=xex(x2+2x+2)(1+x)2f'(x) = \frac{(2x + x^2)e^x(1 + x) - x^2 e^x}{(1+x)^2} = \frac{x e^x (x^2 + 2x + 2)}{(1 + x)^2}.

Prove that σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)(1 - \sigma(x)) for σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}). What is the maximum of σ′\sigma'?

Solution

σ′(x)=e−x(1+e−x)2=11+e−x⋅e−x1+e−x=σ(1−σ)\sigma'(x) = \frac{e^{-x}}{(1 + e^{-x})^2} = \frac{1}{1 + e^{-x}}\cdot\frac{e^{-x}}{1 + e^{-x}} = \sigma(1 - \sigma). Since s(1−s)≤14s(1 - s) \le \frac14, the maximum is 14\frac14 at x=0x = 0 — so each sigmoid layer shrinks gradients by at least a factor 4, one cause of vanishing gradients.

1ComputationFrom Chain rule

Differentiate h(x)=ln⁡(1+e3x)h(x) = \ln\big(1 + e^{3x}\big).

Solution

h′(x)=11+e3x⋅3e3x=3 σ(3x)h'(x) = \frac{1}{1 + e^{3x}}\cdot 3e^{3x} = 3\,\sigma(3x) — the derivative of softplus is a sigmoid.

2AIFrom Chain rule

A 20-layer network uses sigmoid activations. Bound the factor by which the gradient can shrink when it goes through the 20 activations, ignoring the weights.

Solution

Each σ′≤14\sigma' \le \frac14, so the product is at most 4−20≈9⋅10−134^{-20} \approx 9 \cdot 10^{-13}: vanishing gradients. ReLU (derivative 1\text{derivative } 1 when active) and residual connections avoid it.

Find and classify the critical points of f(x)=x4−4x3f(x) = x^4 - 4x^3.

Solution

f′(x)=4x2(x−3)f'(x) = 4x^2(x - 3): critical points 0 and 3. f′(x)=12x2−24xf'(x) = 12x^2 - 24x; f′(3)=36>0f'(3) = 36 > 0 → minimum. At 0, f′=0f' = 0 and f′f' does not change sign (negative on both sides): not an extremum.

A server costs c(n)=100/n+4nc(n) = 100/n + 4n (latency penalty plus hardware) with nn instances. Which nn minimizes cost?

Solution

c′(n)=−100/n2+4=0⇒n=5c'(n) = -100/n^2 + 4 = 0 \Rightarrow n = 5; c′(n)=200/n3>0c'(n) = 200/n^3 > 0, so it is a minimum, c(5)=40c(5) = 40.

1ComputationFrom Bisection method

How many bisection steps on [1,2][1, 2] guarantee 2\sqrt 2 to within 10−610^{-6}?

Solution

2−(k+1)<10−6  ⟺  k+1>6log⁡210≈19.92^{-(k+1)} < 10^{-6} \iff k + 1 > 6\log_2 10 \approx 19.9: k=19k = 19 steps.

1GraphicalFrom Riemann sums

In the demo, compare the left and midpoint sums for sin⁡x\sin x on [0,π][0,\pi] with n=10n = 10. Why is the midpoint so much better?

Solution

On each strip the midpoint rectangle over- and under-estimates by nearly equal amounts on the two halves, so the first-order errors cancel and only a curvature term O(Δx3)O(\Delta x^3) per strip survives: total O(1/n2)O(1/n^2) instead of O(1/n)O(1/n).

1ComputationFrom Definite integral

Compute ∫02(3x2−2x) dx\int_0^2 (3x^2 - 2x)\,\dd x and interpret the sign.

Solution

[x3−x2]02=8−4=4[x^3 - x^2]_0^2 = 8 - 4 = 4. Positive: the area above the axis (for x>2/3x > 2/3) exceeds the small negative part on (0,2/3)(0, 2/3).

2ComputingFrom Definite integral

Estimate π\pi with an integral and random numbers. How many samples for 3 correct decimals?

Solution

π=4∫011−x2 dx≈4N∑1−ui2\pi = 4\int_0^1\sqrt{1 - x^2}\,\dd x \approx \frac4N\sum\sqrt{1 - u_i^2} with uiu_i uniform. The standard error is about 0.9/N0.9/\sqrt N; for ±0.0005\pm 0.0005 you need N≈3⋅106N \approx 3\cdot10^6. Monte Carlo is simple but slow.

1ComputationFrom Numerical series

Does ∑k≥1kk2+1\sum_{k\ge1}\frac{k}{k^2 + 1} converge?

Solution

No: kk2+1≥12k\frac{k}{k^2+1} \ge \frac{1}{2k} for k≥1k \ge 1, and ∑12k\sum\frac1{2k} diverges (comparison with the harmonic series).

2ComputingFrom Numerical series

Quicksort makes about 2(n+1)Hn−4n2(n+1)H_n - 4n comparisons on average. Estimate it for n=106n = 10^6.

Solution

H106≈ln⁡106+0.577≈14.39H_{10^6} \approx \ln 10^6 + 0.577 \approx 14.39, so about 2⋅106⋅14.39−4⋅106≈2.5⋅1072\cdot10^6\cdot14.39 - 4\cdot10^6 \approx 2.5\cdot10^7 comparisons (≈1.39 nlog⁡2n\approx 1.39\,n\log_2 n).

An agent gets reward 1 every step forever, with γ=0.99\gamma = 0.99. What is the return? What is the "effective horizon"?

Solution

∑0.99k=10.01=100\sum 0.99^k = \frac{1}{0.01} = 100. Rewards beyond about 11−γ=100\frac{1}{1-\gamma} = 100 steps contribute little: the effective horizon is ~100 steps.

1ComputationFrom Complex numbers

Compute (1+i)8(1+i)^8 using the polar form.

Solution

1+i=2 eiπ/41 + i = \sqrt 2\, e^{i\pi/4}, so (1+i)8=24e2πi=16(1+i)^8 = 2^4 e^{2\pi i} = 16.

2GraphicalFrom Complex numbers

Where in the plane are the solutions of z6=1z^6 = 1? What shape do they form?

Solution

At e2πik/6e^{2\pi i k/6}, k=0,…,5k = 0,\dots,5: the vertices of a regular hexagon inscribed in the unit circle.

Order by growth: n3/2n^{3/2}, 2n2^{\sqrt n}, nlog⁡2nn \log^2 n, (log⁡n)10(\log n)^{10}, nlog⁡nn^{\log n}.

Solution

(log⁡n)10≪nlog⁡2n≪n3/2≪nlog⁡n≪2n(\log n)^{10} \ll n\log^2 n \ll n^{3/2} \ll n^{\log n} \ll 2^{\sqrt n} (compare logarithms: 10log⁡log⁡n10\log\log n, log⁡n\log n, 1.5log⁡n1.5\log n, log⁡2n\log^2 n, n\sqrt n).

An algorithm takes 1 s for n=1000n = 1000. Estimate the time for n=106n = 10^6 if it is Θ(n2)\Theta(n^2), and if it is Θ(nlog⁡n)\Theta(n\log n).

Solution

Θ(n2)\Theta(n^2): factor 10610^6 → about 11.6 days. Θ(nlog⁡n)\Theta(n\log n): factor 1000⋅log⁡106log⁡103=20001000 \cdot \frac{\log 10^6}{\log 10^3} = 2000 → about 33 minutes.

Estimate 4.1\sqrt{4.1} with the differential of x\sqrt x at x=4x = 4.

Solution

4.1≈2+124⋅0.1=2.025\sqrt{4.1} \approx 2 + \frac{1}{2\sqrt4}\cdot 0.1 = 2.025 (true value 2.02485…2.02485…).

Use the MVT to prove ∣sin⁡x−sin⁡y∣≤∣x−y∣|\sin x - \sin y| \le |x - y| for all real x,yx, y.

Solution

sin⁡x−sin⁡y=cos⁡(c) (x−y)\sin x - \sin y = \cos(c)\,(x - y) for some cc, and ∣cos⁡c∣≤1|\cos c| \le 1.

2AppliedFrom Mean value theorem

A car passes two cameras 10 km apart, 5 minutes apart. Prove it exceeded 110 km/h at some moment.

Solution

Average speed =10/(5/60)=120= 10 / (5/60) = 120 km/h. By the MVT, at some instant s′(t)=120>110s'(t) = 120 > 110.

Compute ddx∫0x2e−t2 dt\frac{\dd}{\dd x}\int_0^{x^2} e^{-t^2}\,\dd t.

Solution

By the FTC and the chain rule: e−x4⋅2xe^{-x^4}\cdot 2x.

A sensor reports velocity every 0.1 s. How do you estimate position, and which theorem justifies it?

Solution

Position is s(0)+∫0Tvs(0) + \int_0^T v (FTC). With samples, approximate the integral by a sum, e.g. the trapezoidal rule ∑vk+vk+12 0.1\sum \frac{v_k + v_{k+1}}{2}\,0.1. Errors accumulate (drift), which is why IMUs are fused with GPS.

1ComputationFrom L'Hôpital's rule

Compute lim⁡x→0x−sin⁡xx3\lim_{x\to 0}\frac{x - \sin x}{x^3}.

Solution

Three applications: 1−cos⁡x3x2→sin⁡x6x→cos⁡x6→16\frac{1 - \cos x}{3x^2} \to \frac{\sin x}{6x} \to \frac{\cos x}{6} \to \frac16.

Show that f(x)=ln⁡(1+ex)f(x) = \ln(1 + e^x) (softplus) is convex.

Solution

f′(x)=σ(x)f'(x) = \sigma(x) and f′(x)=σ(x)(1−σ(x))>0f'(x) = \sigma(x)(1 - \sigma(x)) > 0.

Is the loss of a 1-hidden-layer network convex in its weights? Hint: swap two hidden neurons.

Solution

No. Swapping two hidden neurons (and their outgoing weights) gives a different weight vector with the same loss. If θ1≠θ2\theta_1 \ne \theta_2 are both minima, convexity would make their midpoint at least as good — but the midpoint averages the two neurons into identical ones, generally worse. Symmetric minima rule out convexity.

1ComputationFrom Newton's method

Write the Newton iteration for f(x)=x3−2x−5f(x) = x^3 - 2x - 5 and do two steps from x0=2x_0 = 2.

Solution

xk+1=xk−xk3−2xk−53xk2−2x_{k+1} = x_k - \frac{x_k^3 - 2x_k - 5}{3x_k^2 - 2}. x1=2−−110=2.1x_1 = 2 - \frac{-1}{10} = 2.1, x2=2.1−0.06111.23≈2.094568x_2 = 2.1 - \frac{0.061}{11.23} \approx 2.094568 (root 2.09455152.0945515).

2GraphicalFrom Newton's method

Use the demo with f(x)=x3−2x+2f(x) = x^3 - 2x + 2 and x0=0x_0 = 0. What happens, and why?

Solution

x1=0−2−2=1x_1 = 0 - \frac{2}{-2} = 1, x2=1−11=0x_2 = 1 - \frac{1}{1} = 0: a 2-cycle. The tangents bounce between 0 and 1 forever; the real root (≈−1.77\approx -1.77) is in another basin.

3ComputingFrom Newton's method

Derive the Newton iteration that computes 1/a1/a using only multiplications and subtractions.

Solution

Take f(x)=1x−af(x) = \frac1x - a: xk+1=xk−1/xk−a−1/xk2=xk(2−axk)x_{k+1} = x_k - \frac{1/x_k - a}{-1/x_k^2} = x_k(2 - a x_k). No division needed — this is how many processors divide.

1ComputationFrom Taylor polynomial

Find the Maclaurin polynomial of order 4 of cos⁡x\cos x and use it to estimate cos⁡0.5\cos 0.5.

Solution

T4=1−x22+x424T_4 = 1 - \frac{x^2}{2} + \frac{x^4}{24}; T4(0.5)=0.87760416‾T_4(0.5) = 0.8776041\overline{6} vs cos⁡0.5=0.8775826\cos 0.5 = 0.8775826 (error 2⋅10−52 \cdot 10^{-5}).

Use the second-order Taylor model of L(w)L(w) to derive the step that minimizes it. Which method is this?

Solution

L(w+h)≈L+L′h+12L′′h2L(w + h) \approx L + L'h + \frac12 L''h^2; setting the derivative in hh to zero gives h=−L′/L′′h = -L'/L''. That is Newton's method for optimization.

How many terms of the Maclaurin series of exe^x guarantee ee (at x=1x = 1) to within 10−1010^{-10}?

Solution

∣Rn∣≤e(n+1)!<3(n+1)!|R_n| \le \frac{e}{(n+1)!} < \frac{3}{(n+1)!}. (n+1)!>3⋅1010(n+1)! > 3 \cdot 10^{10} needs n+1=14n + 1 = 14 (14!≈8.7⋅101014! \approx 8.7 \cdot 10^{10}), so n=13n = 13.

Derive the Maclaurin series of arctan⁡x\arctan x from 11+x2\frac{1}{1 + x^2} and use it to write a series for π\pi.

Solution

11+x2=∑(−1)kx2k\frac{1}{1+x^2} = \sum (-1)^k x^{2k}; integrate: arctan⁡x=∑(−1)kx2k+12k+1\arctan x = \sum\frac{(-1)^k x^{2k+1}}{2k+1}. At x=1x = 1: π4=1−13+15−…\frac\pi4 = 1 - \frac13 + \frac15 - \dots (Leibniz; very slow).

1ComputationFrom Partial derivatives

Compute all first and second partial derivatives of f(x,y)=exyf(x, y) = e^{xy} and check that fxy=fyxf_{xy} = f_{yx}.

Solution

fx=yexyf_x = ye^{xy}, fy=xexyf_y = xe^{xy}, fxx=y2exyf_{xx} = y^2e^{xy}, fyy=x2exyf_{yy} = x^2e^{xy}, fxy=fyx=(1+xy)exyf_{xy} = f_{yx} = (1 + xy)e^{xy}.

For y^=σ(w1x1+w2x2+b)\hat y = \sigma(w_1x_1 + w_2x_2 + b) and L=−(yln⁡y^+(1−y)ln⁡(1−y^))L = -\big(y\ln\hat y + (1-y)\ln(1-\hat y)\big), show that ∂L/∂w1=(y^−y) x1\partial L/\partial w_1 = (\hat y - y)\,x_1.

Solution

∂L∂y^=y^−yy^(1−y^)\frac{\partial L}{\partial\hat y} = \frac{\hat y - y}{\hat y(1 - \hat y)}, ∂y^∂z=y^(1−y^)\frac{\partial\hat y}{\partial z} = \hat y(1 - \hat y), ∂z∂w1=x1\frac{\partial z}{\partial w_1} = x_1. The product simplifies to (y^−y)x1(\hat y - y)x_1: sigmoid and cross-entropy cancel beautifully.

1ComputationFrom Gradient

Find ∇f\nabla f for f(x,y,z)=x2y+yz3f(x,y,z) = x^2y + yz^3 at (1,2,−1)(1, 2, -1) and the rate of increase in the direction (1,1,1)/3(1,1,1)/\sqrt3.

Solution

∇f=(2xy,x2+z3,3yz2)=(4,0,6)\nabla f = (2xy, x^2 + z^3, 3yz^2) = (4, 0, 6). Duf=(4+0+6)/3=10/3≈5.77D_u f = (4 + 0 + 6)/\sqrt3 = 10/\sqrt3 \approx 5.77; the maximum possible is ∥∇f∥=52≈7.21\norm{\nabla f} = \sqrt{52} \approx 7.21.

2GraphicalFrom Gradient

Draw the level curves of f(x,y)=x2+4y2f(x,y) = x^2 + 4y^2 and the gradient at (2,1)(2, 1). Why does the arrow not point to the origin?

Solution

Level curves are ellipses, wider in xx. The gradient (4,8)(4, 8) is perpendicular to the ellipse through (2,1)(2,1), which is not the radial direction (2,1)(2, 1) because the ellipse is not a circle.

3AIFrom Gradient

Why does reverse-mode AD compute ∇L∈ℝ109\nabla L \in \R^{10^9} in roughly the time of 3–5 evaluations of LL, while finite differences would need 10910^9?

Solution

Finite differences perturb one parameter at a time. Reverse mode runs the program once forward, then once backward propagating ∂L/∂(intermediate)\partial L/\partial(\text{intermediate}); each intermediate is visited once, so the cost is a constant multiple of the forward pass, independent of the number of parameters (one scalar output).

1ComputationFrom Lagrange multipliers

Maximize f(x,y)=xyf(x,y) = xy subject to x+y=10x + y = 10 with a Lagrange multiplier.

Solution

(y,x)=λ(1,1)(y, x) = \lambda(1, 1) gives x=y=λx = y = \lambda, and x+y=10x + y = 10 gives x=y=5x = y = 5, f=25f = 25, λ=5\lambda = 5.

Show that the distribution maximizing entropy with a fixed mean energy ∑ipiEi=Eˉ\sum_i p_i E_i = \bar E has the form pi∝e−βEip_i \propto e^{-\beta E_i}.

Solution

∂pi[−∑pln⁡p−λ(∑p−1)−β(∑pE−Eˉ)]=−ln⁡pi−1−λ−βEi=0\partial_{p_i}\big[-\sum p\ln p - \lambda(\sum p - 1) - \beta(\sum pE - \bar E)\big] = -\ln p_i - 1 - \lambda - \beta E_i = 0, so pi=e−1−λe−βEip_i = e^{-1-\lambda}e^{-\beta E_i}: a softmax of −βE-\beta E.

1ComputationFrom Multivariable chain rule

If w=f(x,y)w = f(x, y) with x=rcos⁡θx = r\cos\theta, y=rsin⁡θy = r\sin\theta, express ∂w/∂r\partial w/\partial r and ∂w/∂θ\partial w/\partial\theta.

Solution

wr=fxcos⁡θ+fysin⁡θw_r = f_x\cos\theta + f_y\sin\theta, wθ=−fxrsin⁡θ+fyrcos⁡θw_\theta = -f_x r\sin\theta + f_y r\cos\theta.

A network computes L=ℓ(W2 σ(W1x))L = \ell(W_2\,\sigma(W_1x)). Write ∂L/∂W1\partial L/\partial W_1 using the chain rule and say which factors are reused from ∂L/∂W2\partial L/\partial W_2.

Solution

Let z1=W1xz_1 = W_1x, a=σ(z1)a = \sigma(z_1), z2=W2az_2 = W_2a. With g=∂ℓ/∂z2g = \partial\ell/\partial z_2: ∂L/∂W2=g a𝖳\partial L/\partial W_2 = g\,a^{\mathsf T} and ∂L/∂W1=((W2𝖳g)⊙σ′(z1))x𝖳\partial L/\partial W_1 = \big((W_2^{\mathsf T}g)\odot\sigma'(z_1)\big)x^{\mathsf T}. The upstream gradient gg is computed once and reused: that sharing is what makes backprop cheap.

1ComputationFrom Jacobian matrix

Compute the Jacobian of F(x,y)=(x2−y2, 2xy)F(x, y) = (x^2 - y^2,\ 2xy) and its determinant. Where is FF not locally invertible?

Solution

J=(2x−2y2y2x)J = \begin{pmatrix}2x & -2y\\ 2y & 2x\end{pmatrix}, det⁡J=4(x2+y2)\det J = 4(x^2 + y^2). Only at the origin (FF is z↦z2z \mapsto z^2 in complex form).

2AppliedFrom Jacobian matrix

A 2-link planar arm with lengths ℓ1,ℓ2\ell_1, \ell_2 has hand position p=(ℓ1cos⁡q1+ℓ2cos⁡(q1+q2), ℓ1sin⁡q1+ℓ2sin⁡(q1+q2))p = (\ell_1\cos q_1 + \ell_2\cos(q_1+q_2),\ \ell_1\sin q_1 + \ell_2\sin(q_1+q_2)). When is det⁡J=0\det J = 0?

Solution

det⁡J=ℓ1ℓ2sin⁡q2\det J = \ell_1\ell_2\sin q_2: zero when q2=0q_2 = 0 or π\pi — arm fully stretched or folded. There the hand cannot move radially.

1ComputationFrom Hessian matrix

Classify the critical points of f(x,y)=x3−3x+y2f(x, y) = x^3 - 3x + y^2.

Solution

∇f=(3x2−3,2y)=0\nabla f = (3x^2 - 3, 2y) = 0 at (±1,0)(\pm1, 0). H=diag⁡(6x,2)H = \operatorname{diag}(6x, 2): at (1,0)(1,0) positive definite → minimum; at (−1,0)(-1, 0) indefinite → saddle.

On f(x,y)=12(x2+100y2)f(x,y) = \frac12(x^2 + 100y^2), what is the largest learning rate for which gradient descent converges? How many steps to reduce the xx-error by 10−310^{-3} at that rate?

Solution

H=diag⁡(1,100)H = \operatorname{diag}(1, 100), so η<2/100=0.02\eta < 2/100 = 0.02. Along xx each step multiplies the error by 1−η≈0.981 - \eta \approx 0.98: 0.98k=10−3⇒k≈3420.98^k = 10^{-3} \Rightarrow k \approx 342. Condition number 100 makes it slow.

1ComputationFrom Divergence

Compute ∇⋅F\nabla\cdot F for F=(x2y, −xy2, z)F = (x^2y,\ -xy^2,\ z).

Solution

2xy−2xy+1=12xy - 2xy + 1 = 1.

Rewrite y′′+3y′+2y=sin⁡ty'' + 3y' + 2y = \sin t as a first-order system.

Solution

With y1=yy_1 = y, y2=y′y_2 = y': y1′=y2y_1' = y_2, y2′=sin⁡t−2y1−3y2y_2' = \sin t - 2y_1 - 3y_2.

A server room at 35 °C cools to 30 °C in 10 min with the AC at 20 °C. When does it reach 22 °C?

Solution

T−20=15e−ktT - 20 = 15e^{-kt}; 10=15e−10k⇒k=ln⁡(1.5)/1010 = 15e^{-10k} \Rightarrow k = \ln(1.5)/10. 2=15e−kt⇒t=10ln⁡7.5/ln⁡1.5≈49.72 = 15e^{-kt} \Rightarrow t = 10\ln 7.5/\ln 1.5 \approx 49.7 min.

1ComputationFrom Euler's method

Do three Euler steps with h=0.5h = 0.5 for y′=t−yy' = t - y, y(0)=1y(0) = 1.

Solution

y1=1+0.5(0−1)=0.5y_1 = 1 + 0.5(0 - 1) = 0.5; y2=0.5+0.5(0.5−0.5)=0.5y_2 = 0.5 + 0.5(0.5 - 0.5) = 0.5; y3=0.5+0.5(1−0.5)=0.75y_3 = 0.5 + 0.5(1 - 0.5) = 0.75 (exact y(1.5)=1.5−1+2e−1.5≈0.946y(1.5) = 1.5 - 1 + 2e^{-1.5} \approx 0.946).

2ComputingFrom Euler's method

Simulate a planet on a circular orbit with explicit Euler. What happens to its energy? How does semi-implicit Euler fix it?

Solution

Each explicit step moves along the tangent, outside the circle: the radius and energy grow every step and the planet spirals out. Semi-implicit Euler is symplectic: it preserves a slightly perturbed energy, so the orbit stays closed for very long times.

Explain why a learning rate above 2/λmax⁡(H)2/\lambda_{\max}(H) makes training diverge, using Euler's method.

Solution

Near a minimum ∇L≈H(θ−θ∗)\nabla L \approx H(\theta - \theta^\ast), so GD is Euler on e˙=−He\dot e = -He. Along the top eigenvector, ek+1=(1−ηλmax⁡)eke_{k+1} = (1 - \eta\lambda_{\max})e_k, which grows when ∣1−ηλmax⁡∣>1|1 - \eta\lambda_{\max}| > 1, i.e. η>2/λmax⁡\eta > 2/\lambda_{\max}.

For which cc is p(x)=c x(1−x)p(x) = c\,x(1 - x) on [0,1][0,1] a density? Compute P(X>0.5)P(X > 0.5).

Solution

∫01x(1−x) dx=16\int_0^1 x(1-x)\,\dd x = \frac16, so c=6c = 6. By symmetry P(X>0.5)=0.5P(X > 0.5) = 0.5.

1ComputationFrom Expectation

Compute 𝔼[X]\E[X] for the exponential density p(x)=λe−λxp(x) = \lambda e^{-\lambda x}, x≥0x \ge 0.

Solution

By parts: ∫0∞xλe−λx dx=[−xe−λx]0∞+∫0∞e−λx dx=1λ\int_0^\infty x\lambda e^{-\lambda x}\,\dd x = \big[-xe^{-\lambda x}\big]_0^\infty + \int_0^\infty e^{-\lambda x}\,\dd x = \frac1\lambda.

2AIFrom Expectation

Why is the gradient of a random mini-batch loss an unbiased estimate of the full gradient? What assumption is needed?

Solution

If the batch is drawn uniformly at random, 𝔼[1∣B∣∑i∈B∇ℓi]=1N∑i∇ℓi\E[\frac1{|B|}\sum_{i\in B}\nabla\ell_i] = \frac1N\sum_i\nabla\ell_i by linearity of expectation (and exchanging gradient and expectation, which needs mild smoothness). Non-random batches (e.g. sorted data) break it.

1ComputingFrom Runge–Kutta methods

RK4 costs 4 evaluations of ff per step, Euler 1. For error 10−610^{-6} on [0,1][0,1], roughly how many evaluations does each need if the error constants are about 1?

Solution

Euler: h≈10−6h \approx 10^{-6} → 10610^6 evaluations. RK4: h4≈10−6h^4 \approx 10^{-6} → h≈0.03h \approx 0.03, about 32 steps → ~130 evaluations. Higher order wins by orders of magnitude.

1ComputationFrom Equilibria and stability

Find the equilibria of x˙=x(1−x)\dot x = x(1 - x) and classify them.

Solution

x=0x = 0 and x=1x = 1. f′(x)=1−2xf'(x) = 1 - 2x: f′(0)=1>0f'(0) = 1 > 0 unstable, f′(1)=−1<0f'(1) = -1 < 0 stable. Every positive population tends to the carrying capacity.

1ComputationFrom Fourier series

Compute the Fourier coefficients bnb_n of the odd square wave f=1f = 1 on (0,π)(0, \pi), −1-1 on (−π,0)(-\pi, 0).

Solution

bn=2π∫0πsin⁡nt dt=2nπ(1−(−1)n)b_n = \frac2\pi\int_0^\pi\sin nt\,\dd t = \frac{2}{n\pi}(1 - (-1)^n): 4nπ\frac{4}{n\pi} for odd nn, 0 for even nn.

1ComputingFrom Fourier transform

Convolving two signals of length N=106N = 10^6 directly costs ≈N2\approx N^2 operations. Estimate the cost via FFT.

Solution

Three FFTs of size ~2N2N plus a pointwise product: about 3⋅2Nlog⁡2(2N)≈1.3⋅1083\cdot 2N\log_2(2N) \approx 1.3\cdot10^8 vs 101210^{12} — roughly 8000 times faster.

2GraphicalFrom Fourier transform

A signal is a short click (a narrow pulse). What does its spectrum look like? And a long pure tone?

Solution

A narrow pulse has a very wide, flat spectrum (all frequencies); a long pure tone has a narrow peak. Time width × frequency width is bounded below.

1ComputationFrom Convolution

Convolve the sequence [1,2,3][1, 2, 3] with the kernel [1,1]/2[1, 1]/2.

Solution

[0.5, 1.5, 2.5, 1.5][0.5,\ 1.5,\ 2.5,\ 1.5]: a moving average (with the edges padded by zeros).

Find the MLE of λ\lambda for exponential data x1,…,xNx_1, \dots, x_N.

Solution

ℓ(λ)=Nln⁡λ−λ∑xi\ell(\lambda) = N\ln\lambda - \lambda\sum x_i; ℓ′(λ)=N/λ−∑xi=0⇒λ^=1/xˉ\ell'(\lambda) = N/\lambda - \sum x_i = 0 \Rightarrow \hat\lambda = 1/\bar x.

Show that if the noise is Laplace, p(ε)∝e−∣ε∣/bp(\varepsilon) \propto e^{-|\varepsilon|/b}, maximum likelihood regression minimizes the absolute error.

Solution

−log⁡p(y∣x)=∣y−fθ(x)∣/b+const-\log p(y\mid x) = |y - f_\theta(x)|/b + \text{const}; summing over the data, maximizing likelihood is minimizing ∑∣yi−fθ(xi)∣\sum|y_i - f_\theta(x_i)|.

A system has λ=0.5\lambda = 0.5 per day and initial error 10−610^{-6}. When does the error reach 1? And if the initial error is 10−1210^{-12}?

Solution

t=ln⁡(106)/0.5≈27.6t = \ln(10^6)/0.5 \approx 27.6 days. With 10−1210^{-12}: 55.355.3 days. A million times better data only doubles the horizon.

Why does evaluating f′(1)f'(1) for f=exf = e^x with (f(1+h)−f(1))/h(f(1+h) - f(1))/h get worse when hh goes below about 10−810^{-8}?

Solution

The truncation error is ≈h2f′′\approx \frac{h}{2}f'' but the rounding error of the numerator is ≈εf/h\approx \varepsilon f/h. Their sum is minimized at h≈ε≈10−8h \approx \sqrt\varepsilon \approx 10^{-8}; smaller hh amplifies rounding.

A classifier outputs p^=0.01\hat p = 0.01 for the true class. What is its cross-entropy? And with p^=0.9\hat p = 0.9?

Solution

−ln⁡0.01≈4.6-\ln 0.01 \approx 4.6 vs −ln⁡0.9≈0.105-\ln 0.9 \approx 0.105. Confidently wrong predictions are punished ~44 times more.

For L(θ)=a2θ2L(\theta) = \frac a2\theta^2, show that GD converges iff 0<η<2/a0 < \eta < 2/a. What happens at η=1/a\eta = 1/a?

Solution

θk+1=(1−ηa)θk\theta_{k+1} = (1 - \eta a)\theta_k, which tends to 0 iff ∣1−ηa∣<1|1 - \eta a| < 1. With η=1/a\eta = 1/a it jumps to the minimum in one step.

1AppliedFrom PID control

A drone hovers 2 m below its target height. Explain what each of P, I and D contributes, and what goes wrong with only P.

Solution

P pushes up in proportion to the 2 m error; D brakes as the error shrinks quickly, preventing overshoot; I accumulates any persistent error. With only P, gravity needs a non-zero thrust, which requires a non-zero error: the drone settles below the target (steady-state error) and may oscillate if KpK_p is large.

1ComputationFrom Classical mechanics

A ball is thrown up at 20 m/s (g=9.8g = 9.8 m/s²). Using calculus, find the maximum height and the time to reach it.

Solution

y(t)=20t−4.9t2y(t) = 20t - 4.9t^2; y′(t)=20−9.8t=0⇒t≈2.04y'(t) = 20 - 9.8t = 0 \Rightarrow t \approx 2.04 s, y≈20.4y \approx 20.4 m.

Why do edge detectors blur the image (e.g. with a Gaussian) before differentiating?

Solution

Differentiation amplifies high-frequency noise (in Fourier terms it multiplies by 2πiξ2\pi i\xi). Smoothing first suppresses those frequencies; since derivative and convolution commute, ∇(G∗I)=(∇G)∗I\nabla(G * I) = (\nabla G) * I, so one convolution with the derivative of a Gaussian does both.

For y^=σ(wx+b)\hat y = \sigma(wx + b) and L=12(y^−y)2L = \frac12(\hat y - y)^2, derive ∂L/∂w\partial L/\partial w and ∂L/∂b\partial L/\partial b.

Solution

∂L/∂w=(y^−y) y^(1−y^) x\partial L/\partial w = (\hat y - y)\,\hat y(1 - \hat y)\,x and ∂L/∂b=(y^−y) y^(1−y^)\partial L/\partial b = (\hat y - y)\,\hat y(1 - \hat y).

2ComputingFrom Backpropagation

Why does backprop need to store the forward activations, and how does gradient checkpointing trade memory for compute?

Solution

The local derivatives (σ′(z(ℓ))\sigma'(z^{(\ell)}), a(ℓ−1)a^{(\ell-1)}) depend on forward values. Checkpointing stores only some layers' activations and recomputes the others during the backward pass: memory drops (to O(L)O(\sqrt L) with optimal placement) at the cost of roughly one extra forward pass.

A service handles μ=100\mu = 100 req/s. Compare the mean response time at λ=50\lambda = 50, 9090 and 9999 req/s (M/M/1).

Solution

W=1/(μ−λ)W = 1/(\mu - \lambda): 20 ms, 100 ms and 1 s. Going from 90% to 99% utilization multiplies latency by 10.

↑ ↓ to navigate · ↵ · Esc