ML.NET 튜토리얼 - 시작하기(10분)

모델 사용

마지막 단계는 최종 사용자 애플리케이션에서 학습된 모델을 사용하는 것입니다.

  1. myMLApp 프로젝트의 Program.cs 코드를 다음 코드로 바꿉니다.

    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. myMLApp을 실행합니다(Ctrl+F5 또는 디버그 > 디버깅하지 않고 시작 선택). 입력 문이 양수인지 음수인지 예측하는 다음 출력이 표시되어야 합니다.

    출력: 텍스트: 이 식당은 훌륭했어. 감정: 긍정적

ML.NET CLI가 학습된 모델과 코드를 생성했으므로 이제 다음 단계에 따라 .NET 애플리케이션(예: SentimentModel 콘솔 앱)에서 모델을 사용할 수 있습니다.

  1. 명령줄에서 consumeModelApp 디렉터리로 이동합니다.
    Command prompt
    cd SentimentModel
  2. 코드 편집기에서 Program.cs를 열고 코드를 검사합니다. 코드는 다음과 유사해야 합니다.

    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. SentimentModel.ConsoleApp을 실행합니다. 터미널에서 다음 명령을 실행하여 이를 수행할 수 있습니다(SentimentModel 디렉터리에 있는지 확인).

    Command prompt
    dotnet run

    출력은 다음과 같이 표시되어야 합니다.

    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 ===============
계속