TuringLang / Turing.jl

Bayesian inference with probabilistic programming.
https://turinglang.org
MIT License
2.01k stars 217 forks source link

SMC samplers no longer support usage of keyword arguments in models #2007

Open torfjelde opened 1 year ago

torfjelde commented 1 year ago

After #2001 , sampling models with keyword argument using SMC samplers will no longer be supported.

This issue is a reminder + to keep track of the progress towards addressing this.

Related issues and PRs:

joelem commented 2 months ago

Hi Turing team,

First, just wanted to thank you for such an awesome package, we are using it for one of our projects. Specifically, we were using an SMC sampler that sampled a function we created that takes in a matrix as an input.

something like:

{code here that creates a matrix} result = sample(OurFunction(matrix), SMC(), 5000);

We successfully ran these analyses before this #2007 update. I recently revisited this part of our analysis pipeline and the sampling code does not work anymore (keyword argument error). I tried other samplers, but unsuccessfully.

Is there a different way that we should now be feeding data (like a matrix) into a sampled function? How might we solve this issue?

Thank you so much for any help! Joel

torfjelde commented 2 months ago

Hey! Glad to hear you like it:)

At the moment, I think the best approach is unfortunately to implement OurFunction without keyword arguments, or alternatively provide a wrapper around it. For example if we have the following model

@model function demo(arg1, arg2, kwargs::NamedTuple)
    kwarg1 = kwargs.kwarg1
    kwarg2 = kwargs.kwarg2
    x ~ Normal(kwarg1, kwarg2)
end

you can define another version by hand like this:

function demo(arg1, arg2; kwargs...)
    return demo(arg1, arg2, NamedTuple(kwargs))
end

Then you can continue using the method with keyword arguments, but it will all still work:)

Does that make sense?