Exercises · 89
Exercises
Every exercise in the portal, with hints and worked solutions. Filter by type or level.
Prove that is irrational.
Hint
Assume in lowest terms and look at parity.
Solution
If then is even, so is even: . Then , so and is even too, contradicting that was in lowest terms.
In most languages 0.1 + 0.2 == 0.3 is false. Explain why in terms of real numbers.
Solution
, and have infinite binary expansions, so each is rounded to the nearest double. The rounded plus the rounded , rounded again, lands on a different double than the rounded . Compare with a tolerance: .
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, — which is exactly what makes code testable.
How many multiplications does evaluating take naively, and with Horner?
Solution
Naively (computing each power from scratch). Horner: , 4 multiplications.
A dataset doubles every 18 months. How long until it is 100 times larger?
Solution
years.
Why do ML libraries compute as with ?
Solution
Both are equal since . But overflows a double, while every and at least one equals 1, so the sum is in and safe.
A training loss goes (halving each epoch). After how many epochs is it below ? What kind of convergence is this?
Solution
, so 11 epochs. The error shrinks by a constant factor: linear (geometric) convergence.
Why does not exist? Describe the graph.
Solution
The graph oscillates between and infinitely often near 0. Along the values are ; along they are . Two sequences, two different limits.
For which is for , for continuous?
Solution
Left limit , value . Continuity requires , so .
Why can't you train a network whose activation is the step function with gradient descent?
Solution
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.
Sketch and, below it, . Where is zero, positive, negative?
Solution
: zero at (a local max at , a local min at ), negative on where decreases, positive outside.
A one-parameter model has loss . Starting at with learning rate , compute two steps of gradient descent.
Solution
. ; . Each step closes 20% of the gap to the minimum .
Prove that for . What is the maximum of ?
Solution
. Since , the maximum is at — so each sigmoid layer shrinks gradients by at least a factor 4, one cause of vanishing gradients.
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 , so the product is at most : vanishing gradients. ReLU ( when active) and residual connections avoid it.
Find and classify the critical points of .
Solution
: critical points 0 and 3. ; → minimum. At 0, and does not change sign (negative on both sides): not an extremum.
A server costs (latency penalty plus hardware) with instances. Which minimizes cost?
Solution
; , so it is a minimum, .
In the demo, compare the left and midpoint sums for on with . 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 per strip survives: total instead of .
Compute and interpret the sign.
Solution
. Positive: the area above the axis (for ) exceeds the small negative part on .
Estimate with an integral and random numbers. How many samples for 3 correct decimals?
Solution
with uniform. The standard error is about ; for you need . Monte Carlo is simple but slow.
Does converge?
Solution
No: for , and diverges (comparison with the harmonic series).
Quicksort makes about comparisons on average. Estimate it for .
Solution
, so about comparisons ().
An agent gets reward 1 every step forever, with . What is the return? What is the "effective horizon"?
Solution
. Rewards beyond about steps contribute little: the effective horizon is ~100 steps.
Where in the plane are the solutions of ? What shape do they form?
Solution
At , : the vertices of a regular hexagon inscribed in the unit circle.
Order by growth: , , , , .
Solution
(compare logarithms: , , , , ).
An algorithm takes 1 s for . Estimate the time for if it is , and if it is .
Solution
: factor → about 11.6 days. : factor → about 33 minutes.
Estimate with the differential of at .
Solution
(true value ).
A car passes two cameras 10 km apart, 5 minutes apart. Prove it exceeded 110 km/h at some moment.
Solution
Average speed km/h. By the MVT, at some instant .
A sensor reports velocity every 0.1 s. How do you estimate position, and which theorem justifies it?
Solution
Position is (FTC). With samples, approximate the integral by a sum, e.g. the trapezoidal rule . Errors accumulate (drift), which is why IMUs are fused with GPS.
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 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.
Write the Newton iteration for and do two steps from .
Solution
. , (root ).
Use the demo with and . What happens, and why?
Solution
, : a 2-cycle. The tangents bounce between 0 and 1 forever; the real root () is in another basin.
Derive the Newton iteration that computes using only multiplications and subtractions.
Solution
Take : . No division needed — this is how many processors divide.
Find the Maclaurin polynomial of order 4 of and use it to estimate .
Solution
; vs (error ).
Use the second-order Taylor model of to derive the step that minimizes it. Which method is this?
Solution
; setting the derivative in to zero gives . That is Newton's method for optimization.
How many terms of the Maclaurin series of guarantee (at ) to within ?
Solution
. needs (), so .
Derive the Maclaurin series of from and use it to write a series for .
Solution
; integrate: . At : (Leibniz; very slow).
Compute all first and second partial derivatives of and check that .
Solution
, , , , .
For and , show that .
Solution
, , . The product simplifies to : sigmoid and cross-entropy cancel beautifully.
Find for at and the rate of increase in the direction .
Solution
. ; the maximum possible is .
Draw the level curves of and the gradient at . Why does the arrow not point to the origin?
Solution
Level curves are ellipses, wider in . The gradient is perpendicular to the ellipse through , which is not the radial direction because the ellipse is not a circle.
Why does reverse-mode AD compute in roughly the time of 3–5 evaluations of , while finite differences would need ?
Solution
Finite differences perturb one parameter at a time. Reverse mode runs the program once forward, then once backward propagating ; 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).
Maximize subject to with a Lagrange multiplier.
Solution
gives , and gives , , .
Show that the distribution maximizing entropy with a fixed mean energy has the form .
Solution
, so : a softmax of .
A network computes . Write using the chain rule and say which factors are reused from .
Solution
Let , , . With : and . The upstream gradient is computed once and reused: that sharing is what makes backprop cheap.
Compute the Jacobian of and its determinant. Where is not locally invertible?
Solution
, . Only at the origin ( is in complex form).
A 2-link planar arm with lengths has hand position . When is ?
Solution
: zero when or — arm fully stretched or folded. There the hand cannot move radially.
Classify the critical points of .
Solution
at . : at positive definite → minimum; at indefinite → saddle.
On , what is the largest learning rate for which gradient descent converges? How many steps to reduce the -error by at that rate?
Solution
, so . Along each step multiplies the error by : . Condition number 100 makes it slow.
Rewrite as a first-order system.
Solution
With , : , .
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
; . min.
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 makes training diverge, using Euler's method.
Solution
Near a minimum , so GD is Euler on . Along the top eigenvector, , which grows when , i.e. .
For which is on a density? Compute .
Solution
, so . By symmetry .
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, by linearity of expectation (and exchanging gradient and expectation, which needs mild smoothness). Non-random batches (e.g. sorted data) break it.
RK4 costs 4 evaluations of per step, Euler 1. For error on , roughly how many evaluations does each need if the error constants are about 1?
Solution
Euler: → evaluations. RK4: → , about 32 steps → ~130 evaluations. Higher order wins by orders of magnitude.
Find the equilibria of and classify them.
Solution
and . : unstable, stable. Every positive population tends to the carrying capacity.
Compute the Fourier coefficients of the odd square wave on , on .
Solution
: for odd , 0 for even .
Convolving two signals of length directly costs operations. Estimate the cost via FFT.
Solution
Three FFTs of size ~ plus a pointwise product: about vs — roughly 8000 times faster.
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.
Convolve the sequence with the kernel .
Solution
: a moving average (with the edges padded by zeros).
Show that if the noise is Laplace, , maximum likelihood regression minimizes the absolute error.
Solution
; summing over the data, maximizing likelihood is minimizing .
A system has per day and initial error . When does the error reach 1? And if the initial error is ?
Solution
days. With : days. A million times better data only doubles the horizon.
Why does evaluating for with get worse when goes below about ?
Solution
The truncation error is but the rounding error of the numerator is . Their sum is minimized at ; smaller amplifies rounding.
A classifier outputs for the true class. What is its cross-entropy? And with ?
Solution
vs . Confidently wrong predictions are punished ~44 times more.
For , show that GD converges iff . What happens at ?
Solution
, which tends to 0 iff . With it jumps to the minimum in one step.
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 is large.
A ball is thrown up at 20 m/s ( m/s²). Using calculus, find the maximum height and the time to reach it.
Solution
; s, 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 ). Smoothing first suppresses those frequencies; since derivative and convolution commute, , so one convolution with the derivative of a Gaussian does both.
Why does backprop need to store the forward activations, and how does gradient checkpointing trade memory for compute?
Solution
The local derivatives (, ) depend on forward values. Checkpointing stores only some layers' activations and recomputes the others during the backward pass: memory drops (to with optimal placement) at the cost of roughly one extra forward pass.
A service handles req/s. Compare the mean response time at , and req/s (M/M/1).
Solution
: 20 ms, 100 ms and 1 s. Going from 90% to 99% utilization multiplies latency by 10.