Beyond convexity: automatic differentiation

Getting exact derivatives of any function you can write in code — the tool that lets gradient descent work on problems with no formula.

What you'll learn
  • Use autograd to differentiate a Python function exactly
  • Connect first and second derivatives to slope and curvature
  • Check convexity of a function from its second derivative

Gradient descent needs gradients. For the bowls in this course they were easy to write by hand, but for a neural network or a long simulation there is no formula to differentiate. Automatic differentiation computes exact derivatives of any function written in code, by applying the chain rule to every operation as it runs.

The intro script does exactly this with the autograd library:

import autograd.numpy as np
from autograd import grad

def f(x):
    return x**2 + np.sin(x)

f_grad    = grad(f)          # df/dx, as a new function
f_hessian = grad(f_grad)     # d²f/dx², by differentiating again

f_grad(2.0)                  # 2*2 + cos(2) = 3.5839

The derivative is not estimated from nearby points; it is exact to floating-point precision, because each primitive (**, sin, +) knows its own derivative.

Slope, curvature, and convexity

For \(f(x) = x^2 + \sin x\):

\[f'(x) = 2x + \cos x, \qquad f''(x) = 2 - \sin x\]

The first derivative is the slope of the tangent; gradient descent walks against it. The second derivative is the curvature — and since \(\sin x \le 1\), \(f’‘(x) \ge 1 > 0\) everywhere. A function with positive second derivative everywhere is convex, so this \(f\) has exactly one minimum and gradient descent will always find it.

The marble descends f along its tangent (orange), whose slope is f'(x) — shown as the green point on the f' curve. Where f' crosses zero, the marble stops. The dotted f'' never drops below 1, which is why the descent always succeeds. Try a large step size to make it oscillate across the minimum.

The minimum is where \(f’(x) = 0\): \(2x + \cos x = 0\), at \(x \approx -0.4502\) with \(f \approx -0.2325\). There is no closed form for that root — gradient descent finds it anyway.

Vectors and quadratic forms

grad works the same way on functions of vectors, returning the whole gradient. The quadratic-problem script checks it against the formula from the linear algebra course:

A = np.array([[2.0, 0.5],
              [0.5, 1.0]])

def quadratic_function(x):
    return np.dot(x, np.dot(A, x))     # x^T A x

grad_quadratic = grad(quadratic_function)
grad_quadratic(np.array([1.0, 2.0]))   # equals 2 A x = [6., 5.]

For a symmetric \(A\), the gradient of \(\mathbf{x}^{\mathsf{T}}A\mathbf{x}\) is \(2A\mathbf{x}\), and autograd reproduces it without being told.

Why this is the bridge out of convexity

Convex problems come with guarantees; most real problems — training a network, fitting a physical model, planning through contact — do not. What they do share is that you can write their cost as code. Automatic differentiation turns any such code into gradients, and gradient-based methods then do their best on a landscape like the non-convex one in the first lesson: no certificate of global optimality, but in practice remarkably effective. It is the engine underneath PyTorch, JAX and TensorFlow, which the deep learning course uses throughout.

Try it yourself. Use grad to compute \(f’‘(x)\) for \(f(x) = x^2(1-x)^2\), the double well from the duality lesson, and find where it is negative — the region where that function is not convex. Then run gradient descent on it from \(x = 0.45\) and from \(x = 0.55\): which minimum does each reach, and why?

View source on GitHub →