Gradient Flow Visualization
Interactive demo of two-variable gradient flow via central-difference numerical differentiation
An interactive demo that visualizes the gradient field of a two-variable function $f(x,y)$. Click anywhere on the screen to place a particle: it flows along the direction opposite to the gradient (the steepest-descent direction). This provides an intuitive view of the trajectory of gradient descent.
coordinates and gradient
What Is a Gradient Flow?
Gradient vs. Gradient Flow
The gradient $\nabla f$ is a vector pointing in the direction in which the function value increases most steeply (the uphill direction). The gradient flow, in contrast, is its opposite: it flows along $-\nabla f$ (the downhill direction). Note the minus sign. Physically, it corresponds to an object rolling down in the direction in which potential energy decreases, and in optimization it is the continuous-time counterpart of gradient descent.
The ODE of Gradient Flow
The gradient flow obeys the following ordinary differential equation:
$$\dfrac{d\mathbf{x}}{dt} = -\nabla f(\mathbf{x})$$The particle always moves in the direction in which the function value decreases most steeply, and stops upon reaching a local minimum where $\nabla f = \mathbf{0}$.
Gradient Computation via Numerical Differentiation
This demo approximates the gradient using central differences:
$$\begin{aligned} \dfrac{\partial f}{\partial x} &\approx \dfrac{f(x+h, y) - f(x-h, y)}{2h} \\[6pt] \dfrac{\partial f}{\partial y} &\approx \dfrac{f(x, y+h) - f(x, y-h)}{2h} \end{aligned}$$Points to Observe
- Basin of Attraction — For functions with multiple local minima, different initial positions converge to different minima. Note which valley each trajectory from a grid point is drawn into, and observe the structure of the boundaries between basins.
- Saddle Point — In saddle-point functions, an unstable equilibrium appears at which the gradient is zero but the point is not a minimum. Particles pause briefly near the saddle and then diverge rapidly.
- Narrow Valley — In the Rosenbrock function, particles descend steeply into the floor of the valley from outside, but along the floor the gradient becomes extremely small, so trajectories cluster densely and stall.