Infer.NET user guide

Adding attributes to your model

Infer.NET allows models to be annotated with attributes that provide information to the inference engine.  Such attributes can be used to affect how inference is performed in particular parts of the model.

 Attributes can be added to a variable using the following syntax:

x.AddAttribute(new MyAttribute());

where x is of type Variable<T>.  It is often convenient to add attributes to a variable when it is created, using the following inline syntax:

Variable<bool> b = Variable.Bernoulli(0.5).Attrib(new MyAttribute());

Any object can be attached as an attribute to your model.  The following attribute types are currently recognised by the inference engine:

Attribute class
In certain circumstances, the inference engine is unable to determine what type of distribution to use as the marginal for a particular variable.  In this case, you will receive an error asking you to specify the 'marginal prototype' for that variable.  A marginal prototype is an instance of a distribution class e.g. new Gaussian() which indicates the form of the desired marginal. The parameters of the supplied distribution are not used, just the class and dimensionality. 
To specify a marginal prototype for a variable, add a MarginalPrototype attribute.


When a variable is being shared between multiple models, it is necessary to infer the marginal distribution excluding the effect of the prior.  In Infer.NET terminology, this is called an output message.  It will only be computed by the inference engine for variables marked as output variables using the Output attribute.  The SharedVariable class will automatically add this attribute.  However, it may also be added manually.
For a variable v which was marked with this attribute, the output message can be retrieved using the GetOutputMessage(v) method on the inference engine.

x.AddAttribute(new Output());

TraceMessages Causes all messages related to this variable to be printed to the console during inference

x.AddAttribute(new TraceMessages());

DoNotInfer Tells the inference engine that this variable will not be inferred, so that more efficient code can be generated.
DivideMessages Says whether outgoing messages from a variable should be calculated by division. This is especially relevant to the Expectation Propagation algorithm where division is much more efficient. However division can be less accurate numerically, and this attribute gives the option to use multiplication instead.

x.AddAttribute(new DivideMessages(false));


This attribute is only currently relevant to Gibbs sampling, and allows you to specify what type information you want to retrieve for a variable when you query the model. Setting this attribute for various query types tells the model compiler to generate additional code for the different query types. The QueryTypes class contains some standard query types:

When you call Infer, you can specify any of these types and the inference call will recompile the model if necessary to allow you to retrieve these, so using the attribute is not essential, but setting it may avoid some recompilations of the model.



Some attributes are set by a method on the variable, and these are shown in the following table. Although these attributes can be added in the standard way as in the previously table, there is some additional processing that needs to be done when setting these attributes, and this is handled in the method:


void SetSparsity(Sparsity sparsity)

Sets the sparsity for Vector random variables. Typically this is only meaningful for models involving Dirichlet factors, and you do not normally have to call this method as the sparsity is inferred from the sparsity of the vector parameter.



void SetValueRange(Range valueRange)

Specifies the range of values taken by an integer variable, or the dimension of a Vector variable. This method can be used to explicitly specify the value range for a variable in cases where it cannot be deduced by the model compiler. Many of the factory methods that create integer or Vector variables can take a range argument and will apply this attribute automatically. You should only use this method if none of these factory methods is suitable, and you get an error message about a missing ValueRange.

Range k = new Range(mixtureSize);
Variable<Vector> weights =

©2009-2013 Microsoft Corporation. All rights reserved.  Terms of Use | Trademarks | Privacy Statement