Tutorial ML.NET - Mulai dalam 10 menit

Gunakan model Anda

Langkah terakhir adalah menggunakan model terlatih Anda di aplikasi pengguna akhir.

  1. Ganti kode Program.cs di proyek myMLApp Anda dengan kode berikut:

    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. Jalankan myMLApp (pilih Ctrl+F5 atau Debug > Mulai Tanpa Penelusuran Kesalahan). Anda akan melihat output berikut, memprediksi apakah pernyataan input positif atau negatif.

    Output: Teks: Restoran ini luar biasa. Sentimen: Positif

CLI ML.NET telah membuat model dan kode terlatih untuk Anda, sehingga sekarang Anda dapat menggunakan model di aplikasi .NET (misalnya, aplikasi konsol SentimentModel) dengan mengikuti langkah-langkah berikut:

  1. Di baris perintah, navigasikan ke direktori consumeModelApp.
    Command prompt
    cd SentimentModel
  2. Buka Program.cs di editor kode apa pun dan periksa kodenya. Kode harus terlihat mirip dengan berikut ini:

    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. Jalankan SentimentModel.ConsoleApp Anda. Anda dapat melakukan ini dengan menjalankan perintah berikut di terminal (pastikan Anda berada dalam direktori SentimentModel):

    Command prompt
    dotnet run

    Output akan terlihat seperti ini:

    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 ===============
Melanjutkan