共用方式為


評估模型輸入

將值繫結至模型的輸入和輸出之後,您就可以開始評估模型的輸入並取得其預測。

若要執行模型,您可以在 LearningModelSession 上呼叫任何 Evaluate* 方法。 您可以使用 LearningModelEvaluationResult來查看輸出功能。

範例

在下列範例中,我們會對工作階段執行評估,並傳入繫結和唯一的相互關聯識別碼。 然後,我們會將輸出剖析為機率清單,將其與我們的模型可辨識的各種事項的標籤清單比對,並將結果寫入主控台:

// How many times an evaluation has been run
private int runCount = 0;

private void EvaluateModel(
    LearningModelSession session,
    LearningModelBinding binding,
    string outputName,
    List<string> labels)
{
    // Process the frame with the model
    var results =
        await session.EvaluateAsync(binding, $"Run {++runCount}");

    // Retrieve the results of evaluation
    var resultTensor = results.Outputs[outputName] as TensorFloat;
    var resultVector = resultTensor.GetAsVectorView();

    // Find the top 3 probabilities
    List<(int index, float probability)> indexedResults = new List<(int, float)>();

    for (int i = 0; i < resultVector.Count; i++)
    {
        indexedResults.Add((index: i, probability: resultVector.ElementAt(i)));
    }

    // Sort the results in order of highest probability
    indexedResults.Sort((a, b) =>
    {
        if (a.probability < b.probability)
        {
            return 1;
        }
        else if (a.probability > b.probability)
        {
            return -1;
        }
        else
        {
            return 0;
        }
    });

    // Display the results
    for (int i = 0; i < 3; i++)
    {
        Debug.WriteLine(
            $"\"{labels[indexedResults[i].index]}\" with confidence of {indexedResults[i].probability}");
    }
}

裝置移除

如果裝置變得無法使用,或者想要使用不同的裝置,您必須關閉工作階段並建立新的工作階段。

在某些情況下,圖形裝置可能需要卸載並重載,如 DirectX 文件所述。

使用 Windows ML 時,您必須偵測此案例並關閉工作階段。 若要從裝置移除或重新初始化復原,您將建立新的工作階段,這會觸發裝置選取邏輯以再次執行。

最常見的情況是您會在 LearningModelSession.Evaluate 期間看到此錯誤。 在移除或重設裝置的情況下,LearningModelEvaluationResult.ErrorStatus 會是 DXGI_ERROR_DEVICE_REMOVEDDXGI_ERROR_DEVICE_RESET

另請參閱

注意

使用下列資源取得 Windows ML 的說明:

  • 如需詢問或回答有關 Windows ML 的技術問題,請使用 Stack Overflow 上的 windows-machine-learning 標籤。
  • 如需回報錯誤 (bug),請在 GitHub 上提出問題。