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

MethodError: no method matching logdensity_and_gradient #19

Open
DominiqueMakowski opened this issue Aug 31, 2024 · 1 comment
Open

Comments

@DominiqueMakowski
Copy link

I'm trying to use MCHMC with a fairly simple (?) model, but it errors with the following:

using CSV
using DataFrames
using Random, Turing
using MicroCanonicalHMC

@model function model_Gaussian(rt)

    # Priors
    μ_intercept ~ Normal(0.3, 0.5)
    σ_intercept ~ Normal(log(0.2), 3) 

    for i in 1:length(rt)
        μ = μ_intercept 
        σ = σ_intercept
        rt[i] ~ Normal(μ, exp(σ))
    end
end

fit = model_Gaussian(rand(100))

posteriors = sample(fit, NUTS(), 200)  # Works
posteriors = sample(fit, externalsampler(MCHMC(200, 0.01; adaptive=true)), 200)
ERROR: MethodError: no method matching logdensity_and_gradient(::LogDensityFunction{…}, ::Vector{…})

Closest candidates are:
  logdensity_and_gradient(::LogDensityProblemsADTrackerExt.TrackerGradientLogDensity, ::AbstractVector{T}) where T
   @ LogDensityProblemsADTrackerExt C:\Users\domma\.julia\packages\LogDensityProblemsAD\rBlLq\ext\LogDensityProblemsADTrackerExt.jl:36
  logdensity_and_gradient(::LogDensityProblemsADForwardDiffExt.ForwardDiffLogDensity, ::AbstractVector)
   @ LogDensityProblemsADForwardDiffExt C:\Users\domma\.julia\packages\LogDensityProblemsAD\rBlLq\ext\LogDensityProblemsADForwardDiffExt.jl:111

Stacktrace:
  [1] (::MicroCanonicalHMC.var"#∂lπ∂x#2"{LogDensityFunction{…}})(x::Vector{Float64})
    @ MicroCanonicalHMC C:\Users\domma\.julia\packages\MicroCanonicalHMC\Q9s3C\src\hamiltonian.jl:8
  [2] Step(rng::TaskLocalRNG, sampler::MicroCanonicalHMC.MCHMCSampler, h::MicroCanonicalHMC.Hamiltonian, x::Vector{…}; inv_transform::Function, kwargs::@Kwargs{})
    @ MicroCanonicalHMC C:\Users\domma\.julia\packages\MicroCanonicalHMC\Q9s3C\src\sampler.jl:140
  [3] Step
    @ C:\Users\domma\.julia\packages\MicroCanonicalHMC\Q9s3C\src\sampler.jl:130 [inlined]
  [4] #step#27
    @ C:\Users\domma\.julia\packages\MicroCanonicalHMC\Q9s3C\src\abstractmcmc.jl:14 [inlined]
  [5] step(rng::TaskLocalRNG, model::AbstractMCMC.LogDensityModel{LogDensityFunction{…}}, spl::MicroCanonicalHMC.MCHMCSampler)
    @ MicroCanonicalHMC C:\Users\domma\.julia\packages\MicroCanonicalHMC\Q9s3C\src\abstractmcmc.jl:1
  [6] step(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, sampler_wrapper::DynamicPPL.Sampler{…}; kwargs::@Kwargs{})
    @ Turing.Inference C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\abstractmcmc.jl:46
  [7] step
    @ C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\abstractmcmc.jl:30 [inlined]
  [8] macro expansion
    @ C:\Users\domma\.julia\packages\AbstractMCMC\YrmkI\src\sample.jl:130 [inlined]
  [9] macro expansion
    @ C:\Users\domma\.julia\packages\ProgressLogging\6KXlp\src\ProgressLogging.jl:328 [inlined]
 [10] macro expansion
    @ C:\Users\domma\.julia\packages\AbstractMCMC\YrmkI\src\logging.jl:9 [inlined]
 [11] mcmcsample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, N::Int64; progress::Bool, progressname::String, callback::Nothing, discard_initial::Int64, thinning::Int64, chain_type::Type, initial_state::Nothing, kwargs::@Kwargs{})
    @ AbstractMCMC C:\Users\domma\.julia\packages\AbstractMCMC\YrmkI\src\sample.jl:120
 [12] sample(rng::TaskLocalRNG, model::DynamicPPL.Model{…}, sampler::DynamicPPL.Sampler{…}, N::Int64; chain_type::Type, resume_from::Nothing, initial_state::Nothing, kwargs::@Kwargs{})
    @ DynamicPPL C:\Users\domma\.julia\packages\DynamicPPL\rXg4T\src\sampler.jl:93
 [13] sample
    @ C:\Users\domma\.julia\packages\DynamicPPL\rXg4T\src\sampler.jl:83 [inlined]
 [14] #sample#3
    @ C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\Inference.jl:219 [inlined]
 [15] sample
    @ C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\Inference.jl:212 [inlined]
 [16] #sample#2
    @ C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\Inference.jl:209 [inlined]
 [17] sample(model::DynamicPPL.Model{…}, alg::Turing.Inference.ExternalSampler{…}, N::Int64)
    @ Turing.Inference C:\Users\domma\.julia\packages\Turing\hbbFY\src\mcmc\Inference.jl:203
 [18] top-level scope
    @ Untitled-1:22
Some type information was truncated. Use `show(err)` to see complete types.

Am I missing something obvious? thanks!

@JaimeRZP
Copy link
Owner

Hi! This is weird indeed. I will have a look. Maybe something changed in Turing and I need to updated the interface here. Try running the code above using the project.toml in the examples folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants