Using Infer.NET in Silverlight

Infer.NET can now be used from within Silverlight applications, although it is necessary to precompile your inference algorithm.  Silverlight support means that you can now write Infer.NET applications that run inside the browser, with the computation happening at the client. You can use Infer.NET with Silverlight 3.0 or later versions.


The Silverlight environment imposes a few limitations on what Infer.NET functionality can be used:

  • As mentioned above, models must be precompiled into a generated inference algorithm.  This is because the Silverlight security model does not allow for code compilation. 
  • DiscreteEnum distributions cannot be used, due to a missing static method on Enum in Silverlight. 
    Workaround: use int instead of your enum type.

What you need to do

To use Infer.NET in a Silverlight project, you will need:

  • A non-Silverlight project with the Infer.NET model code in it.  This should reference the normal Infer.NET Compiler and Runtime DLLs. You can use this project to develop and test the model and the inference algorithm. This project will create the generated inference code for use by the Silverlight project.
  • Your Silverlight project, with an added reference the Silverlight version of the Infer.NET runtime. This is a DLL called Infer.Runtime.Silverlight.dll which can be found in the Infer.NET installation in the same location as the standard DLLs (i.e. in the bin/Debug and bin/Release folders of the installation folder).  You must also add the inference code generated by running the non-Silverlight project.  You can then perform inference by calling in to this generated code, as described here.


As an example, we will look at the clinical trial model.  There is an example project included in the Infer.NET installation which provides a WPF user interface for this model.  We can use this as our non-Silverlight project, to generate the inference code for this model.

The Silverlight project provides a similar user interface, but written in Silverlight.  You can find this example project in Samples/C#/ClinicalTrialSilverlight.  Here are some snippets, showing how this project calls in to the generated inference code:


model = new ClinicalTrialGeneratedCode();


model.numberPlacebo = placebo.Length;
model.numberTreated = treated.Length;
model.treatedGroupOutcomes = treated;
model.placeboGroupOutcomes = placebo;

double probTrue = model.IsEffectiveMarginal().GetProbTrue();


The code creates an instance of the generated code, sets properties on it for each observed value and then calls Execute(50) to run 50 iterations of inference.  The results of inference for the IsEffective variable are retrieved using the IsEffectiveMarginal() call.

You can see the Clinical Trial example running in Silverlight in your browser on this external page.

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