JuliaDiff/ReverseDiff.jl
소개
Julia용 역방향 모드 자동 미분입니다.
기타 프로젝트What you can do
- supports a large subset of the Julia language, including loops, recursion, and control flow
- user-friendly API for reusing and compiling tapes
- user-friendly performance annotations such as @forward and @skip (with more to come!)
- compatible with ForwardDiff, enabling mixed-mode AD
측정된 스타 증가
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- +2
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalReverseDiff
ReverseDiff is a fast and compile-able tape-based reverse mode automatic differentiation (AD) that implements methods to take gradients, Jacobians, Hessians, and higher-order derivatives of native Julia functions (or any callable object, really).
While performance can vary depending on the functions you evaluate, the algorithms implemented by ReverseDiff generally outperform non-AD algorithms in both speed and accuracy.
Wikipedia's entry on automatic differentiation is a useful resource for learning about the advantages of AD techniques over other common differentiation methods (such as finite differencing).
Installation
To install ReverseDiff, simply use Julia's package manager:
julia> Pkg.add("ReverseDiff")
Why use ReverseDiff?
Other Julia packages may provide some of these features, but only ReverseDiff provides all of them (as far as I know at the time of this writing):
- supports a large subset of the Julia language, including loops, recursion, and control flow
- user-friendly API for reusing and compiling tapes
- user-friendly performance annotations such as
@forwardand@skip(with more to come!) - compatible with ForwardDiff, enabling mixed-mode AD
- built-in definitions leverage the benefits of ForwardDiff's
Dualnumbers (e.g. SIMD, zero-overhead arithmetic) - a familiar differentiation API for ForwardDiff users
- non-allocating linear algebra optimizations
- nested differentiation
- suitable as an execution backend for graphical machine learning libraries
- ReverseDiff doesn't need to record scalar indexing operations (a huge cost for many similar libraries)
- higher-order
mapandbroadcastoptimizations - it's well tested
...and, simply put, it's fast (for gradients, at least). For example:
julia> using BenchmarkTools, ReverseDiff
julia> f(a, b) = sum(a' * b + a * b');
julia> compiled_f_tape = ReverseDiff.compile(ReverseDiff.GradientTape(f, (rand(100, 100), rand(100, 100))));
julia> ∇f!(results, inputs) = ReverseDiff.gradient!(results, compiled_f_tape, inputs);
julia> a, b = rand(100, 100), rand(100, 100);
julia> inputs = (a, b);
julia> results = (similar(a), similar(b));
# Benchmark the original objective function, sum(a' * b + a * b')
julia> @benchmark f($a, $b)
BenchmarkTools.Trial:
memory estimate: 234.61 kb
allocs estimate: 6
--------------
minimum time: 110.000 μs (0.00% GC)
median time: 137.416 μs (0.00% GC)
mean time: 173.085 μs (11.63% GC)
maximum time: 3.613 ms (91.47% GC)
# Benchmark ∇f! at the same inputs (this is executing the function,
# getting the gradient w.r.t. `a`, and getting the gradient w.r.t
# to `b` simultaneously). Notice that the whole thing is
# non-allocating.
julia> @benchmark ∇f!($results, $inputs)
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 429.650 μs (0.00% GC)
median time: 431.460 μs (0.00% GC)
mean time: 469.916 μs (0.00% GC)
maximum time: 937.512 μs (0.00% GC)
I've used this benchmark (and others) to pit ReverseDiff against every other native Julia reverse-mode AD package that I know of (including source-to-source packages), and have found ReverseDiff to be faster and use less memory in most cases.
Should I use ReverseDiff or ForwardDiff?
ForwardDiff is algorithmically more efficient for differentiating functions where the input dimension is less than the output dimension, while ReverseDiff is algorithmically more efficient for differentiating functions where the output dimension is less than the input dimension.
Thus, ReverseDiff is generally a better choice for gradients, but Jacobians and Hessians are trickier to determine. For example, optimized methods for computing nested derivatives might use a combination of forward-mode and reverse-mode AD.
ForwardDiff is often faster than ReverseDiff for lower dimensional gradients (length(input) < 100), or gradients of functions where the number of input parameters is small compared
to the number of operations performed on them. ReverseDiff is often faster if your code
is expressed as a series of array operations, e.g. a composition of Julia's Base linear
algebra methods.
In general, your choice of algorithms will depend on the function being differentiated, and you should benchmark different methods to see how they fare.
관련 프로젝트 살펴보기
불러오는 중…