From 1f03f1562c403c883e2576826454a6c54e6bc919 Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Tue, 26 Jul 2016 19:28:36 -0400 Subject: [PATCH] update to ForwardDiff 0.2.x --- REQUIRE | 2 +- src/elbo_kl.jl | 21 +++++---------- test/derivative_utils.jl | 58 +++++++++++++++++++--------------------- test/test_derivatives.jl | 8 +++--- 4 files changed, 39 insertions(+), 50 deletions(-) diff --git a/REQUIRE b/REQUIRE index c5925c0b..ddd61ebe 100644 --- a/REQUIRE +++ b/REQUIRE @@ -3,7 +3,7 @@ Compat DataFrames Distributions DocOpt -ForwardDiff 0.1.8 0.2 +ForwardDiff 0.2 JLD Lumberjack Optim 0.5 0.5.1 diff --git a/src/elbo_kl.jl b/src/elbo_kl.jl index 1ad2d2bf..4aaa7464 100644 --- a/src/elbo_kl.jl +++ b/src/elbo_kl.jl @@ -173,23 +173,16 @@ function subtract_kl!{NumType <: Number}( if calculate_derivs if calculate_hessian - hess, all_results = - ForwardDiff.hessian(subtract_kl_value_wrapper, - vp_vec, - ForwardDiff.AllResults) - accum.h += hess - accum.d += reshape(ForwardDiff.gradient(all_results), P, Sa) - accum.v[1] += ForwardDiff.value(all_results) + result = ForwardDiff.HessianResult(vp_vec) + ForwardDiff.hessian!(result, subtract_kl_value_wrapper, vp_vec) + accum.h += ForwardDiff.hessian(result) else - grad, all_results = - ForwardDiff.gradient(subtract_kl_value_wrapper, - vp_vec, - ForwardDiff.AllResults) - accum.d += reshape(grad, P, Sa) - accum.v[1] += ForwardDiff.value(all_results) + result = ForwardDiff.GradientResult(vp_vec) + ForwardDiff.gradient!(result, subtract_kl_value_wrapper, vp_vec) end + accum.d += reshape(ForwardDiff.gradient(result), P, Sa) + accum.v[1] += ForwardDiff.value(result) else accum.v[1] += subtract_kl_value_wrapper(vp_vec) end end - diff --git a/test/derivative_utils.jl b/test/derivative_utils.jl index 53723c14..c09d8326 100644 --- a/test/derivative_utils.jl +++ b/test/derivative_utils.jl @@ -1,39 +1,36 @@ import Base.convert - -function convert(FDType::Type{ForwardDiff.GradientNumber}, - ea::ElboArgs{Float64}) - x = ea.vp[1] - P = length(x) - FDType = ForwardDiff.GradientNumber{length(ea.vp[1]), Float64} - - fd_x = [ ForwardDiff.GradientNumber(x[i], zeros(Float64, P)...) for i=1:P ] - convert(FDType, x[1]) - - vp_fd = convert(Array{Array{FDType, 1}, 1}, ea.vp[1]) - ea_fd = ElboArgs(vp_fd) -end - -function convert(FDType::Type{ForwardDiff.HessianNumber}, - ea::ElboArgs{Float64}) - x = ea.vp[1] - P = length(x) - FDType = ForwardDiff.HessianNumber{length(ea.vp[1]), Float64} - - fd_x = [ ForwardDiff.HessianNumber(x[i], zeros(Float64, P)...) for i=1:P ] - convert(FDType, x[1]) - - vp_fd = convert(Array{Array{FDType, 1}, 1}, ea.vp[1]) - ea_fd = ElboArgs(vp_fd) -end +# function convert(FDType::Type{ForwardDiff.GradientNumber}, +# ea::ElboArgs{Float64}) +# x = ea.vp[1] +# P = length(x) +# FDType = ForwardDiff.GradientNumber{length(ea.vp[1]), Float64} +# +# fd_x = [ ForwardDiff.GradientNumber(x[i], zeros(Float64, P)...) for i=1:P ] +# convert(FDType, x[1]) +# +# vp_fd = convert(Array{Array{FDType, 1}, 1}, ea.vp[1]) +# ea_fd = ElboArgs(vp_fd) +# end +# +# function convert(FDType::Type{ForwardDiff.HessianNumber}, +# ea::ElboArgs{Float64}) +# x = ea.vp[1] +# P = length(x) +# FDType = ForwardDiff.HessianNumber{length(ea.vp[1]), Float64} +# +# fd_x = [ ForwardDiff.HessianNumber(x[i], zeros(Float64, P)...) for i=1:P ] +# convert(FDType, x[1]) +# +# vp_fd = convert(Array{Array{FDType, 1}, 1}, ea.vp[1]) +# ea_fd = ElboArgs(vp_fd) +# end # Maybe write it as a convert()? -function forward_diff_model_params{T <: Number}( - FDType::Type{T}, - ea0::ElboArgs{Float64}) +function forward_diff_model_params{T<:Number}(::Type{T}, ea0::ElboArgs{Float64}) P = length(ea0.vp[1]) - vp = Vector{FDType}[zeros(FDType, P) for s=1:ea0.S] + vp = Vector{T}[zeros(T, P) for s=1:ea0.S] # Set the values (but not gradient numbers) for parameters other # than the galaxy parameters. for s=1:ea0.S, i=1:length(ids) @@ -46,4 +43,3 @@ function forward_diff_model_params{T <: Number}( ea0.patches, ea0.active_sources) end - diff --git a/test/test_derivatives.jl b/test/test_derivatives.jl index c4827f42..ec7d9227 100644 --- a/test/test_derivatives.jl +++ b/test/test_derivatives.jl @@ -823,12 +823,12 @@ function test_galaxy_cache_component() f_wrap(par)) # Check the gradient. - ad_grad_fun = ForwardDiff.gradient(f_wrap) + ad_grad_fun = x -> ForwardDiff.gradient(f_wrap, x) ad_grad = ad_grad_fun(par) bvn_derivs = elbo_vars.bvn_derivs @test_approx_eq ad_grad [bvn_derivs.bvn_u_d; bvn_derivs.bvn_s_d] - ad_hess_fun = ForwardDiff.hessian(f_wrap) + ad_hess_fun = x -> ForwardDiff.hessian(f_wrap, x) ad_hess = ad_hess_fun(par) @test_approx_eq ad_hess[1:2, 1:2] bvn_derivs.bvn_uu_h @@ -871,11 +871,11 @@ function test_galaxy_sigma_derivs() gal_derivs = ElboDeriv.GalaxySigmaDerivs(e_angle, e_axis, e_scale, XiXi) - ad_grad_fun = ForwardDiff.gradient(f_wrap) + ad_grad_fun = x -> ForwardDiff.gradient(f_wrap, x) ad_grad = ad_grad_fun(par) @test_approx_eq gal_derivs.j[si, :][:] ad_grad - ad_hess_fun = ForwardDiff.hessian(f_wrap) + ad_hess_fun = x -> ForwardDiff.hessian(f_wrap, x) ad_hess = ad_hess_fun(par) @test_approx_eq( ad_hess,