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 |
Description |
Example |
|
MarginalPrototype |
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. |
x.AddAttribute( new MarginalPrototype( new Gaussian())); | |
|
Output |
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()); | |
| ValueRange |
Specifies the range of values taken by an integer variable, or the dimension of a Vector variable. This attribute 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 methods that create integer or Vector variables can take a range argument and will apply this attribute automatically. You should only use this attribute if none of these methods is suitable, and you get an error message about a missing ValueRange. |
Range k = new Range(mixtureSize); Variable<Vector> weights = Variable.New<Vector>(); weights.AddAttribute( new ValueRange(k))); | |
|