Hi all,
As some sort of continuation of Recommended way to extract logprob and build gradients in Turing.jl?
I wish to extract the log probability density from a Turing model (to use with Pathfider.jl for example),
and I encountered the LogDensityFunction implementation of DynamicPPL, which is
f = LogDensityFunction(model)
This reexports the model within the LogDensityProblems interface.
However, is it possible to transform the model variables to unconstrained Euclidean space beforehand, so that MCMC can be performed without manually transforming each parameter using TransformVariables.jl?
Iβve stumbled upon the link!! function from DynamicPPL, but Iβve got no clue how to implement it.
You can use Turing directly with Pathfinder, similar to the examples in Turing usage Β· Pathfinder.jl
But this should also work for you (adapted from https://github.com/TuringLang/Turing.jl/blob/4affc28b341f4763bd1abc8523e4e209e9f6aa6e/src/contrib/inference/abstractmcmc.jl#L38-L48):
julia> using DynamicPPL, LogDensityProblems, LogDensityProblemsAD, ForwardDiff, Distributions
julia> @model function foo()
x ~ Gamma()
end;
julia> model = foo();
julia> β = DynamicPPL.LogDensityFunction(model);
julia> LogDensityProblems.logdensity(β, [-10.0]) # constrained
-Inf
julia> DynamicPPL.link!!(β.varinfo, model);
julia> LogDensityProblems.logdensity(β, [-10.0]) # unconstrained
-10.000045399929762
julia> β_with_grad = LogDensityProblemsAD.ADgradient(Val(:ForwardDiff), β);
julia> using Pathfinder
julia> pfr = pathfinder(β_with_grad);
Thanks! works like a charm 