GithubHelp home page GithubHelp logo

jacobusmmsmit / reversediff.jl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juliadiff/reversediff.jl

1.0 1.0 0.0 465 KB

Reverse Mode Automatic Differentiation for Julia

License: Other

Julia 100.00%

reversediff.jl's Introduction

ReverseDiff

Stable Dev Build status codecov.io

See ReverseDiff Usage Examples

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 @forward and @skip (with more to come!)
  • compatible with ForwardDiff, enabling mixed-mode AD
  • built-in definitions leverage the benefits of ForwardDiff's Dual numbers (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 map and broadcast optimizations
  • it's well tested

...and, simply put, it's fast (for gradients, at least). Using the code from examples/gradient.jl:

julia> using BenchmarkTools, Pkg

# this script defines f and ∇f!
julia> include(joinpath(Pkg.dir("ReverseDiff"), "examples/gradient.jl"));

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.

reversediff.jl's People

Contributors

andreasnoack avatar bicycle1885 avatar chrisrackauckas avatar delehef avatar dependabot[bot] avatar devmotion avatar dkarrasch avatar femtocleaner[bot] avatar github-actions[bot] avatar hyrodium avatar jmxpearson avatar jrevels avatar juliatagbot avatar mateuszbaran avatar miguelraz avatar mipals avatar mohamed82008 avatar ranocha avatar rdeits avatar sethaxen avatar staticfloat avatar viralbshah avatar willtebbutt avatar yebai avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.