Post · 2 min read· pp. 5–6

Gradient-Vector Product

We've all written the following test for our gradient code (known as the finite-difference approximation).

∂∂xif(x)≈12ε(f(x+ε⋅ei)−f(x−ε⋅ei))\frac{\partial}{\partial x_i} f(\boldsymbol{x}) \approx \frac{1}{2 \varepsilon} \Big( f(\boldsymbol{x} + \varepsilon \cdot \boldsymbol{e_i}) - f(\boldsymbol{x} - \varepsilon \cdot \boldsymbol{e_i}) \Big)

where ε>0\varepsilon > 0 and ei\boldsymbol{e_i} is a vector of zeros except at ii where it is $1$. This approximation is exact in the limit, and accurate to o(ε)o(\varepsilon) additive error.

This is a specific instance of a more general approximation! The dot product of the gradient and any (conformable) vector d\boldsymbol{d} can be approximated with the following formula,

∇f(x)⊤d≈12ε(f(x+ε⋅d)−f(x−ε⋅d))\nabla f(\boldsymbol{x})^{\top} \boldsymbol{d} \approx \frac{1}{2 \varepsilon} \Big( f(\boldsymbol{x} + \varepsilon \cdot \boldsymbol{d}) - f(\boldsymbol{x} - \varepsilon \cdot \boldsymbol{d}) \Big)

We get the special case above when d=ei\boldsymbol{d}=\boldsymbol{e_i}. This also exact in the limit and just as accurate.

Runtime? Finite-difference approximation is probably too slow for approximating a high-dimensional gradient because the number of function evaluations required is $2 n$ where nn is the dimensionality of xx. However, if the end goal is to approximate a gradient-vector product, a mere $2$ function evaluations is probably faster than specialized code for computing the gradient.

How to set ε\varepsilon? The second approach is more sensitive to ε\varepsilon because d\boldsymbol{d} is arbitrary, unlike ei\boldsymbol{e_i}, which is a simple unit-norm vector. Luckily some guidance is available. Andrei (2009) reccommends

ε=ϵmach(1+∥x∥∞)/∥d∥∞\varepsilon = \sqrt{\epsilon_{\text{mach}}} (1 + \|\boldsymbol{x} \|_{\infty}) / \| \boldsymbol{d} \|_{\infty}

where ϵmach\epsilon_{\text{mach}} is machine epsilon. (Numpy users: numpy.finfo(x.dtype).eps).

Why do I care?

  1. Well, I tend to work on sparse, but high-dimensional problems where finite-difference would be too slow. Thus, my usual solution is to only test several randomly selected dimensions−-biasing samples toward dimensions which should be nonzero. With the new trick, I can effectively test more dimensions at once by taking random vectors d\boldsymbol{d}. I recommend sampling d\boldsymbol{d} from a spherical Gaussian so that we're uniform on the angle of the vector.
  2. Sometimes the gradient-vector dot product is the end goal. This is the case with Hessian-vector products, which arises in many optimization algorithms, such as stochastic meta descent. Hessian-vector products are an instance of the gradient-vector dot product because the Hessian is just the gradient of the gradient.

Hessian-vector product

Hessian-vector products are an instance of the gradient-vector dot product because since the Hessian is just the gradient of the gradient! Now you only need to remember one formula!

H(x) d≈12ε(∇f(x+ε⋅d)−∇f(x−ε⋅d))H(\boldsymbol{x})\, \boldsymbol{d} \approx \frac{1}{2 \varepsilon} \Big( \nabla f(\boldsymbol{x} + \varepsilon \cdot \boldsymbol{d}) - \nabla f(\boldsymbol{x} - \varepsilon \cdot \boldsymbol{d}) \Big)

With this trick you never have to actually compute the gnarly Hessian! More on Justin Domke's blog