#MLNET – API improvements in the new 0.6.0 version

Hi!

A few days ago, the ML.Net team released the 0.6.0 version of Machine Learning.Net and one of the most important changes it’s on the way we use ML.Net API.

In my MLNet sessions I usually comment on a prediction scenario based on the Label of a person to see if it is a child, baby or teenager. All of this using a small set of data with information like name, age and gender.

01 mlnet age range data

As you can see from the previous image, my training data set has the columns Name, Age, Gender And Label. well, One important detail is that I also need 2 .Net classes with fields to represent the rows of My trainings Datasets and the expected Prediction.


public class AgeRangeData
{
[Column(ordinal: "0")]
public float AgeStart;
[Column(ordinal: "1")]
public float AgeEnd;
[Column(ordinal: "2", name: "Label")]
public string Label;
}
public class AgeRangePrediction
{
[ColumnName("PredictedLabel")]
public string PredictedLabels;
}

In the 0.5.0 version of ML.Net the way of working was based on creating a pipeline with the following steps

  • Define the data model and load training data
  • Define the features and labels
  • Select a Trainer and train the model

The generated model allowed to make Predictions. The following example explains it in 20 Lines of code


static void Main(string[] args)
{
var fileName = "AgeRanges.csv";
var pipeline = new LearningPipeline();
pipeline.Add(new TextLoader(fileName).CreateFrom<AgeRange>(separator: ',', useHeader: true));
pipeline.Add(new Dictionarizer("Label"));
pipeline.Add(new ColumnConcatenator("Features", "Age"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
pipeline.Add(new PredictedLabelColumnOriginalValueConverter {PredictedLabelColumn = "PredictedLabel" });
var model = pipeline.Train<AgeRange, AgeRangePrediction>();
Predict(model, "john", 9, "M");
Predict(model, "mary", 14, "M");
Predict(model, "laura", 2, "M");
Console.ReadLine();
}

With the 0.6.0 version, the way the API is used has changed a lot. The best thing at this point is to read the original post of Cesar De la Torre where he explains the novelties of this version (see references). However, I think leaving an example of the same code, adapted to version 0.6.0, will be good enough to share an idea of how flexible is the new API


static void Main(string[] args)
{
var dataPath = "AgeRangeData.csv";
var env = new LocalEnvironment();
var reader = TextLoader.CreateReader(env, ctx => (
Age: ctx.LoadFloat(1),
Label: ctx.LoadText(3)),
separator: ',', hasHeader: true);
var trainData = reader.Read(new MultiFileSource(dataPath));
var classification = new MulticlassClassificationContext(env);
var learningPipeline = reader.MakeNewEstimator()
.Append(r => (
r.Label,
Predictions: classification.Trainers.Sdca
(label: r.Label.ToKey(),
features: r.Age.AsVector())))
.Append(r => r.Predictions.predictedLabel.ToValue());
var model = learningPipeline.Fit(trainData);
var predictionFunc = model.AsDynamic.MakePredictionFunction<AgeRangeNewApi, AgeRangePredictionNewApi>(env);
var example = new AgeRangeNewApi()
{
Age = 6,
Name = "John",
Gender = "M"
};
var prediction = predictionFunc.Predict(example);
Console.WriteLine("prediction: " + prediction.PredictedLabel);
Console.ReadLine();
}

The main changes

  • Lines 5 to 9, loading the initial data file for training
  • Lines 11 to 18, Defining Features and Label for training
  • Line 20, training and model creation
  • Line 22, Creating a function to make predictions
  • Lines 24 to 32, example of a prediction

The complete example available in https://github.com/elbruno/Blog/tree/master/20181011%20MLNET%200.6%20NewAPI

Happy coding!

Greetings @ Toronto

El Bruno

References

My Posts

7 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: