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:
The gradient points uphill, so we move in the opposite direction. Learning rate controls step size.
| Variant | Batch Size | Speed | Stability |
|---|---|---|---|
| Batch GD | Full dataset | Slow | Stable |
| SGD | 1 sample | Fast, noisy | Can escape local minima |
| Mini-batch | 32–256 | Best tradeoff | GPU 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.
Related questions
- Derive the gradient of the cross-entropy loss for softmax output.
- What is the difference between convex and non-convex optimization? Why does it matter in deep learning?
- What are gradients, Jacobians, and Hessians? When do you use each in ML?
- What is the vanishing/exploding gradient problem? How do modern architectures solve it?
- Explain learning rate scheduling strategies. Which are most effective?