-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Do not use `include` in `__init__` * Fix error on Julia 1.0 * Fix a seed * Discard initial samples * Increase number of samples * Move ad.jl to contrib subfolder * Update test/sampler.jl * Update sampler-vec.jl * Decrease leapfrog stepsize for some samplers. * Decrease leapfrog stepsize for some samplers. * Slightly relax test error bounds. Co-authored-by: Hong Ge <[email protected]>
- Loading branch information
Showing
8 changed files
with
97 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import .ForwardDiff, .ForwardDiff.DiffResults | ||
|
||
function ∂ℓπ∂θ_forwarddiff(ℓπ, θ::AbstractVector) | ||
res = DiffResults.GradientResult(θ) | ||
ForwardDiff.gradient!(res, ℓπ, θ) | ||
return DiffResults.value(res), DiffResults.gradient(res) | ||
end | ||
|
||
# Implementation 1 | ||
function ∂ℓπ∂θ_forwarddiff(ℓπ, θ::AbstractMatrix) | ||
jacob = similar(θ) | ||
res = DiffResults.JacobianResult(similar(θ, size(θ, 2)), jacob) | ||
ForwardDiff.jacobian!(res, ℓπ, θ) | ||
jacob_full = DiffResults.jacobian(res) | ||
|
||
d, n = size(jacob) | ||
for i in 1:n | ||
jacob[:,i] = jacob_full[i,1+(i-1)*d:i*d] | ||
end | ||
return DiffResults.value(res), jacob | ||
end | ||
|
||
# Implementation 2 | ||
# function ∂ℓπ∂θ_forwarddiff(ℓπ, θ::AbstractMatrix) | ||
# local densities | ||
# f(x) = (densities = ℓπ(x); sum(densities)) | ||
# res = DiffResults.GradientResult(θ) | ||
# ForwardDiff.gradient!(res, f, θ) | ||
# return ForwardDiff.value.(densities), DiffResults.gradient(res) | ||
# end | ||
|
||
# Implementation 3 | ||
# function ∂ℓπ∂θ_forwarddiff(ℓπ, θ::AbstractMatrix) | ||
# v = similar(θ, size(θ, 2)) | ||
# g = similar(θ) | ||
# for i in 1:size(θ, 2) | ||
# res = GradientResult(θ[:,i]) | ||
# gradient!(res, ℓπ, θ[:,i]) | ||
# v[i] = value(res) | ||
# g[:,i] = gradient(res) | ||
# end | ||
# return v, g | ||
# end | ||
|
||
function ForwardDiffHamiltonian(metric::AbstractMetric, ℓπ) | ||
∂ℓπ∂θ(θ::AbstractVecOrMat) = ∂ℓπ∂θ_forwarddiff(ℓπ, θ) | ||
return Hamiltonian(metric, ℓπ, ∂ℓπ∂θ) | ||
end | ||
|
||
ADAVAILABLE[ForwardDiff] = ForwardDiffHamiltonian |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import .Zygote | ||
|
||
function ∂ℓπ∂θ_zygote(ℓπ, θ::AbstractVector) | ||
res, back = Zygote.pullback(ℓπ, θ) | ||
return res, first(back(Zygote.sensitivity(res))) | ||
end | ||
|
||
function ∂ℓπ∂θ_zygote(ℓπ, θ::AbstractMatrix) | ||
res, back = Zygote.pullback(ℓπ, θ) | ||
return res, first(back(ones(eltype(res), size(res)))) | ||
end | ||
|
||
function ZygoteADHamiltonian(metric::AbstractMetric, ℓπ) | ||
∂ℓπ∂θ(θ::AbstractVecOrMat) = ∂ℓπ∂θ_zygote(ℓπ, θ) | ||
return Hamiltonian(metric, ℓπ, ∂ℓπ∂θ) | ||
end | ||
|
||
ADAVAILABLE[Zygote] = ZygoteADHamiltonian |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters