ML Systems Lab Open interactive version →
Foundational 26 min read calculusgradientschain ruleconvexity

Calculus for ML

Gradients, chain rule, Hessian, convexity

You are training logistic regression with binary cross-entropy loss: L = -[y log(ŷ) + (1-y) log(1-ŷ)] where ŷ = σ(Wx + b). You want to update W to reduce L. How much does L change when you nudge W₁₁ — the weight from feature 1 to the output — by a tiny ε? This is the partial derivative ∂L/∂W₁₁. Computing all such partial derivatives for all weights gives the gradient ∇L — the direction of steepest ascent in loss space. You step in the opposite direction. This is gradient descent.

The chain rule makes gradient computation tractable for deep networks. For the composition L(ŷ(z(W))), the chain rule gives ∂L/∂W = (∂L/∂ŷ) × (∂ŷ/∂z) × (∂z/∂W) where z = Wx + b. Each arrow in the computation graph corresponds to a derivative. The product of derivatives along a path is the derivative of the composed function. For the sigmoid: ∂σ(z)/∂z = σ(z)(1 - σ(z)) — a closed form that depends only on the output value.

Key derivatives in ML: for MSE loss L = (y - Wx)²/2, the gradient is -(y - Wx)x = -residual × feature. For cross-entropy with sigmoid output, the gradient is (ŷ - y)x — the residual times the feature. These clean gradient formulas are not accidents. They are chosen precisely because the derivative of the cross-entropy loss through the sigmoid produces a numerically stable, interpretable update.

Taylor expansion: f(x + ε) ≈ f(x) + f'(x)ε + f''(x)ε²/2. Gradient descent uses the first-order approximation — it assumes the loss surface is locally linear at each step. Second-order methods (Newton's method) use the quadratic term via the Hessian. They take more accurate steps but require O(n²) memory to store the Hessian — infeasible for large models.

NOT this. Calculus in ML is not just gradient descent. Calculus is why certain loss and activation combinations work together and others do not. Using MSE loss with a sigmoid output produces gradient saturation — in the saturated region where σ(z) ≈ 0 or 1, the derivative σ(z)(1-σ(z)) ≈ 0, and the gradient of the MSE loss through this near-zero value nearly vanishes. The network cannot learn from these examples. Cross-entropy with sigmoid was chosen specifically because the saturating term cancels algebraically, leaving a non-saturating gradient. Every loss-activation combination is a calculus decision.

Key points

Takeaway

The choice of loss function and activation is a calculus decision, not an aesthetic one. The clean gradient (ŷ - y)x that makes logistic regression easy to train exists because cross-entropy and sigmoid were chosen together precisely to cancel the saturation term.

Recap

Check your understanding

Q1. What is the gradient of f(x) = ‖Ax − b‖₂² with respect to x? Derive it step by step.

Q2. The chain rule for ∂L/∂W at layer l requires the upstream gradient ∂L/∂z_{l+1}. Which two of the following correctly explain why backpropagation computes gradients with a backward pass rather than a forward pass?

Q3. You want to find the minimum of f(x₁,x₂) = (x₁−3)² + 2(x₂+1)². What are ∂f/∂x₁ and ∂f/∂x₂, and what is the minimum?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →