Tutorial de ML.NET: comience en 10 minutos

Consumir el modelo

El último paso es consumir el modelo entrenado en la aplicación del usuario final.

  1. Reemplace el código Program.cs en su proyecto myMLApp con el código siguiente:

    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. Ejecutar myMLApp (seleccionar Ctrl+F5 o Depurar > Iniciar sin depurar). Debería ver la siguiente salida, prediciendo si la declaración de entrada es positiva o negativa.

    La salida: Texto: Este restaurante fue fantástico. Opinión: positiva

La CLI de ML.NET ha generado el modelo entrenado y el código para usted, por lo que ahora puede utilizar el modelo en aplicaciones .NET (por ejemplo, su aplicación de consola SentimentModel) siguiendo estos pasos:

  1. En la línea de comandos, vaya al directorio consumeModelApp.
    Command prompt
    cd SentimentModel
  2. Abra el Program.cs en cualquier editor de código e inspeccione el código. El código debe ser similar al siguiente:

    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. Ejecute el SentimentModel.ConsoleApp. Puede hacerlo ejecutando el siguiente comando en la terminal (asegúrese de estar en el directorio SentimentModel):

    Command prompt
    dotnet run

    La salida debe tener un aspecto similar al siguiente:

    Command prompt
    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 ===============
Continuar