Didacticiel ML.NET - Démarrage en 10 minutes

Consommez votre modèle

La dernière étape consiste à consommer votre modèle formé dans l'application de l'utilisateur final.

  1. Remplacez le code Program.cs dans votre projet myMLApp par le code suivant :

    Program.cs
    using MyMLApp;
    // Add input data
    var sampleData = new SentimentModel.ModelInput()
    {
        Col0 = "This restaurant was wonderful."
    };
    
    // Load model and predict output of sample data
    var result = SentimentModel.Predict(sampleData);
    
    // If Prediction is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"
    var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";
    Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");
  2. Exécutez myMLApp (sélectionnez Ctrl+F5 ou Déboguer > Démarrage sans débogage). Vous devez voir la sortie suivante, qui prédit si l’instruction d’entrée est positive ou négative.

    Sortie : Texte : ce restaurant était formidable. Sentiment : positif

L’interface CLI ML.NET a généré le modèle et le code entraînés pour vous. Vous pouvez donc l’utiliser dans les applications .NET (par exemple, votre application SentimentModel console) en suivant ces étapes :

  1. Dans la ligne de commande, accédez au répertoire consumeModelApp.
    Terminal
    cd SentimentModel
  2. Ouvrez le code Program.cs dans n’importe quel éditeur de code et inspectez le code. Le code doit ressembler à ce qui suit :

    Program.cs
    using System;
    
    namespace SentimentModel.ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Add input data
                SentimentModel.ModelInput sampleData = new SentimentModel.ModelInput()
                {
                  Col0 = @"Wow... Loved this place."
                };
    
                // Make a single prediction on the sample data and print results
                var predictionResult = SentimentModel.Predict(sampleData);
    
                Console.WriteLine("Using model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data...\n\n");
    
    
                Console.WriteLine($"Col0: @{"Wow... Loved this place."}");
                Console.WriteLine($"Col1: {1F}");
    
    
                Console.WriteLine($"\n\nPredicted Col1: {predictionResult.PredictedLabel}\n\n");
                Console.WriteLine("=============== End of process, hit any key to finish ===============");
                Console.ReadKey();
            }
        }
    }
  3. Exécutez votre SentimentModel.ConsoleApp. Pour ce faire, exécutez la commande suivante dans le terminal (vérifiez que vous êtes dans le répertoire SentimentModel) :

    Terminal
    dotnet run

    La sortie devrait ressembler à ceci :

    Terminal
    Using model to make single prediction -- Comparing actual Col1 with predicted Col1 from sample data...
    
    
    Col0: Wow... Loved this place.
    Col1: 1
    Class                          Score
    -----                          -----
    1                              0.9651076
    0                              0.034892436
    =============== End of process, hit any key to finish ===============
Continuer