An introduction to Infer# in .Net

As the need for Machine Learning (ML) and AI increases on a seemingly daily basis, we need enough languages and tools to aid developers in the software development process. There are so many roads and avenues to follow when you start programming for Machine Learning, but you have to remember that it is not simply a language that you learn. Machine Learning is quite complex, so to start, here are few links to get you started with the basics if you are unfamiliar with ML development:

What is Probabilistic Programming (PP)?

In Probabilistic programming (PP), probabilistic models are specified and inference for these models is performed automatically. This approach attempts to unify probabilistic modeling and traditional programming, so that probabilistic modeling can be slightly easier and more widely acceptable.

Probabilistic reasoning has been around for quite a while and has been used for Probabilistic tasks such as predicting stock prices, recommending movies, and image detection. The problem, however, was that because of limited computing power, probabilistic programming was limited in scope, and inference algorithms for tasks had to be written manually.

What is the Infer.NET Framework?

The development of the Infer.NET framework started in 2004 and was released for academic use in 2008, then released as open source in 2018. Infer.NET is a framework for running Bayesian inference in graphical models. In addition:

  • Infer.NET is used for probabilistic programming.
  • Infer.NET runs Bayesian inference in graphical models.
  • Infer.NET follows a model-based approach.

Infer.NET can solve many machine learning problems such as classification, recommendation, and clustering. Infer.NET can also be used in a variety of domains such as information retrieval, bioinformatics, epidemiology, and vision. Infer.NET is available on GitHub and available as NuGet packages. Infer.NET is used by Microsoft themselves as the machine learning engine in products such as Azure, Xbox and Office.

Infer.NET can be used with .NET languages such as C++, C#, F#, Visual Basic, and IronPython. It simplifies processes necessary to implement probabilistic programming because of the following features:

  • A modelling API for creating probabilistic models.
  • An inference engine that handles the inference process and combines the Infer.NET model to all the observations.
  • It supports continuous as well as discrete variables. T
  • It was designed to be scalable.
  • It acts as a compiler that converts model descriptions into source code.
  • It provides multiple algorithms.

How to Create an Infer# Project

Let’s quickly create a project:

  • First, download the Infer.NET Framework from GitHub
  • Open the infer-main project that you have just downloaded.
  • Visual Studio may prompt you to install some Python libraries because the download project contains python scripts. Install them.

Infer# Install Python

  • Build the infer-main solution to create the necessary files that we will need in our little project.
  • Start Visual Studio and create a new Console project
  • Name the project Infer_Ex for example.
  • Right-click on the project in solution Explorer
  • Select Add Reference.
  • In the Reference Manager screen, select Browse

Infer# Reference Manage

Browse to the following location:

WHERE YOU INSTALLED IT\ C:\Users\ockert.dupreez\Desktop\Infer#\infer-main\infer-main\src\Learners\Classifier\bin\Debug\netstandard2.0

Select the following files:

  • Microsoft.ML.Probabilistic.Learners.dll
  • Microsoft.ML.Probabilistic.Learners.Classifier.dll
  • Microsoft.ML.Probabilistic.dll

Or, you can also add a reference to Microsoft.ML.Probabilistic.Compiler through NuGet Package Manager. To do this, follow these steps:

  • Right-click on your project in the Solution Explorer.
  • Select Manage NuGet Packages.
  • Click the Browse tab
  • Enter: Microsoft.ML.Probabilistic.Compiler
  • Select it, then click Install

NuGet Package for Microsoft.ML.Probabilistic.Compiler

Add the following Namespaces to your Program.cs file:

using System;
using System.Linq;
using Microsoft.ML.Probabilistic;
using Microsoft.ML.Probabilistic.Distributions;
using Microsoft.ML.Probabilistic.Models;

Edit the Main method to look like the following:

        static void Main(string[] args)
            var varWinner = new[] { 0, 0, 0, 1, 3, 4 };
            var varLoser = new[] { 1, 3, 4, 2, 1, 2 };

            var varGame = new Range(varWinner.Length);
            var varCurrPlayer = new Range(varWinner.Concat(varLoser).Max() + 1);
            var varCurrPlayerSkillset = Variable.Array(varCurrPlayer);
            varCurrPlayerSkillset[varCurrPlayer] = Variable.GaussianFromMeanAndVariance(6, 9).ForEach(varCurrPlayer);

            var varAllWinners = Variable.Array(varGame);
            var varAllLosers = Variable.Array(varGame);

            using (Variable.ForEach(varGame))

                var varWinPerformance = Variable.GaussianFromMeanAndVariance(varCurrPlayerSkillset[varAllWinners[varGame]], 1.0);
                var varLosePerformance = Variable.GaussianFromMeanAndVariance(varCurrPlayerSkillset[varAllLosers[varGame]], 1.0);

                Variable.ConstrainTrue(varWinPerformance > varLosePerformance);


            varAllWinners.ObservedValue = varWinner;
            varAllLosers.ObservedValue = varLoser;

            var varInferenceEngine = new InferenceEngine();
            var varInferredSkills = varInferenceEngine.Infer<Gaussian[]>(varCurrPlayerSkillset);

            var varOrderedPlayerSkills = varInferredSkills
                    .Select((s, i) => new { varCurrPlayer = i, varCurrPlayerSkillset = s })
                    .OrderByDescending(ps => ps.varCurrPlayerSkillset.GetMean());

            foreach (var playerSkill in varOrderedPlayerSkills)
                Console.WriteLine($"Player {playerSkill.varCurrPlayer} skill: {playerSkill.varCurrPlayerSkillset}");


The code is loosely based on the example found here.

More by Author

Must Read