Infer.NET user guide : Controlling how inference is performed
Structure of generated inference code
Infer.NET generates a C# class for performing inference on your model. By default, the source of this class is placed at: (Debug/Release)\bin\GeneratedSource\[ModelName].cs
. There are circumstances where it is useful to use this generated code directly, for example, to use a precompiled inference algorithm or to manually modify the message-passing schedule or some other aspect of the code. To help with this, we will now describe the structure of the generated class. Generated source code files also contain full documentation for the class, fields and methods so you can also refer to these directly. To make the code readable, you should attach names to variables in the model.
The Generated Class
The class is an ordinary C# class of name [ModelName]_[Algorithm]
, for example Model0_EP
. It typically contains a number of public fields, properties, and methods. A property is created to hold each observed variable in the model. Fields are created for:
- all requested marginals
- all forward and backwards messages needed to compute requested marginals
(
x_uses_F[i]
is a message from a variable x to its ith use, whilex_uses_B[i]
is a message to a variable x from its ith use. A gate block counts as a single use.) - invalidation flags to track which parts of the schedule need to be recomputed when observed values change
Methods are created for:
-
Setting observed values and initialising messages
-
Performing inference by updating messages
-
Retrieving posterior marginals and output messages
Each field and method has a documentation comment describing its purpose.
Generated Methods for Performing Inference
The methods in the table below are used when performing inference. The Reset, Execute and Update methods can all be called through the IGeneratedAlgorithm interface.
Method | Purpose |
---|---|
Reset() |
Resets all messages to their initial values. |
Execute() |
Execute the inference algorithm for the specified number of iterations, starting from the initial state. |
Update() |
Performs additional iterations of inference from the current state. |
[Variable]Marginal() |
Returns the current marginal for a variable. This method can be called at any time to return the current estimate of the marginal distribution, as given by the current state of the inference algorithm. |
[Variable]MarginalDividedByPrior() |
Returns the current output message for a variable. This method is similar to [Variable]Marginal() but returns the ‘output message’ which is the current marginal for the variable divided by the prior. This message is very useful when models are being combined in a modular fashion and is the mechanism used when sharing variables between models. These methods are only generated for variables that are marked with a QueryType attribute. |