ML.NET Tutorial - Get started in 10 minutes

Consume your model

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

  1. From your myMLApp app, add a reference to the generated library project (myMLAppML.Model).

  2. Right-click on your myMLApp project and select Add > Reference. In the Reference Manager, check myMLAppML.Model and select OK.

  3. In your myMLApp app, install the ML.NET NuGet package.

  4. Right-click on your myMLApp project and select Manage NuGet Packages. Under the Browse tab, search for Microsoft.ML and install the NuGet package.

  5. In myMLAppML.Model, right-click MLModel.zip, select Properties, and set Copy to Output to "Copy Always."
  6. Replace the Program.cs code in your myMLApp with the following code:
  7. Program.cs
    using System;
    using MyMLAppML.Model.DataModels;
    using Microsoft.ML;
    
    namespace myMLApp
    {
        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 = "Type your sentiment";
    
                // 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");
            }
        }
    }
  8. In your myMLApp, modify the sample text (input.SentimentText) in Program.cs.
  9. Program.cs
    input.SentimentText = "That is rude.";
  10. Run your myMLApp.

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).

  2. 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/
  3. In your consumeModelApp, install the ML.NET NuGet package.

  4. In your terminal, run the following command:

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

  6. Replace the Program.cs code in your consumeModelApp with the following code:
  7. 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");
            }
        }
    }
  8. In your consumeModelApp, modify the sample text (input.SentimentText) in Program.cs.
  9. Program.cs
    input.SentimentText = "That is rude.";
  10. Run your consumeModelApp.

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

    Command prompt
    dotnet run
Continue