Infer.NET user guide : Running inference

Inference engine settings

High-level inference settings are all accessed via properties or methods of an InferenceEngine object (in the MicrosoftResearch.Infer namespace). The settings take effect at the point of model compilation; typically this occurs when you request a marginal value from one of your variables, at which point both model compilation and inference occur. However, you can separate out compilation and inference as discussed in Controlling how inference is performed.
You can get an instance of the InferenceEngine class via the following snippet:

InferenceEngine engine = new InferenceEngine();

Algorithm

This setting specifies the inference engine that will be used. Working with different inference algorithms describes this option in more detail. The options currently are:

// Use Expectation propagation
engine.Algorithm = new ExpectationPropagation();

// Use Variational Message Passing
engine.Algorithm = new VariationalMessagePassing();

// Use Gibbs sampling
engine.Algorithm = new GibbsSampling();

The default setting for the algorithm is Expectation Propagation. You can also optionally specify the algorithm when you create the engine:

InferenceEngine engine = new InferenceEngine(new VariationalMessagePassing());

BrowserMode

The first time you request a marginal (or other posterior value) from your program, Infer.NET 'compiles' your model into an efficient execution module before running the inference. This compilation is achieved by passing your code through a series of transformations. The output of each transformation is a refined piece of code representing your model. The browser mode allows you to browse the output of these transformations in a pop-up window. Once you close the window, the inference continues. The final transformation provides the source for your compiled model; this source is also, by default, saved to disk (whether or not the browser mode is set), and is discussed in more detail in Debugging inference.

The BrowserMode options are:

// Never show the transformations browser
engine.BrowserMode = BrowserMode.
Never;

// Only show the transformations browser when an error occurs in the compilation
engine.BrowserMode = BrowserMode.OnError;

// Always show the transformations browser
engine.BrowserMode = BrowserMode.Always
;

The default is to show the browser on error.

Note: in some circumstances showing the browser can consume a lot of memory. If you receive an out of memory exception when compiling, you can reduce memory consumption by setting the BrowserMode to Never.

Compiler

This property holds the compiler that the inference engine uses to compile the model into efficient inference code. You should not modify this property but you can modify properties of the returned ModelCompiler object. In particular, the GeneratedSourceFolder property is a relative or absolute path to where the C# inference source code should be generated:

engine.Compiler.GeneratedSourceFolder = "MyGeneratedSourceFolder";

You can also specify whether the generated source code should be written to disk using the WriteSourceFiles property:

engine.Compiler.WriteSourceFiles = false;


This property is set to true by default.

Another useful property allows you to generate the assembly in memory. Generating the assembly in memory can avoid security exceptions if you are running your inference from a network drive. This is set to true by default. If you want to debug into your generated inference code, you must set this to false:

engine.Compiler.GenerateInMemory = false;

To allow more efficient use of multiple cores, you can also instruct the compiler to emit parallel for loops rather than normal for loops using the UseParallelForLoops property:

engine.Compiler.UseParallelForLoops= true;

For this to work, you must have the Microsoft Parallel Extensions CTP installed or you will get an error at when the model is compiled.
Note: this is an experimental feature and subject to change in future releases.

DefaultEngine

All the defaults specified on this page can modfied by modifying properties of DefaultEngine which is a static property of the InferenceEngineclass. For example:

InferenceEngine.DefaultEngine.Algorithm = new VariationalMessagePassing();
InferenceEngine.DefaultEngine.BrowserMode = BrowserMode.Always;
InferenceEngine.DefaultEngine.ShowFactorGraph = true;

These settings will then become the default settings when a new InferenceEngine instance is created.

Group()

Allows you to specify that a set of variables are in a group. This is currently only used to manually specify the blocks in Block Gibbs Sampling. Note that groupings are automatically determined as necessary, so it is not typically necessary for you to call this method; but this does give you a manual override. A list of variables is passed to this method; the first argument is the root variable in the block which determines the order of message passing within it (first marginalisation within the block occurs towards the root, and then sampling is done away from from the root). Here is an example for a product of two Gaussians:

Variable<double> a = Variable.GaussianFromMeanAndVariance(1, 2);
Variable<double> b = Variable.GaussianFromMeanAndVariance(2, 3);
Variable<double> c = a * b;
GibbsSampling gs = new GibbsSampling();
gs.BurnIn = 100; gs.Thin = 10;
InferenceEngine engine = new InferenceEngine(gs);
engine.NumberOfIterations = 10000;
engine.Group(a, c);
engine.Group(b, c);
var marg = engine.Infer<Gaussian>(c);

ModelName

Allows a name to be specified for the model, which is used in the class name of the generated code. Defaults to "Model".

// Change the model name
engine.ModelName = "MyMixtureModel";

NumberOfIterations

This specifies how many iterations the algorithm will run for. An 'iteration' is a single update of all the variables in a model. For the EP and VMP algorithms. only a handful of iterations are sometimes required, and the inference algorithm may converge well before the default number of 50 iterations for the two algorithms.

// Change the number of iterations
engine.NumberOfIterations = 10;

Setting the number of iterations to -1 tells the inference engine to use the algorithm-specific default number of iterations. Note that Gibbs sampling will typically need many more iterations than EP or VMP.

ShowFactorGraph

This specifies whether to display the factor graph corresponding to the model. A factor graph is a graphical representation of a model which shows each variable as a circle and each factor or constraint as a square. If a variable participates in the factor or the constraint, then an edge is shown between the corresponding circle and square. Factor graphs are more than just a picture of the model; they are intimately related to the message passing algorithms used for inference in Infer.NET.

// Show the factor graph of the model
engine.ShowFactorGraph = true;

The default setting for this bool property is false.

ShowMsl

If this flag is set to true, the Model Specification Language (MSL) version of your model will be written to the Console

// Show the model in the Model Specification Language
engine.ShowMsl = true;

The default setting for this bool property is false.

ShowProgress

If this flag is set to true, progress information is written to the console, updating at each iteration.

// Print out progress information
engine.ShowProgress = true;

The default setting for this bool property is true

ShowTiming

If this flag is set to true, timing information is written to the console at the conclusion of the inference, showing total time and the time per iteration.

// Print out timing information
engine.ShowTiming = true;

The default setting for this bool property is false.

ShowWarnings

If this flag is set to true, any warnings encountered during inference will be printed to the console. The default setting for this bool property is true, so that you are aware of any problems encountered. To suppress warnings, set the property to false.

// Do not print out warnings
engine.ShowWarnings = false;

Last modified at 8/3/2009 10:05 AM  by John Guiver 
©2009 Microsoft Corporation. All rights reserved.  Terms of Use | Trademarks | Privacy Statement