Windows ML API を使用すると、モデルを評価するデバイスを選択できます。 セッションを作成するときにデバイスを選択します。 そのためには、機械学習モデルの評価に使用するデバイスを定義する LearningModelDevice クラスを使用します。
ここでは、前のアプリの classifier.cs ファイルに変更を加えて、デバイスの GPU でモデル評価を実行できるようにします。 前のチュートリアルでは、WinML Code Generator によって自動的に生成されたため、そのファイルには触れませんでした。
classifier.csファイルを開き、CreateFromStreamAsyncメソッドに次のコードを追加します。
// Select GPU or another DirectX device to evaluate the model.
LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
別の実行デバイスを選択するには、DirectX フィールドを別の実行デバイスに切り替えます。
- これで、セッションの作成時にこのデバイスを選択できるようになりました。
LearningModelSessionコンストラクターを変更して、実行デバイスを指定します。
// Create the evaluation session with the model and device.
learningModel.session = new LearningModelSession(learningModel.model, device);
CreateFromStreamAsync メソッドは次のようになります。
CreateFromStreamAsync(IRandomAccessStreamReference stream)
{
classifierModel learningModel = new classifierModel();
learningModel.model = await LearningModel.LoadFromStreamAsync(stream);
// Select GPU or another DirectX device to evaluate the model.
LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
// Create the evaluation session with the model and device.
learningModel.session = new LearningModelSession(learningModel.model, device);
learningModel.binding = new LearningModelBinding(learningModel.session);
return learningModel;
}
それだけです! モデルの評価用に GPU を正常に選択しました。
デバイスを指定しない場合、システムは Default を使用します。 Default を使用して、将来的にシステムが自動的に選択できるようにする柔軟性を得ることをお勧めします。
Windows AI では、呼び出し元が既に作成したデバイスの使用がサポートされています。 高度なデバイス作成の詳細については、セッションの作成に関するドキュメントを参照してください。
注
実行デバイスを選択する方法については、LearningModelDevice のドキュメントを参照してください。