In a previous post, I detailed how to build and deploy C#
ML.NET models with
ASP.NET Core. With inspiration from Jeff Fritz, I have been learning F# for the past week and a half or so. When trying to think of projects to start practicing my F#, porting over the code I had built in C# naturally came to mind. After overcoming many obstacles and with much guidance from Alan Ball and Isaac Abraham whose F# book I highly recommend, I was able to successfully port over the main parts of my code which highlight
ML.NET functionality. In this writeup, I will port a C#
ML.NET classification model to F# which predicts the type of flower based on four numerical measurement inputs. I tried to keep the organization of this post nearly identical to that of the C# article where possible. Sample code for this project can be found at the following link.
This project was built on a Linux PC but should work cross-platform on Mac and Windows.
The first thing we want to do is create a folder for our solution.
Then, we want to create a solution inside our newly created folder.
First, we want to create the project. From the solution folder enter:
dotnet new console -o model -lang f#
Now we want to add this new project to our solution.
dotnet sln add model/model.fsproj
Since we’ll be using the
ML.NET framework, we need to add it to our
dotnet add model/model.fsproj package Microsoft.ML
Before we start training the model, we need to download the data we’ll be using to train. We do so by downloading the data file into our root solution directory.
curl -o iris-data.txt https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
If we take a look at the data file, it should look something like this:
Now that we have all our dependencies set up, it’s time to build our model. I leveraged the demo that is used on the
ML.NET Getting-Started website.
At the time of this writing,
0.2.0 does not fully support F# Records. A workaround for this are mutable classes. Not really inline with F# paradigms, but it should be good enough.
Program.fs file of our
model project directory, let’s create two mutable classes called
IrisPrediction which will define our features and predicted attribute respectively. Both of them will use
Microsoft.ML.Runtime.Api to add the property attributes.
Here is what our
IrisData class looks like:
type IrisData() =
Similarly, here is the
type IrisPrediction() =
The way the
ML.NET computations process is via a sequential pipeline of steps that are performed eventually leading up to the training of the model. We can add that logic inside the
main function of our
let dataPath = "./iris-data.txt"
Now that we have our data structures and model trained, it’s time to test it to make sure it’s working. Following our training operation, we can add the following code.
// Test data for prediction
Program.fs file should contain content similar to that below:
All set to run. We can do so by entering the following command from our solution directory:
dotnet run -p model/model.fsproj
Once the application has been run, the following output should display on the console.
Automatically adding a MinMax normalization transform, use 'norm=Warn' or 'norm=No' to turn this behavior off.
In this post, we ported over a C#
ML.NET classification model to F# which predicts the class of flower based on numerical measurement inputs. While several workarounds needed to be made,
ML.NET is still in its infancy. As more people become involved and provide feedback hopefully in the near future, F# support and functionality will become more stable. Happy coding!