Try .NET on Azure for free
Get started with 12 months of free services and build .NET cloud apps with your Azure free account.

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.

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

  2. 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}");
            }
        }
    }
  3. Run myMLApp. You should see the following output, predicting whether the input statement is toxic (true) or non-toxic (false).

  4. 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 (for example, your consumeModelApp console app) by following these steps:

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

  2. Program.cs
    using System;
    using SampleClassification.Model;
    
    namespace consumeModelApp
    {
        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 your consumeModelApp. In your terminal, run the following command (make sure you are in your consumeModelApp directory):

    Terminal
    dotnet run
Continue