项目作者: Non-Contradiction

项目描述 :
Automatic Differentiation for R
高级语言: R
项目地址: git://github.com/Non-Contradiction/autodiffr.git
创建时间: 2018-05-02T00:49:40Z
项目社区:https://github.com/Non-Contradiction/autodiffr

开源协议:Other

下载


autodiffr for Automatic Differentiation in R through Julia

Travis-CI Build
Status
AppVeyor Build
Status
!-- [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/JuliaCall)](https://cran.r-project.org/package=JuliaCall) [![](https://cranlogs.r-pkg.org/badges/JuliaCall)](https://cran.r-project.org/package=JuliaCall) [![](https://cranlogs.r-pkg.org/badges/grand-total/JuliaCall)](https://cran.r-project.org/package=JuliaCall) --

Package autodiffr provides an R wrapper for Julia packages
ForwardDiff.jl and
ReverseDiff.jl through
JuliaCall to do
automatic differentiation for native R functions.

Installation

Julia is needed to use autodiffr. You can
download a generic Julia binary from
https://julialang.org/downloads and add it to the path. Pakcage
autodiffr is not on CRAN yet. You can get the development version of
autodiffr by

  1. devtools::install_github("Non-Contradiction/autodiffr")

Important: Note that currently Julia v0.6.x, v0.7.0 and v1.0 are
all supported by autodiffr, but to use autodiffr with Julia
v0.7/1.0, you need to get the development version of JuliaCall by:

  1. devtools::install_github("Non-Contradiction/JuliaCall")

Basic Usage

  1. library(autodiffr)
  2. ## Do initial setup
  3. ad_setup()
  4. #> Julia version 1.0.0 at location /Applications/Julia-1.0.app/Contents/Resources/julia/bin will be used.
  5. #> Loading setup script for JuliaCall...
  6. #> Finish loading setup script for JuliaCall.
  7. ## If you want to use a julia at a specific location, you could do the following:
  8. ## ad_setup(JULIA_HOME = "the folder that contains julia binary"),
  9. ## or you can set JULIA_HOME in command line environment or use `options(...)`
  10. ## Define a target function with vector input and scalar output
  11. f <- function(x) sum(x^2L)
  12. ## Calculate gradient of f at [2,3] by
  13. ad_grad(f, c(2, 3)) ## deriv(f, c(2, 3))
  14. #> [1] 4 6
  15. ## Get a gradient function g
  16. g <- makeGradFunc(f)
  17. ## Evaluate the gradient function g at [2,3]
  18. g(c(2, 3))
  19. #> [1] 4 6
  20. ## Calculate hessian of f at [2,3] by
  21. ad_hessian(f, c(2, 3))
  22. #> [,1] [,2]
  23. #> [1,] 2 0
  24. #> [2,] 0 2
  25. ## Get a hessian function h
  26. h <- makeHessianFunc(f)
  27. ## Evaluate the hessian function h at [2,3]
  28. h(c(2, 3))
  29. #> [,1] [,2]
  30. #> [1,] 2 0
  31. #> [2,] 0 2
  32. ## Define a target function with vector input and vector output
  33. f <- function(x) x^2
  34. ## Calculate jacobian of f at [2,3] by
  35. ad_jacobian(f, c(2, 3))
  36. #> [,1] [,2]
  37. #> [1,] 4 0
  38. #> [2,] 0 6
  39. ## Get a jacobian function j
  40. j <- makeJacobianFunc(f)
  41. ## Evaluate the gradient function j at [2,3]
  42. j(c(2, 3))
  43. #> [,1] [,2]
  44. #> [1,] 4 0
  45. #> [2,] 0 6

Advanced Usage

Functions with Multiple Arguments

  1. ## Define a target function with mulitple arguments
  2. f <- function(a = 1, b = 2, c = 3) a * b ^ 2 * c ^ 3
  3. ## Calculate gradient/derivative of f at a = 2, when b = c = 1 by
  4. ad_grad(f, 2, b = 1, c = 1) ## deriv(f, 2, b = 1, c = 1)
  5. #> [1] 1
  6. ## Get a gradient/derivative function g w.r.t a when b = c = 1 by
  7. g <- makeGradFunc(f, b = 1, c = 1)
  8. ## Evaluate the gradient/derivative function g at a = 2
  9. g(2)
  10. #> [1] 1
  11. ## Calculate gradient/derivative of f at a = 2, b = 3, when c = 1 by
  12. ad_grad(f, list(a = 2, b = 3), c = 1)
  13. #> $a
  14. #> [1] 9
  15. #>
  16. #> $b
  17. #> [1] 12
  18. ## Get a gradient/derivative function g w.r.t a and b when c = 1 by
  19. g <- makeGradFunc(f, c = 1)
  20. ## Evaluate the gradient/derivative function g at a = 2, b = 3
  21. g(list(a = 2, b = 3))
  22. #> $a
  23. #> [1] 9
  24. #>
  25. #> $b
  26. #> [1] 12

Trouble Shooting and Way to Get Help

Julia is not found

Make sure the Julia installation is correct. autodiffr is able to
find Julia on PATH, and there are three ways for autodiffr to find
Julia not on PATH.

  • Use ad_setup(JULIA_HOME = "the folder that contains julia binary")
  • Use options(JULIA_HOME = "the folder that contains julia binary")
  • Set JULIA_HOME in command line environment.

How to Get Help

Suggestion and Issue Reporting

autodiffr is under active development now. Any suggestion or issue
reporting is welcome! You may report it using the link:
https://github.com/Non-Contradiction/autodiffr/issues/new. Or email me
at lch34677@gmail.com or cxl508@psu.edu.

Acknowledgement

The project autodiffr was a Google Summer of Code (GSoC) 2018 project
for the “R Project for statistical computing” and with mentors John Nash
and Hans W Borchers. Thanks a lot!