3 ms·
There are automatic differentiation libraries for many languages, including C++. The result is generally only piecewise differentiable because, e.g. you obviou
by nullc 7y ago
There are automatic differentiation libraries for many languages, including C++.
The result is generally only piecewise differentiable because, e.g. you obviously cannot differentiate across the hard branch of an if-statement.
Usually for optimization you want reverse-mode differentiation, where you learn how the function output-- some optimization objective-- depends on the input variables. The automatic differentiation libraries records all the operations you perform on the input variables and then composes them using the chain rule, working backwards.
For some kinds of codebases it's actually pretty natural... e.g. I've taken a big body of C digital signal processing code and in a couple hours gotten gradients for it just by replacing all the floating point variables with special differentiable types from an AD library. These libraries also include collections of standard math lib functions which it knows the derivative for.
For the purpose of optimization the fact that your program isn't differentiable everywhere isn't necessarily a problem. You can use gradient free methods for searches across those boundaries, and gradient methods to refine your results within them.
If you're optimizing over problems that have thousands of input variables having derivatives is critical to making machine optimization tractable-- unless your search space is particularly trivial and can be well approximated by the simple differentiable models that gradient free local optimization tools use internally.
Of course, you could also attempt to directly program your derivatives-- it almost would certainly be much faster to evaluate than transcript based reverse mode AD. But it may be impossible to construct analytical derivatives that cover your whole problem domain, it's often unreasonable hard even when possible, and even if you have them keeping the functions in-sync is difficult and a source of devious bugs.