Vector-Jacobian products and automatic differentiation
Consider . The Jacobian of , denoted , is an matrix of all the partial derivatives. Pretty basic:
For and , let's look at the product of the row vector and . This is known as the vector-Jacobian product (VJP).
The row vectors are pesky, so transposes are taken.
As the "variables" are , , and in , we can define a new operation .[1]
Note that .
But that still requires us to calculate , which can be hard depending on . This is especially the case if is a composition of multiple functions.[2]
The chain rule for Jacobians is . We can define as the th intermediate function value. Reconciling this with VJPs:
Noting that , we can express with a composition of s.
Which brings us to our conclusion:
The value of this representation of VJPs lies in the fact that it enables reverse mode automatic differentiation.
Automatic differentiation
Automatic differentiation (autodiff) follows from the fact that we can obtain the VJP of a composite function algorithmically by evaluating the VJPs of its constituent functions. Since even the most complicated of functions are made up of a composition of elementary functions, and the VJP of elementary functions is trivial, we can build up VJPs for "complicated" (i.e. lots of variables, deep composition, etc.) functions step-by-step. We require numeric values for and which sets this method apart from symbol differentiation.
Reverse mode autodiff refers to the fact that two passes -- forward and backward -- are required to calculate VJPs. Calculating the VJP of requires us to have calculated the VJP of as well as . The forward pass as we evaluate from to gives us the intermediate value . The backwards pass as we build up the VJPs from to calculates the VJP of .
Wait, are VJPs actually doing differentiation though? Sure, we can calculate the product of a vector and Jacobian, but differentiation would imply something like a gradient or the entire Jacobian. Turns out VJPs allow us to calculate both. Using and , we obtain the VJP:
So the VJP of a scalar function can be used to calculate its gradient at a point. Similarly, we can calculate the entire Jacobian of a function row-by-row. Choosing as one-hot encoded vectors results in the VJP being the row of the Jacobian encoded by .
It is worth noting that building the entire Jacobian with VJPs requires passes for with an Jacobian. We can assume that this method is more efficent the smaller is compared to . In a machine learning context, is common as the outputs are scalar loss values and the inputs are model weights. This makes VJPs suitable for machine learning backpropagation.
Vector-Jacobian products can be built up from VJPs and produce gradients and Jacobians. This enables reverse mode automatic differentiation, which is efficient for machine learning. Everything falls into place.