makeGradients
wrapper to simplify common use case
#91
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The most common "big win" case for reverse-mode autodiff is for a function from a vector over the reals to the reals. But compared to
ForwardDiff.jl
,ReverseDiff.jl
has some boilerplate and cognitive overhead, because of the need to preallocate the tape.A simple way around this is to build a wrapper
makeGradients
like so:This seems to me to arrive at the best of both worlds: It's very simple to use, and computation is efficient and requires no allocation. And it returns both the gradient as well as a function that computes the value and gradient together in only one pass. Many algorithms require both of these in order to really be efficient. The wrapper makes building it very simple.
In principle, this could be extended to inputs other than vectors, as well as to computations other than function values and gradients.