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

Add concretize before generating Chains #1180

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Release 0.10.1
- Fix bug where arrays with mixed integers, floats, and missing values were not being passed to the `MCMCChains.Chains` constructor properly [#1180](https://github.com/TuringLang/Turing.jl/pull/1180).

# Release 0.10.0
- Update elliptical slice sampling to use [EllipticalSliceSampling.jl](https://github.com/TuringLang/EllipticalSliceSampling.jl) on the backend. [#1145](https://github.com/TuringLang/Turing.jl/pull/1145). Nothing should change from a front-end perspective -- you can still call `sample(model, ESS(), 1000)`.
- Added default progress loggers in [#1149](https://github.com/TuringLang/Turing.jl/pull/1149).
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ EllipticalSliceSampling = "0.2"
ForwardDiff = "0.10.3"
Libtask = "0.3.1"
LogDensityProblems = "^0.9, 0.10"
MCMCChains = "3"
MCMCChains = "3.0.7"
MacroTools = "0.5.1"
PDMats = "0.9.9"
ProgressLogging = "0.1"
Expand Down
4 changes: 2 additions & 2 deletions src/contrib/inference/dynamichmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ end
model::AbstractModel,
alg::DynamicNUTS,
N::Integer;
chain_type=Chains,
chain_type=MCMCChains.Chains,
resume_from=nothing,
progress=PROGRESS[],
kwargs...
Expand All @@ -136,7 +136,7 @@ function AbstractMCMC.psample(
alg::DynamicNUTS,
N::Integer,
n_chains::Integer;
chain_type=Chains,
chain_type=MCMCChains.Chains,
progress=PROGRESS[],
kwargs...
)
Expand Down
4 changes: 2 additions & 2 deletions src/inference/AdvancedSMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ function AbstractMCMC.sample_end!(
loge = mean(t.le for t in ts)

# If we already had a chain, grab the logevidence.
if resume_from isa Chains
if resume_from isa MCMCChains.Chains
# pushfirst!(samples, resume_from.info[:samples]...)
pre_loge = resume_from.logevidence
# Calculate new log-evidence
pre_n = length(resume_from)
loge = (pre_loge * pre_n + loge * N) / (pre_n + N)
elseif resume_from !== nothing
error("keyword argument `resume_from` has to be `nothing` or a `Chains` object")
error("keyword argument `resume_from` has to be `nothing` or a `MCMCChains.Chains` object")
end

# Store the logevidence.
Expand Down
23 changes: 13 additions & 10 deletions src/inference/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ using Random: GLOBAL_RNG, AbstractRNG, randexp
using DynamicPPL
using AbstractMCMC: AbstractModel, AbstractSampler
using Bijectors: _debug
using MCMCChains: Chains

import AbstractMCMC
import AdvancedHMC; const AHMC = AdvancedHMC
Expand All @@ -26,6 +25,7 @@ import DynamicPPL: getspace, get_matching_type,
VarName, _getranges, _getindex, getval, _getvns
import EllipticalSliceSampling
import Random
import MCMCChains

export InferenceAlgorithm,
Hamiltonian,
Expand Down Expand Up @@ -146,7 +146,7 @@ function AbstractMCMC.sample(
model::AbstractModel,
alg::InferenceAlgorithm,
N::Integer;
chain_type=Chains,
chain_type=MCMCChains.Chains,
resume_from=nothing,
progress=PROGRESS[],
kwargs...
Expand Down Expand Up @@ -175,7 +175,7 @@ function AbstractMCMC.psample(
alg::InferenceAlgorithm,
N::Integer,
n_chains::Integer;
chain_type=Chains,
chain_type=MCMCChains.Chains,
progress=PROGRESS[],
kwargs...
)
Expand Down Expand Up @@ -310,14 +310,14 @@ function get_transition_extras(ts::Vector)
return extra_names, valmat
end

# Default Chains constructor.
# Default MCMCChains.Chains constructor.
function AbstractMCMC.bundle_samples(
rng::AbstractRNG,
model::Model,
spl::Sampler,
N::Integer,
ts::Vector,
chain_type::Type{Chains};
chain_type::Type{MCMCChains.Chains};
discard_adapt::Bool=true,
save_state=false,
kwargs...
Expand Down Expand Up @@ -358,8 +358,11 @@ function AbstractMCMC.bundle_samples(
info = NamedTuple()
end

# Conretize the array before giving it to MCMCChains.
parray = MCMCChains.concretize(parray)

# Chain construction.
return Chains(
return MCMCChains.Chains(
parray,
string.(nms),
deepcopy(TURING_INTERNAL_VARS);
Expand Down Expand Up @@ -398,12 +401,12 @@ function AbstractMCMC.bundle_samples(
return map(identity, nts)
end

function save(c::Chains, spl::Sampler, model, vi, samples)
function save(c::MCMCChains.Chains, spl::Sampler, model, vi, samples)
nt = NamedTuple{(:spl, :model, :vi, :samples)}((spl, model, deepcopy(vi), samples))
return setinfo(c, merge(nt, c.info))
end

function resume(c::Chains, n_iter::Int; chain_type=Chains, progress=PROGRESS[], kwargs...)
function resume(c::MCMCChains.Chains, n_iter::Int; chain_type=MCMCChains.Chains, progress=PROGRESS[], kwargs...)
@assert !isempty(c.info) "[Turing] cannot resume from a chain without state info"

# Sample a new chain.
Expand All @@ -414,7 +417,7 @@ function resume(c::Chains, n_iter::Int; chain_type=Chains, progress=PROGRESS[],
n_iter;
resume_from=c,
reuse_spl_n=n_iter,
chain_type=Chains,
chain_type=MCMCChains.Chains,
progress=progress,
kwargs...
)
Expand All @@ -425,7 +428,7 @@ end

function set_resume!(
s::Sampler;
resume_from::Union{Chains, Nothing}=nothing,
resume_from::Union{MCMCChains.Chains, Nothing}=nothing,
kwargs...
)
# If we're resuming, grab the sampler info.
Expand Down
1 change: 0 additions & 1 deletion src/utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using DynamicPPL: AbstractSampler, Sampler
using DynamicPPL: init, inittrans, reconstruct, reconstruct!, vectorize
using Distributions, Bijectors
using StatsFuns, SpecialFunctions
using MCMCChains: Chains, setinfo
import Distributions: sample

export vectorize,
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ include("test_utils/AllUtils.jl")
Turing.setadbackend(:forwarddiff)
@testset "stdlib" begin
include("stdlib/distributions.jl")
# include("stdlib/RandomMeasures.jl")
include("stdlib/RandomMeasures.jl")
end

@testset "utilities" begin
Expand Down
Loading