Machine Learning/Gradient Descent
50 / 67

04/2022Machine Learning

Gradient Descent

Fitting regression classifiers by gradient descent — deriving the update from the loss gradient, then stepping the parameters downhill and watching conditioning set the pace.

╌╌╌╌

Regression models fit to a labeled dataset with no closed-form solver in play: the parameters start arbitrary and gradient descent moves them toward the values that minimize the loss. The point of the exercise is the optimizer itself — deriving the gradient of the loss and stepping against it.

For parameters and a loss averaged over the training set, gradient descent repeatedly steps opposite the gradient:

where the learning rate sets the step size. The gradient points uphill, so its negation is the direction of steepest local decrease. For a convex loss the iteration reaches the global minimum; otherwise it settles into a local one.

Take squared-error loss over examples, and write the per-example residual :

Each residual depends on the weights through , so the chain rule differentiates one term as . Averaging over the examples gives the gradient the update uses:

It is the average of each example's error scaled by its features, so the step pushes weights toward reducing the largest residuals first.

How quickly descent converges depends on the shape of that bowl. The loss here is a quadratic bowl, and its contours are ellipses whose axes are the eigenvectors of the Hessian . When the features are on similar scales the bowl is round and descent heads almost straight for the minimum. When one direction is much steeper than another the bowl is a narrow valley, and the gradient, perpendicular to each contour, points mostly across the valley rather than down it, so the path zigzags.

Elliptical loss contours with the minimum at the center. On elongated, ill-conditioned contours the negative gradient points across the valley, so gradient descent zigzags in toward the minimum.

The step size interacts with that shape. Convergence on a quadratic needs , where is the largest curvature; above it the iteration overshoots and diverges. The number of steps to converge grows with the condition number , the ratio of the largest to the smallest curvature, which is exactly how elongated the ellipses are. Too small an is always safe but crawls.

Algorithm:Gradient-Descent(X,y,η)\textsc{Gradient-Descent}(X, y, \eta) — batch parameter fit
  1. 1
    input: features XX, targets yy, learning rate η\eta
  2. 2
    initialize w\mathbf{w} to zeros
  3. 3
    repeat
  4. 4
    gwJ(w)\mathbf{g} \gets \nabla_{\mathbf{w}} J(\mathbf{w}) over all examples
  5. 5
    wwηg\mathbf{w} \gets \mathbf{w} - \eta \, \mathbf{g}
  6. 6
    until J(w)J(\mathbf{w}) stops decreasing
  7. 7
    return w\mathbf{w}

Standardizing the features first shrinks toward , so no single dimension dominates the step and one learning rate suits them all. Stochastic and minibatch variants estimate the gradient from a subset each step, trading a noisier direction for far more updates per pass over the data. Training stops when the loss flattens.

References

  1. Project repository
  2. Reference notes: Numerical Optimization and Gradient Descent
  3. Reference notes: Learning from Examples

╌╌ END ╌╌