Escaping a Saddle Point with Noisy Gradient Descent

Quick Overview

Implement gradient descent with optional Gaussian noise on a function with a saddle point at the origin, then measure how many steps each run takes to leave it. Tests seeded NumPy reproducibility, careful trajectory bookkeeping, and how noise changes the escape time along a direction of negative curvature.

Escaping a Saddle Point with Noisy Gradient Descent

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

The function $f(x, y) = x^2 - y^2 + \frac{y^4}{4}$ has a saddle point at the origin and minima at $(0, \pm\sqrt{2})$. Its gradient is $\nabla f = (2x, -2y + y^3)$. Implement four NumPy functions that run gradient descent on $f$, with optional Gaussian noise, and measure when each run leaves the saddle. - `grad_f(p)` returns the analytic gradient `[2x, -2y + y**3]` as a float array. - `gradient_descent(p0, lr, n_steps, noise_std=0.0, seed=None)` applies the update `p <- p - lr * grad_f(p) + noise_std * z` with `z ~ N(0, I_2)`. Create `rng = np.random.default_rng(seed)` and draw exactly one `rng.standard_normal(2)` per step, computed before the update. Return a float array of shape `(n_steps + 1, 2)` whose row 0 equals `p0`. Do not mutate `p0`. - `escape_step(trajectory, threshold)` returns the smallest index `t` with `|y_t| >= threshold` as an `int`, or `-1` if no such index exists. - `compare_escape(p0, lr, noise_std, seed, threshold, max_steps)` runs plain descent (`noise_std=0`) and noisy descent for `max_steps` steps, both with the same `seed`, and returns `(plain_escape, noisy_escape)`. Expected behavior on small inputs: ```python grad_f(np.array([1.5, 2.0])) # [3.0, 4.0] gradient_descent(np.array([0.5, 0.1]), 0.1, 2) # [[0.5, 0.1 ], # [0.4, 0.1199 ], # [0.32, 0.143708]] escape_step(np.array([[0, 0.1], [0, -0.3], [0, -0.5], [0, 0.9]]), 0.5) # 2 gradient_descent(np.zeros(2), 0.1, 200) # stays at origin, escape_step -> -1 ``` ```hint Follow the draw rule exactly The spec fixes how many random draws happen on each step. Check that your loop follows it on every step, whatever the value of `noise_std`. ``` ```hint Where the escape comes from Linearize the y-update around the origin and look at the factor that multiplies `y_t` on each step. ``` ### Constraints & Assumptions - `p0` has length 2. `0 < lr ≤ 0.1`, `noise_std ≥ 0`, `threshold > 0`. - `n_steps, max_steps ≤ 2000`. Trajectories are checked to `rtol=1e-7` against a seeded reference. - Use NumPy only. ### What a Strong Answer Covers - A gradient that matches $f$ exactly and comes back as a float array. - An update loop that copies `p0`, stores `n_steps + 1` rows, and draws one standard normal pair per step in the stated order. - An `escape_step` that returns the first qualifying index as a Python `int`, counts index 0, and returns `-1` when the threshold is never reached. - What plain descent does when started exactly at the origin, and how noise changes the escape time from the saddle. - The time and memory cost of the loop. ### Follow-up Questions - Why does plain gradient descent from `(0, 1e-6)` eventually escape, and how does its escape time scale with the initial offset? - What learning rates keep the iteration stable on this function? - How does this relate to the guarantees for perturbed gradient descent in high dimensions?

Overview: Implement gradient descent with optional Gaussian noise on a function with a saddle point at the origin, then measure how many steps each run takes to leave it. Tests seeded NumPy reproducibility, careful trajectory bookkeeping, and how noise changes the escape time along a direction of negative curvature.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

The function f(x,y)=x2−y2+y44f(x, y) = x^2 - y^2 + \frac{y^4}{4} has a saddle point at the origin and minima at (0,±2)(0, \pm\sqrt{2}). Its gradient is ∇f=(2x,−2y+y3)\nabla f = (2x, -2y + y^3). Implement four NumPy functions that run gradient descent on ff, with optional Gaussian noise, and measure when each run leaves the saddle.

  • grad_f(p) returns the analytic gradient [2x, -2y + y**3] as a float array.
  • gradient_descent(p0, lr, n_steps, noise_std=0.0, seed=None) applies the update p <- p - lr * grad_f(p) + noise_std * z with z ~ N(0, I_2) . Create rng = np.random.default_rng(seed) and draw exactly one rng.standard_normal(2) per step, computed before the update. Return a float array of shape (n_steps + 1, 2) whose row 0 equals p0 . Do not mutate p0 .
  • escape_step(trajectory, threshold) returns the smallest index t with |y_t| >= threshold as an int , or -1 if no such index exists.
  • compare_escape(p0, lr, noise_std, seed, threshold, max_steps) runs plain descent ( noise_std=0 ) and noisy descent for max_steps steps, both with the same seed , and returns (plain_escape, noisy_escape) .

Expected behavior on small inputs:

grad_f(np.array([1.5, 2.0]))            # [3.0, 4.0]

gradient_descent(np.array([0.5, 0.1]), 0.1, 2)
# [[0.5,  0.1     ],
#  [0.4,  0.1199  ],
#  [0.32, 0.143708]]

escape_step(np.array([[0, 0.1], [0, -0.3], [0, -0.5], [0, 0.9]]), 0.5)  # 2

gradient_descent(np.zeros(2), 0.1, 200)  # stays at origin, escape_step -> -1

Constraints & Assumptions

  • p0 has length 2. 0 < lr ≤ 0.1 , noise_std ≥ 0 , threshold > 0 .
  • n_steps, max_steps ≤ 2000 . Trajectories are checked to rtol=1e-7 against a seeded reference.
  • Use NumPy only.

What a Strong Answer Covers Guidance

  • A gradient that matches ff exactly and comes back as a float array.
  • An update loop that copies p0 , stores n_steps + 1 rows, and draws one standard normal pair per step in the stated order.
  • An escape_step that returns the first qualifying index as a Python int , counts index 0, and returns -1 when the threshold is never reached.
  • What plain descent does when started exactly at the origin, and how noise changes the escape time from the saddle.
  • The time and memory cost of the loop.

Follow-up Questions Guidance

  • Why does plain gradient descent from (0, 1e-6) eventually escape, and how does its escape time scale with the initial offset?
  • What learning rates keep the iteration stable on this function?
  • How does this relate to the guarantees for perturbed gradient descent in high dimensions?