2023/05/11

AutoML:自動機械学習(2)

今朝方体調不良で目が覚めた。ここ数日の肌寒さが気にはなっていたが特に対処しなかったのがたたったかな😨 くしゃみ、鼻水が止まらなくて寝てられなかった。とはいえ、先に進める必要があるので作業は続けてます。

前回進捗状況観る為にってやつですが、あれだと何が何だか分からない出力がだらだらと出るだけで、まあ、チュートリアルではなんであんなの入れているのか不明だけど、実際にはその続きが書かれているのかなぁ。

  1. public class AutoMLMonitor : IMonitor
  2. {
  3. private readonly SweepablePipeline _pipeline;
  4.  
  5. private readonly List<TrialResult> _completedTrials;
  6.  
  7. private readonly RichTextBox _richTextBox;
  8.  
  9. public IEnumerable<TrialResult> GetCompletedTrials() => _completedTrials;
  10.  
  11. public AutoMLMonitor(SweepablePipeline pipeline, RichTextBox rtbLog)
  12. {
  13. _pipeline = pipeline;
  14. _completedTrials = new List<TrialResult>();
  15. _richTextBox = rtbLog;
  16. }
  17.  
  18. public void ReportBestTrial(TrialResult result)
  19. {
  20. return;
  21. }
  22.  
  23. public void ReportCompletedTrial(TrialResult result)
  24. {
  25. var trialId = result.TrialSettings.TrialId;
  26. var timeToTrain = result.DurationInMilliseconds;
  27. var pipeline = _pipeline.ToString(result.TrialSettings.Parameter);
  28. _completedTrials.Add(result);
  29. // Console.WriteLine($"Trial {trialId} finished training in {timeToTrain}ms with pipeline {pipeline}");
  30. _richTextBox.AppendText($"Trial {trialId} finished training in {timeToTrain}ms with pipeline {pipeline}" + Environment.NewLine);
  31. }
  32.  
  33. public void ReportFailTrial(TrialSettings settings, Exception exception = null)
  34. {
  35. if (exception.Message.Contains("Operation was canceled."))
  36. {
  37. // Console.WriteLine($"{settings.TrialId} cancelled. Time budget exceeded.");
  38. _richTextBox.AppendText($"{settings.TrialId} cancelled. Time budget exceeded." + Environment.NewLine);
  39. }
  40. // Console.WriteLine($"{settings.TrialId} failed with exception {exception.Message}");
  41. _richTextBox.AppendText($"{settings.TrialId} failed with exception {exception.Message}" + Environment.NewLine);
  42. }
  43.  
  44. public void ReportRunningTrial(TrialSettings setting)
  45. {
  46. return;
  47. }
  48. }

これが本来使うもので、ああ、実際には相変わらずConsole.WriteLineだったのでRichTextBoxに出力する様に修正してます。で、これをAutoML実行前に、

  1. var monitor = new AutoMLMonitor(pipeline, rtbLog);
  2. experiment.SetMonitor(monitor);

としておけばいい感じです。AutoMLの実行も

  1. var cts = new CancellationTokenSource();
  2. TrialResult experimentResults = await experiment.RunAsync(cts.Token);

って感じに変更なんですが、この部分がいまいちよく分かってないけど真似しておく😉 AutoMPが終わったら

  1. var completedTrials = monitor.GetCompletedTrials();
  2.  
  3. mlContext.Model.Save(experimentResults.Model, data.Schema, $"ShinbaTime.zip");

てな感じで保存しておかないと無駄になるのでしておきます。保存しておけば、

  1. DataViewSchema predictionPipelineSchema;
  2. ITransformer predictionPipeline = mlContext.Model.Load($"ShinbaTime.zip", out predictionPipelineSchema);
  3.  
  4. predictionEngine = mlContext.Model.CreatePredictionEngine<ShinbaTime, ShinbaTimePrediction>(predictionPipeline);

とすれば使えます。予測したいサンプル準備して

  1. yoso = predictionEngine.Predict(inputData);

まだまだやる事は多いけど、一時は全ての苦労が水の泡かと思ったけどなんとかなったかな😁

0 件のコメント:

コメントを投稿