The importance of using SetTo
The New/SetTo constructs are crucial when using branches. Suppose in the scalar mixture model on the previous page, we used ordinary C# assignments:
Variable<int> c
= Variable.Discrete(new
double[] { 0.5, 0.5 }); |
This code fails because of the imperative execution of C#. The second assignment to x destroys the random variable created in the first case. As the C# equality operator cannot be overridden, the Infer.NET API cannot intercept this assignment to provide different behaivour. Instead, Infer.NET provides New/SetTo to allow both definitions to exist in the model, with switching done at runtime. The correct code is:
Variable<int> c
= Variable.Discrete(new
double[] { 0.5, 0.5 }); |
For more help with building mixture models, see the Mixture of Gaussians tutorial.
Another thing to note about assignment and SetTo is that you should never assign one variable directly to another, like so:
// incorrect!!
- two references to the same variable object |
Instead make a new variable using Variable.Copy:
observedLabel.SetTo(Variable.Copy(label)); |

