Running Inference (after adding findings) without re-compiling the network all over again
-
Wednesday, 28 March 2012 4:23 PM
Hi,
I'm currently experimenting a bit with Infer.Net, trying to use it for Bayesian networks (very much in the same way as the 'Sprinkler, Cloudy, Rain'-example that can be found in the tutorials).
I have a question regarding when the network decides to compile the Model. I assume that the ideal way is to: build a network, compile it once, and then run inference over it numerous times.
When I look at the messages (Engine.ShowProgress) I see that after I add a new ObservedValue to one of my Variables, and perform Inference, Infer.Net re-compiles the model (before running Inference).
When you look at the example that can be found in the tutorials ('Sprinkler, Cloudy, Rain'-example), and turn on 'ShowProgress, Infer.Net re-compiles the model for every new query of Inference.
For example the output of the first two queries, is like this:
Am I wrong in thinking that compiling the model should only happen once?
Thank you in advance for your help.
With regard,
Thomas
All Replies
-
Wednesday, 28 March 2012 4:36 PM
Maybe a better question should be: Can I determine the posteriors of the network after adding Observed Values.
I understand that I would have to run Inference, but don't really agree on re-compiling the whole network again.
I've tried to do this in the following ways (both re compile the model):
// Adding observed values: ... myVariable1.ObservedValue = myObservedValue1; myVariable2.ObservedValue = myObservedValue2; ... // Running Inference - method 1: var myPosterior = Engine.Infer<Discrete[]>(myVariable1); Vector myProbs = myPosterior[0].GetProbs(); ... // Running Inference - method 2: var ca = Engine.GetCompiledInferenceAlgorithm(myVariable1); var myPosterior = ca.Marginal<Discrete[]>(myVariable1.NameInGeneratedCode);
Vector myProbs = myPosterior[0].GetProbs();
Thanks again.
With regards,
Thomas
- Edited by ThoFlam Wednesday, 28 March 2012 4:38 PM
-
Friday, 30 March 2012 5:37 PMOwner
A model is recompiled when the pattern of observations change, not when the observations themselves change. If you observe something one time, and not the next time, you are removing a constraint from the model, and the generated inference code to give the best approximation to the posterior must change also. To avoid recompilation, you should create a seperate instance of your model class for each pattern of observations. Then there will be one compilation for each pattern, but thereafter no more recompilations. In a typical simple situation, you have a model for which you (a) want to learn the parameters, then (b) fix the parameters and do predictions. In this case, create a model class that defines the model, along with methods to run (a) and (b), but then create two instances.
John
-
Wednesday, 4 April 2012 4:46 PM
Besides John Guiver's solution, another approach is to build the model to handle missing data. Then you do not have to recompile when the pattern of observations changes.