probcomp / Gen.jl

A general-purpose probabilistic programming system with programmable inference
https://gen.dev
Apache License 2.0
1.8k stars 160 forks source link

Speeding up "update" for custom inference #271

Open Kenta426 opened 4 years ago

Kenta426 commented 4 years ago

I have seen several issues regarding this topic such as #182. What is the current status? I do find update run a bit slow for my custom inference algorithm. (When I comment out update, it runs pretty fast). Also is there anyway I can update multiple trace addresses in a single update call?

alex-lew commented 4 years ago

Hi there,

Is there anyway I can update multiple trace addresses in a single update call?

Yes: just call update with a choicemap containing everything you want to change.

For example,

update(trace, args, argdiffs, choicemap(:x => 2, :y => "hello", (:z => 1) => 5.4))

will change :x, :y, and :z => 1 in one call.

I do find update run a bit slow for my custom inference algorithm.

Can you say a little bit more about your model and inference algorithm? Many custom inference algorithms should be possible to write without directly calling update (but some do require it), so it'd be good to have a better sense for what you're trying to do. In general, you can make update (and other operations) run faster by using the Static Modeling Language and Combinators to express all or part of your model.

Kenta426 commented 4 years ago

Hi Alex, Thanks for answering about updating multiple trace addresses at once.

I am implementing the infinite mixture of GLMs (Dirichlet Process Mixtures of Generalized Linear Models, Hannah et al) and there is an R library (https://rdrr.io/github/stablemarkets/ChiRP/man/fDPMix.html).

I have a slow but working version in Gen with Neal's Gibbs sampling (Markov Chain Sampling Methods for Dirichlet Process Mixture Models, Neal et al), and I need to keep track of parameters indexed by cluster assignments. I use update to account for label swapping after each sampling step.

Also, I wrote DP with Chinese Restaurant Process realization, and I use for loop in the generative function, which is not supported by static modeling syntax. Otherwise I tried to use (static) and combinators as much as I can. Perhaps, I could use stick-breaking realization so I can wrap this inside of static modeling syntax.

By the way, the goal is to implement the mixture of experts where each expert is a nonparametric model such as GP (An Alternative Infinite Mixture Of Gaussian Process Experts, Meeds et al)