DEVintersection 2019
Nov 18th – 21st, MGM Grand, Las Vegas NV. Keynotes by Microsoft's Scott Guthrie, Eric Boyd, Julia Liuson, and Scott Hanselman.

ML.NET Tutorial - Get started in 10 minutes

Consume your model

Model Builder generates the trained model and code for you, including the next steps for model consumption, so you can easily use the model in your end-user .NET application.

Replace the Program.cs code in your myMLApp with the following code:

Program.cs
using System;
using MyMLAppML.Model;

namespace myMLApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Add input data
            var input = new ModelInput();
            input.SentimentText = "That is rude.";

            // Load model and predict output of sample data
            ModelOutput result = ConsumeModel.Predict(input);
            Console.WriteLine($"Text: {input.SentimentText}\nIs Toxic: {result.Prediction}");
        }
    }
}

Run myMLApp. You should see the following output, predicting whether the input statement is toxic (true) or non-toxic (false).

The output 'Text: This is rude. Is Toxic: True'

The ML.NET CLI has generated the trained model and code for you, so you can now use the model in your other .NET applications (e.g. your consumeModelApp console app) by following these steps:

  1. From your consumeModelApp, add a reference to the generated library project (SampleBinaryClassification.Model).

    In your terminal, run the following command (make sure you are in your consumeModelApp directory):

    Command prompt
    cd consumeModelApp
    dotnet add reference ../SampleBinaryClassification/SampleBinaryClassification.Model/
  2. In your consumeModelApp, install the ML.NET NuGet package.

    In your terminal, run the following command:

    Command prompt
    dotnet add package Microsoft.ML --version 1.0.0
  3. Copy the model (MLModel.zip) from SampleBinaryClassification.Model and paste it into the consumeModelApp directory.

  4. Replace the Program.cs code in your consumeModelApp with the following code:

    Program.cs
    using System;
    using SampleBinaryClassification.Model.DataModels;
    using Microsoft.ML;
    
    namespace consumeModelApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConsumeModel();
            }
    
            public static void ConsumeModel()
            {
                // Load the model
                MLContext mlContext = new MLContext();
    
                ITransformer mlModel = mlContext.Model.Load("MLModel.zip", out var modelInputSchema);
    
                var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
    
                // Use the code below to add input data
                var input = new ModelInput();
                input.SentimentText = "That is rude";
    
                // Try model on sample data
                // True is toxic, false is non-toxic
                ModelOutput result = predEngine.Predict(input);
    
                Console.WriteLine($"Text: {input.SentimentText} | Prediction: {(Convert.ToBoolean(result.Prediction) ? "Toxic" : "Non-toxic")} sentiment");
            }
        }
    }
  5. In your consumeModelApp, modify the sample text (input.SentimentText) in Program.cs.

    Program.cs
    input.SentimentText = "That is rude.";
  6. Run your consumeModelApp.

    In your terminal, run the following command (make sure you are in your consumeModelApp directory):

    Command prompt
    dotnet run
Continue