Exploring Infer.NET
How to build custom machine learning models using .NET? Infer.NET opens the door to powerful probabilistic programming with surprising ease. Discover how this hidden gem from Microsoft can transform the way you think about inference and learning.
Step-by-Step Exploration of the Infer.NET Library: Creating Variables and the Bernoulli Distribution
The Infer.NET library, an open-source framework developed by Microsoft Research, empowers users to construct probabilistic programming systems. It facilitates models where variables are governed by probability distributions.
Core Namespaces
using Microsoft.ML.Probabilistic.Models;
using Microsoft.ML.Probabilistic.Algorithms;
using Microsoft.ML.Probabilistic.Distributions;
Understanding the Bernoulli Distribution
- Models a trial with two outcomes: true/false.
- Example: coin flip, yes/no response.
- Parameter:
probTrue, probability of “true”.
Declaring and Using Bernoulli Variables in Infer.NET
// Fair coin (50%)
Variable<bool> firstCoin = Variable.Bernoulli(0.5).Named("firstCoin");
// Picking a red ball (17%)
Variable<bool> firstPicking = Variable.Bernoulli(0.17).Named("firstPicking");
// Second coin toss
Variable<bool> secondCoin = Variable.Bernoulli(0.5).Named("secondCoin");
// Logical AND of two coin tosses
Variable<bool> bothHeads = firstCoin & secondCoin;
// Inference
InferenceEngine engine = new InferenceEngine();
Bernoulli bothHeadsDistribution = engine.Infer<Bernoulli>(bothHeads);
Console.WriteLine($"Probability both coins are heads: {bothHeadsDistribution.GetProbTrue()}");
Beyond Bernoulli: Exploring Other Variable Declaration Options
Gaussian Distribution (Continuous)
// Mean = 0, Variance = 1
Variable<double> gaussianVar1 = Variable.GaussianFromMeanAndVariance(0, 1).Named("gaussianVar1");
// Mean = 10, Precision = 2
Variable<double> gaussianVar2 = Variable.GaussianFromMeanAndPrecision(10, 2).Named("gaussianVar2");
// Mean from another variable
Variable<double> meanVar = Variable.GaussianFromMeanAndVariance(5, 0.5).Named("meanVar");
Variable<double> gaussianVar3 = Variable.GaussianFromMeanAndPrecision(meanVar, 1.0).Named("gaussianVar3");
Discrete Distribution (Categorical)
using Microsoft.ML.Probabilistic.Math;
// Discrete over 3 states: [0.2, 0.5, 0.3]
Variable<int> discreteVar = Variable.Discrete(new Vector(new double { 0.2, 0.5, 0.3 })).Named("discreteVar");
Other Commonly Used Distributions
// Gamma
Variable<double> gammaVar = Variable.GammaFromShapeAndRate(1, 1).Named("gammaVar");
// Beta
Variable<double> betaVar = Variable.Beta(1, 1).Named("betaVar");
// Poisson
Variable<int> poissonVar = Variable.Poisson(3).Named("poissonVar");
// Dirichlet
Variable<Vector> dirichletVar = Variable.Dirichlet(new Vector(new double { 1, 1, 1 })).Named("dirichletVar");
Using Variable.Random() for Custom Distributions
// Assuming MyCustomDistribution is a valid Infer.NET distribution
// Variable<double> customVar = Variable.Random(new MyCustomDistribution()).Named("customVar");
Deterministic vs. Random Variables
Constants
Variable<double> constantValue1 = Variable.Constant(3.14);
Variable<int> constantValue2 = 10;
Observed Variables
Variable<int> observedSize = Variable.Observed<int>().Named("observedSize");
observedSize.ObservedValue = 10;
Variable<double> observedData = Variable.Array<double>(new Range(5)).Named("observedData");
observedData.ObservedValue = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
Table: Common Probability Distributions in Infer.NET
Infer.NET provides a rich set of built-in probability distributions, you also have the option to create variables with custom distributions using code that defines your specific logic [1].
One way to do this is by using the Variable.Random() method [1]. This method allows you to declare a variable with a user-defined distribution class. To use this, you would need to create a class that inherits from one of the distribution base classes provided by Infer.NET [1]. This gives you the flexibility to model scenarios where the standard distributions might not perfectly fit your needs.
For example, if you have a complex process that generates data according to a specific set of rules or a combination of existing distributions, you could encapsulate that logic within a custom distribution class and then use Variable.Random() to create a variable with that distribution [1].
It’s worth noting that while custom distributions offer great flexibility, they might require a deeper understanding of the underlying probabilistic concepts and the Infer.NET framework to implement correctly.
Infer.NET already provides a wide array of common probability distributions that cover many typical modeling scenarios. These include:
- Discrete Distributions: Bernoulli, Discrete, Discrete (enum), Binomial, Multinomial, Poisson, DiscreteChar [2].
- Continuous Distributions: Beta, Gaussian, Gamma [2].
- Multivariate Distributions: Dirichlet, Wishart [2].
- Sequence Distributions: StringDistribution [2].
You can also create variables based on expressions involving other random variables. For instance, we saw how bothHeads was created by combining firstCoin and secondCoin using the logical AND operator [3, 4]. This allows you to build more complex relationships between variables in your model [3].
So, while the built-in distributions are extensive, Infer.NET does provide the capability to define variables with custom probabilistic behavior through code when needed [1].
| Distribution | Syntax Example | Description | Typical Use Cases |
|---|---|---|---|
| Bernoulli | Variable.Bernoulli(0.5) |
Binary trial | Coin toss |
| Gaussian | Variable.GaussianFromMeanAndVariance(0, 1) |
Continuous variable | Height, temperature |
| Discrete | Variable.Discrete(new Vector(...)) |
Categorical variable | Dice roll |
| Gamma | Variable.GammaFromShapeAndRate(1, 1) |
Positive continuous | Wait times |
| Beta | Variable.Beta(1, 1) |
Probability (0–1) | Conversion rates |
| Poisson | Variable.Poisson(3) |
Event count | Website visits |
| Dirichlet | Variable.Dirichlet(new Vector(...)) |
Prior over categories | Multinomial parameters |
Conclusion and Next Steps
- Start with
Variable.Bernoulli()for binary outcomes. - Use
InferenceEnginefor deriving posterior distributions. - Explore other distributions for broader modeling.
- Learn through official Infer.NET documentation.