MLCrack

    ML MATH & LINEAR ALGEBRA · INTERVIEW QUESTION

    What is gradient descent? Explain the intuition behind it.

    🟢 beginner · Calculus & Optimization

    Hint: Imagine finding the lowest point in a hilly landscape by always walking downhill.

    Answer

    Gradient descent is an iterative optimization algorithm:

    θt+1=θt−η∇L(θt)\theta_{t+1} = \theta_t - \eta \nabla \mathcal{L}(\theta_t)

    The gradient ∇L\nabla \mathcal{L} points uphill, so we move in the opposite direction. Learning rate η\eta controls step size.

    VariantBatch SizeSpeedStability
    Batch GDFull datasetSlowStable
    SGD1 sampleFast, noisyCan escape local minima
    Mini-batch32–256Best tradeoffGPU parallelism
    python
    # Simple gradient descent def gradient_descent(grad_fn, theta, lr=0.01, steps=1000): for _ in range(steps): grad = grad_fn(theta) theta = theta - lr * grad return theta # PyTorch example optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) for batch in dataloader: loss = criterion(model(batch.x), batch.y) loss.backward() optimizer.step() optimizer.zero_grad()

    Convergence needs explicit assumptions. For a convex, L-smooth objective with an attained minimum, a suitable fixed step (for example 0 < eta <= 1/L) yields convergence in objective value. Merely decreasing eta is insufficient. In non-convex optimization, many results concern approaching stationary points under additional assumptions, not guaranteed global minima or convergence for every training run.