Controlling how inference is performedYou can get fine-grained control of how inference is performed by getting hold of a compiled algorithm object. The following code shows, for our simple Gaussian example, how to do this. One thing to note is that you should pass down all the variables you are interested in inferring to this method call; in this case mean and precision. Recall that compilation typically takes place when you asked for a marginal on one of your variables; instead, this call directly requests a compilation, thus obviating the need for a compilation when you ask for the marginals.
Once you have a reference to the compiled algorithm, you can explicitly control the initialisation and updates of the algorithm. This allows, for example, implementation of a custom convergence criterion by monitoring the level of change in certain marginals of interest. The CompiledAlgorithm object that you get is the same that you would get if you precompiled your inference algorithm. The following code snippets illustrate all the ways in which you can use the compiled algorithm object: ResetThis method allocates all resources needed before performing inference. It should always be called if you make changes which might modify the resources needed.
Initialise and UpdateThe initialise method must be called before running the updates. It performs any computations which can be done before iterating over the 'schedule'. Each update is an entire single pass through the schedule which updates all variable's backward and forward messages.
ExecuteThe execute method provides a short-cut for performing an initialisation followed by a series of updates. The execute method allows the number of updates to be passed in as a parameter - if this is omitted the default number of update iterations is used. The following code snippet sets the number of iterations to 10 and is equivalent to the previous code snippet.
Setting observed valuesWhen using a compiled algorithm directly, you must use the SetObservedValue() method to ensure that the compiled algorithm is aware of the latest value of an observed variable. First, set the ObservedValue property on the variable itself, then call SetObservedValue(observation) on the CompiledAlgorithm instance. Finally, if the size of a variable array has changed, call Reset() on the CompiledAlgorithm instance in order to reallocate resources. See below for an example. If the Variable instance is no longer around, for example, if you are using a precompiled inference algorithm, you will need to use the version of SetObservedValue() which takes a string. Getting marginalsWhen using a compiled algorithm directly, you use the Marginal() method to retrieve marginal distributions (see below for an example). This can be done at any time, for example, so that the change in the marginal distribution during inference can be monitored to check for convergence. As with setting observed values, if the Variable instances are not available, you will need to use the version of Marginal() which takes a string. ExampleThe following example illustrates these mechanisms by extending the simple Gaussian example in several ways. The purpose of this example is to perform inference using the same model for two different data sets, whilst avoiding compilation of the model between the two calls. Firstly, the observed data changes for each call of the algorithm. We still create data as a variable array which is observed; however we defer setting what those observed values are until we are ready to call the algorithm; Note that we do need to set an initial dummy value to let the algorithm know that data is observed (in the code below it is just given a null value). Because the length of data is now variable, we introduce a dataCount variable to indicate its length. Again, we will set the observed value of this variable when we are ready to call the inference, but we initialise it with a dummy value of 0. The call to GetCompiledInferenceAlgorithm compiles the model and returns a CompiledAlgorihm object which can be then used to perform inference any number of times. The ObservedValue properties for data and dataCount are set in the body of the for loop; in addition, the CompiledAlgorithm instance needs to be informed that these have changed by calling its SetObservedValue method. The inference can then be performed by calling the Reset and Execute methods. You can verify that compilation only occurs once by setting the BrowserMode to Always - you will see that the browser window only pops up once - indicating a single compilation.
The output from the inference should be:
|


