Skip to main content

Infer.NET user guide

Calling Infer.NET from F#

Since Infer.NET is a .NET API, it can be called directly from F#. In addition to this, the Infer.NET source code includes an optional wrapper library that allows more succinct F# code.

Porting existing C# code to F#

If you are taking existing C# Infer.NET example code and porting it to F# there are a couple of issues to bear in mind:

F# Wrapper

The F# wrapper consists of a set of type declarations and functionality to make the experience of using Infer.NET more user friendly for the F# programmer. Infer.NET makes heavy use of generic classes methods and overrides, and F#’s implicit type inference has some difficulty disambiguating some Infer.NET types. Calls to some methods therefore need explicit specification of type arguments, and these types can get quite complex, especially, for example, for distribution arrays over jagged array domains. Therefore many type declarations are included in the F# wrapper to make such explicit typing much easier.

Another issue with calling Infer.NET from F# is the use of imperative constructs which are counter-intuitive for an F# programmer. Hence the wrapper provides functions to call these methods in a more natural way.

Complex jagged arrays of data are also difficult to create and assign values to and methods are provided to make this easier and allow assignment of values in a non imperative fashion.

A final issue is comparison operator overriding for Variable<T> objects. In C# these can be used to create new variables (for example a < b) whose result types are Variable<bool>s. F# does not recognise such overrides, so the wrapper provides alternative operators (for example a « b). The Wrapper is divided into a set of modules as described below.

The Modules

The F# wrapper is accessed by referencing the FSharpWrapper.dll and including the namespace Microsoft.ML.Probabilistic.FSharp in an F# script file, or by including references in the Solution Project of an F# program file. It contains the following modules, which can be accessed by calling moduleName.methodName with appropriate arguments, from an F# file. Alternatively use open moduleName to avoid referring to the moduleName each time a module is used.

Using the Methods in the F# Wrapper

A following description of how to use the methods is divided up by commonly used functionality needed to build models in Infer.NET, which can make use of methods and type declarations from several modules.

Tutorial modules

These modules show how to use the F# Wrapper to rewrite the C# tutorials: