Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No glue code #319

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 317 additions & 0 deletions Lab.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.4.4"
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
InplaceOps = "505f98c9-085e-5b2c-8e89-488be7bf1f34"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
Expand Down
4 changes: 4 additions & 0 deletions src/AdvancedHMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using LogDensityProblemsAD: LogDensityProblemsAD

import AbstractMCMC
using AbstractMCMC: LogDensityModel
using DynamicPPL

import StatsBase: sample

Expand Down Expand Up @@ -222,6 +223,9 @@ function Hamiltonian(metric::AbstractMetric, ℓπ, kind::Union{Symbol,Val}; kwa
return Hamiltonian(metric, ℓ)
end

### Turing Interface
include("turing_utils.jl")

### Init

using Requires
Expand Down
62 changes: 62 additions & 0 deletions src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,68 @@ sample(
(pm_next!) = pm_next!,
)

###
# Allows to pass Turing model to build Hamiltonian

function sample(
model::DynamicPPL.Model,
metric::AbstractMetric,
κ::AbstractMCMCKernel,
θ::AbstractVecOrMat{<:AbstractFloat},
n_samples::Int,
adaptor::AbstractAdaptor = NoAdaptation(),
n_adapts::Int = min(div(n_samples, 10), 1_000);
drop_warmup = false,
verbose::Bool = true,
progress::Bool = false,
(pm_next!)::Function = pm_next!,
)
ctxt = model.context
vi = DynamicPPL.VarInfo(model, ctxt)

# We will need to implement this but it is going to be
# Interesting how to plug the transforms along the sampling
# processes

#vi_t = Turing.link!!(vi, model)

ℓ = LogDensityProblemsAD.ADgradient(DynamicPPL.LogDensityFunction(vi, model, ctxt))
h = Hamiltonian(metric, ℓ)
return sample(
GLOBAL_RNG,
h,
κ,
θ,
n_samples,
adaptor,
n_adapts;
drop_warmup = drop_warmup,
verbose = verbose,
progress = progress,
(pm_next!) = pm_next!,
)
end

function sample(model::DynamicPPL.Model, ϵ::Number, TAP::Number, n_samples::Int, n_adapts::Int;
initial_θ=initial_θ, progress=true, kwargs...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function sample(model::DynamicPPL.Model, ϵ::Number, TAP::Number, n_samples::Int, n_adapts::Int;
initial_θ=initial_θ, progress=true, kwargs...)
function sample(
model::DynamicPPL.Model,
ϵ::Number,
TAP::Number,
n_samples::Int,
n_adapts::Int;
initial_θ = initial_θ,
progress = true,
kwargs...,
)

ctxt = model.context
vi = VarInfo(model, ctxt)

dists = _get_dists(vi)
dist_lengths = [length(dist) for dist in dists]
vsyms = _name_variables(vi, dist_lengths)
d = length(vsyms)

metric = DiagEuclideanMetric(d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
metric = DiagEuclideanMetric(d)
metric = DiagEuclideanMetric(d)

integrator = Leapfrog(ϵ)
proposal = NUTS{MultinomialTS, GeneralisedNoUTurn}(integrator)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
proposal = NUTS{MultinomialTS, GeneralisedNoUTurn}(integrator)
proposal = NUTS{MultinomialTS,GeneralisedNoUTurn}(integrator)

adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(TAP, integrator))
return sample(model, metric, proposal, initial_θ, n_samples, adaptor, n_adapts;
progress=progress, kwargs...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
return sample(model, metric, proposal, initial_θ, n_samples, adaptor, n_adapts;
progress=progress, kwargs...)
return sample(
model,
metric,
proposal,
initial_θ,
n_samples,
adaptor,
n_adapts;
progress = progress,
kwargs...,
)

end

###

"""
sample(
rng::AbstractRNG,
Expand Down
19 changes: 19 additions & 0 deletions src/turing_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function _get_dists(vi::VarInfo)
mds = values(vi.metadata)
return [md.dists[1] for md in mds]
end

function _name_variables(vi::VarInfo, dist_lengths::AbstractVector)
vsyms = keys(vi)
names = []
for (vsym, dist_length) in zip(vsyms, dist_lengths)
if dist_length==1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
if dist_length==1
if dist_length == 1

name = [vsym]
append!(names, name)
else
name = [DynamicPPL.VarName(Symbol(vsym, i,)) for i in 1:dist_length]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
name = [DynamicPPL.VarName(Symbol(vsym, i,)) for i in 1:dist_length]
name = [DynamicPPL.VarName(Symbol(vsym, i)) for i = 1:dist_length]

append!(names, name)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

end
return names
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end