Command Line

ML.net has a command line available through the following syntax:

dotnet ./bin/AnyCPU.Debug/Microsoft.ML.Console/netcoreapp2.1/MML.dll <string>

The <string> is either ? to get help or a command.

List of available commands

<<<

?

The whole list of commands documented at Commands.

Help on a particulier command

<<<

? chain

List of available object kind

<<<

? kind =

List of available trainers

<<<

? kind = trainer

Help on a particular object

<<<

? lr

One example: train a logistic regression

<<<

train
data = iris.txt
loader = text{col = Label: R4: 0 col = Features: R4: 1 - 4 header = +}
tr = mlr{maxiter = 5}
out = logistic_regression.zip

Chaining a training with an export

The following command line trains a model then exports it to ONNX (see also ML.net and ONNX).

<<<

chain

cmd = train{
    data = iris.txt
    loader = text{col = Label: R4: 0 col = Features: R4: 1 - 4 header = +}
    tr = mlr{maxiter = 5}
    out = logistic_regression.zip
}

cmd = saveonnx{
    in = logistic_regression.zip
    onnx = logistic_regression.onnx
    domain = ai.onnx.ml
    idrop = Label
}

Produces a C# code to predict

<<<

chain

cmd = train{
    data = iris.txt
    loader = text{col = Label: R4: 0 col = Features: R4: 1 - 4 header = +}
    tr = mlr{maxiter = 5}
    out = logistic_regression.zip
}

cmd = codegen{
    in = logistic_regression.zip
    cs = logistic_regression.cs
}

The second command produces a C# which can be used to compute predictions with a C# implementation.

    using System;
    using System.Collections.Generic;
    using Microsoft.ML.Legacy;
    using Microsoft.ML.Runtime.Api;
    
    namespace MLGeneratedCode
    {
    public class Program
    {
    /// <summary>
    /// This is the input to the trained model.
    ///
    /// In most pipelines, not all columns that are used in training are also used in scoring. Namely, the label
    /// and weight columns are almost never required at scoring time. Since we don't know which columns
    /// are 'optional' in this sense, all the columns are listed below.
    ///
    /// You are free to remove any fields from the below class. If the fields are not required for scoring, the model
    /// will continue to work. Otherwise, the exception will be thrown when a prediction engine is created.
    ///
    /// </summary>
    public class InputData
    {
                public Single Label;
    
                [VectorType(4)]
                public Single[] Features = new Single[4];
    }
    
    /// <summary>
    /// This is the output of the scored model, the prediction.
    ///
    ///</summary>
    public class ScoredOutput
    {
                [KeyType(Count=3, Min=0, Contiguous=true)]
                public UInt32 PredictedLabel;
    
                [VectorType(3)]
                public Single[] Score;
    
                // These are all remaining available columns, either supplied as the input, or intermediate
                // columns generated by the transforms. Materializing these columns has a performance cost,
                // so they are commented out. Feel free to uncomment any column that is useful for your scenario.
    #if false
                public Single Label;
    
                [VectorType(4)]
                public Single[] Features;
    #endif
    }
    
    /*public static void Main(string[] args)
    {
    string modelPath;
    modelPath = "logistic_regression.zip";
    PredictAsync(modelPath);
    }*/
    
    /// <summary>
    /// This method demonstrates how to run prediction.
    ///
    ///</summary>
    public static async void PredictAsync(string modelPath)
    {
        var model = await PredictionModel.ReadAsync<InputData, ScoredOutput>(modelPath);
    
        var inputData = new InputData();
        // TODO: populate the example's features.
    
        var score = model.Predict(inputData);
        // TODO: consume the resulting score.
    
        var scores = model.Predict(new List<InputData> { inputData, inputData });
        // TODO: consume the resulting scores.
      }
    } 
    }