Day 14 · 2026.07.06

Optimization

Optimization — how "find the best one" became a branch of mathematics
"Nothing in the world takes place without optimization, and there is no doubt that all aspects of the world that have a rational basis can be explained by optimization methods." — Leonhard Euler

Gradient Descent

The algorithm that walks downhill blindfolded
Optimization
Intuition

You're blindfolded on a hillside and must reach the lowest valley. You can't see far, but your feet can feel which direction is steepest right here. The strategy is humble: step in the steepest downhill direction, feel again, step again. If the terrain is well-behaved, you'll reach the bottom.

That's gradient descent. The gradient is the mathematical name for "steepest uphill direction"—a vector assembled from the function's partial derivatives along each coordinate. To descend, walk in the opposite direction. All of modern machine learning—from linear regression to GPT—is this loop of "feel the slope, take a step," repeated millions of times across a hillside of billions of dimensions.

$$\theta_{t+1} = \theta_t - \eta\,\nabla f(\theta_t)$$
Formal definition

$\theta_t$ is your current position (the model's parameters), $\nabla f$ is the gradient, and the minus sign turns you around, downhill. $\eta$ is the learning rate—your step size: too small and you crawl forever; too large and you overshoot the valley and climb the far wall. This single line is the entire main loop of the deep-learning engine.

start minimum f(θ)
Why it's beautiful

Its beauty is "local information driving a global goal." You don't need to see the whole mountain or solve any equation—just a small patch of slope under your feet steers you, step by step, toward a good answer: a complex, high-dimensional problem is solved by the dumbest possible rule, "improve a little each time." It echoes nature—water flows downhill, light takes the shortest path, proteins fold to their lowest-energy state; the universe seems to be doing gradient descent everywhere.

Applications

It's the unshakable foundation of deep learning: the loss function is the "mountain," and backpropagation uses the chain rule to compute billions of partial derivatives (the gradient) in one sweep, from which the optimizer updates the weights. It also powers logistics routing, chip layout, portfolio allocation, and robot control: wherever there's "an objective to minimize," gradient descent is often the first and sharpest knife.

Essence + a question
You don't need to see the whole mountain—just step in the steepest downhill direction each time, and you'll reach the valley floor.
If there's more than one valley (many small dips), gradient descent gets stuck in the one nearest your start, not the deepest. Why does this fear of "getting trapped in a bad local minimum" turn out to matter far less than expected in networks with billions of dimensions?

Convexity

The magic that makes "local optimum = global optimum"
Convex Optimization
Intuition

Picture a bowl. Wherever on the bowl's wall you place a marble, it always rolls to the same lowest point. A bowl has no "false valleys"—no little dips that can trap the marble. That's the world of a convex function: the terrain is a smooth bowl, and any "local minimum" is necessarily the "global minimum."

A visual test: pick any two points on the graph and draw the line segment between them. If the segment always lies on or above the function, it's convex. Convexity is the watershed of optimization theory, because it turns "find the best"—a potentially very hard problem—into "just walk downhill," where you never have to worry about being in the wrong valley.

$$f\big(\lambda x+(1-\lambda)y\big)\;\le\;\lambda f(x)+(1-\lambda)f(y)$$
Formal definition

The left side is "the function's value at a point on the segment joining $x$ and $y$"; the right side is "the weighted average of the endpoint values" (i.e. the height of that chord), with $\lambda\in[0,1]$ sliding along. The inequality says: the curve never rises above its chord. That is the precise statement of "bowl-shaped, no false valleys." Equivalently, if the second derivative (in higher dimensions, the Hessian matrix) is everywhere non-negative, the function is convex—the curvature always bends upward, never doubling back to form a trap.

x y curve below chord f
Why it's beautiful

Convexity draws the border between heaven and hell in the optimization world. The mathematician Rockafellar famously said: "the great watershed in optimization is not between linearity and nonlinearity, but between convexity and nonconvexity." Convex problems can almost always be solved reliably to the global optimum, with a mathematical guarantee that "this is the best"; nonconvex problems may never let you know whether you've reached the optimum. A single geometric property (bowl-shaped) decides whether a problem is "solvable" or "doomed to approximation"—this clarity of "structure decides fate" is a deep mathematical beauty.

Applications

SVMs, Lasso and ridge regression, logistic regression, mean–variance portfolio optimization—all are convex, hence reliably solvable. Engineers deliberately "convexify" problems: change variables, relax constraints, all to land in convex heaven. By contrast, deep learning's loss surface is wildly nonconvex; we gave up the global guarantee but gained astonishing expressive power—a fascinating trade-off at the heart of modern AI.

Essence + a question
Convexity is a passport: as long as the terrain is a bowl, "walk downhill" equals "walk toward the global optimum."
A deep network's loss surface is highly nonconvex, so in theory gradient descent has no global guarantee—yet in practice it trains superbly. Does this suggest the iron law "only convex is reliable" needs to be rethought in very high-dimensional worlds?

Lagrange Multipliers

Finding the optimum while wearing chains
Constrained Optimization
Intuition

Many optimizations aren't "find the lowest point anywhere," but are constrained: enclose the largest area with a fixed length of fence, maximize utility within a budget, fit data with a neural net whose weights stay "not too big." A constraint confines you to a curve, and you can only move along it.

Lagrange's insight is elegant: standing on the constraint curve, as long as you can still move along it and keep improving the objective, you haven't reached the optimum. The optimum is exactly where "the objective's contour line is tangent to the constraint curve"—at that instant, any movement along the constraint stops improving the objective. Tangency means the two gradients point in parallel directions, differing only by a factor—and that factor is the Lagrange multiplier $\lambda$.

$$\nabla f = \lambda\,\nabla g,\qquad \text{constraint } g(x)=0$$
Formal definition

$f$ is the objective, $g(x)=0$ is the constraint surface. The equation says: at the optimum, the objective's gradient $\nabla f$ and the constraint's gradient $\nabla g$ point in the same direction (the algebraic form of tangency), with proportionality factor $\lambda$. It also carries a striking economic meaning—$\lambda$ is the "shadow price": how much the optimal objective value improves when the constraint is relaxed by one unit is exactly that $\lambda$. It quantifies how precious the constraint is: how much one more dollar of budget earns you.

f contours g(x)=0 tangent = optimum
Why it's beautiful

It turns a "constraint" from an obstacle into an equal member of the equation system: introduce a new variable $\lambda$, and constraint and objective fuse into a single "Lagrangian," which you differentiate all at once. More beautiful is the duality—$\lambda$ is both the means of solving and the marginal value of the constraint. One symbol plays both "key" and "price tag"; this compression of meaning is one of the most striking moments in mathematics.

Applications

It's the universal language of constrained optimization. Economics uses it to derive consumer equilibrium ($\lambda$ = the marginal utility of income); physics's Lagrangian mechanics uses it to unify classical mechanics, with $\lambda$ corresponding to constraint forces; in machine learning, the dual derivation of SVMs and the fact that "weight decay = imposing a constraint on the parameter norm" both stem from it; its generalization, the KKT conditions, is the theoretical core of every convex-optimization solver.

Essence + a question
The chained optimum is where the objective's contour is tangent to the constraint curve; that tangency ratio also quietly tells you what the constraint is worth.
"Regularization" (adding $\lambda\|\theta\|^2$ to the loss) and "setting a hard upper bound on the weight norm" are two mathematically equivalent sides of the same coin. This equivalence is the Lagrange multiplier's most everyday incarnation in ML—can you articulate what happens to the model as $\lambda$ grows?

Stochastic Gradient Descent

Trading a "slightly worse" gradient for a "much faster" speed
Machine Learning
Intuition

To train a model, the "true slope" must be computed from all the data—tens of millions of samples each contributing a bit. Traversing the full dataset for every single step is hopelessly slow. Stochastic gradient descent (SGD) makes a bet: rather than compute the slope exactly and take one big step, estimate a rough slope from a small batch of samples and get moving—each step's direction wobbles a little, but you move fast, and the wobble cancels out over millions of steps.

That wobble is an unexpected gift: like a random stumble for the descender, it can jog you out of shallow dips, keeping you from getting stuck early in a bad local minimum. Here noise turns from a defect into a feature.

$$\theta_{t+1} = \theta_t - \eta\,\nabla f_{\mathcal{B}_t}(\theta_t)$$
Formal definition

The only difference from gradient descent is the subscript $\mathcal{B}_t$: each step estimates the gradient from a single random mini-batch of data rather than the full set. This estimate is unbiased—right on average, with random error on any single step. Training thus becomes a trade of "cheap, noisy gradients for many more update steps." In practice, on massive data, many rough steps beat a few precise ones.

start wobbling to the floor loss
Why it's beautiful

It overturns an intuition: being imprecise is actually better. Classical optimization chases an optimal step every time, but SGD proves that in an ocean of data, "fast and rough" systematically beats "slow and precise." And going deeper: since the data itself is noisy, why compute a gradient more precise than the data? This "embrace randomness" spirit runs through Monte Carlo, annealing, even evolutionary selection—noise is not the enemy; it's the fuel of exploration.

Applications

Almost every large model today is trained by SGD's descendants: Adam adapts the step size per parameter, Momentum accumulates inertia to charge across plateaus, and learning-rate schedules explore in big steps first then converge in small ones. The trillions of parameter updates in ChatGPT, Stable Diffusion, and AlphaFold are all, at heart, the loop of "grab a small batch, estimate a gradient, take a step." Understand SGD and you understand the core machine roaring day and night in the engine room of modern AI.

Essence + a question
Taking many steps with a "rough gradient" from a small batch beats a few steps with a "precise gradient" from all the data; and its noise conveniently helps you escape shallow dips.
The smaller the batch, the rougher the gradient and the larger the noise—yet generalization is often better; the larger the batch, the more precise, yet it more often overfits to sharp minima. Why does "estimating less accurately" make the model perform better on data it hasn't seen?
Deeper Reflections
Why is the gradient the "steepest ascent direction," and not some other direction?
Walking in a unit direction $u$, the function's instantaneous rate of change is the directional derivative $\nabla f\cdot u$—the dot product of gradient and direction. The dot product is largest when the two vectors are aligned, so the fastest-growing direction is exactly $\nabla f$, and the fastest descent is $-\nabla f$. This isn't a definition but a corollary of dot-product geometry. It also exposes gradient descent's limitation: steepest is only a local best choice; in a long, curved valley, always going steepest zig-zags and converges painfully slowly—precisely what Momentum and second-order methods are built to fix.
Convex optimization "almost always solves to the global optimum," so are nonconvex problems hopeless?
In theory, general nonconvex optimization is NP-hard to solve globally. But practice offers three ways out: first, many nonconvex problems have "hidden benign structure" (e.g. matrix completion), where local optima are almost all global; second, in the high-dimensional loss surfaces of deep networks bad local minima are extremely rare, most critical points being saddle points rather than pits, and SGD's noise escapes saddles effectively; third, we simply drop the obsession with "the global optimum"—a "good enough" solution already powers ChatGPT. This marks a pragmatic shift in optimization philosophy from "seek the optimal" to "seek the good enough."
The Lagrange multiplier λ is a "shadow price"—how deep does this economic intuition run?
Deep enough to run through all of duality theory. $\lambda$ exactly equals "the rate of improvement in the optimal value when the constraint loosens by one unit." In linear programming, the primal problem (how to maximize output from resources) and the dual problem (how to price the resources) mirror each other, and at the optimum their objective values are equal—this is strong duality. It means that behind any resource-allocation problem hides a self-consistent "pricing system": market prices, option pricing, bandwidth allocation, auction design are all, in essence, solving the dual prices of some optimization problem. Lagrange multipliers weld "optimum" and "value" into two sides of one coin.
Why does SGD's noise help "generalization," not just "escaping dips"?
A hypothesis with supporting evidence is that SGD's noise favors flat minima over sharp ones. A flat valley floor means "perturb the parameters slightly and the loss barely changes"—such a solution is more robust to the small distribution gap between training and test sets, so it generalizes better; a sharp valley is sensitive to perturbation and prone to overfitting. The larger noise of small batches acts like a sieve, continually shaking the model out of sharp pits and leaving it in flat basins. Thus the tool used for training quietly shapes the generalization quality of the final solution—one of the most active frontiers in deep-learning theory.
Further Resources