定型及評估模型

了解如何建置機器學習模型、收集計量,以及使用 ML.NET 測量效能。 雖然此範例訓練的是迴歸模型,但概念可適用於大多數的其他演算法。

分割資料以進行定型和測試

機器學習模型的目標是識別定型資料中模式。 這些模式會用來使用新資料進行預測。

資料可由 HousingData 等類別建立模型。

public class HousingData
{
    [LoadColumn(0)]
    public float Size { get; set; }

    [LoadColumn(1, 3)]
    [VectorType(3)]
    public float[] HistoricalPrices { get; set; }

    [LoadColumn(4)]
    [ColumnName("Label")]
    public float CurrentPrice { get; set; }
}

假設下列資料已載入至 IDataView

HousingData[] housingData = new HousingData[]
{
    new HousingData
    {
        Size = 600f,
        HistoricalPrices = new float[] { 100000f ,125000f ,122000f },
        CurrentPrice = 170000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 200000f, 250000f, 230000f },
        CurrentPrice = 225000f
    },
    new HousingData
    {
        Size = 1000f,
        HistoricalPrices = new float[] { 126000f, 130000f, 200000f },
        CurrentPrice = 195000f
    },
    new HousingData
    {
        Size = 850f,
        HistoricalPrices = new float[] { 150000f,175000f,210000f },
        CurrentPrice = 205000f
    },
    new HousingData
    {
        Size = 900f,
        HistoricalPrices = new float[] { 155000f, 190000f, 220000f },
        CurrentPrice = 210000f
    },
    new HousingData
    {
        Size = 550f,
        HistoricalPrices = new float[] { 99000f, 98000f, 130000f },
        CurrentPrice = 180000f
    }
};

使用 TrainTestSplit 方法將資料分割成定型及測試集。 其結果將會是一個 TrainTestData 物件,其中包含兩個 IDataView 成員,一個用於定型集,一個用於測試集。 資料分割百分比是由 testFraction 參數所決定。 以下程式碼片段會保留原始資料的 20% 作為測試集。

DataOperationsCatalog.TrainTestData dataSplit = mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
IDataView trainData = dataSplit.TrainSet;
IDataView testData = dataSplit.TestSet;

準備資料

資料需要進行預先處理,才能定型機器學習模型。 資料準備的詳細資訊可在資料準備操作說明文章,以及 transforms page 中找到。

ML.NET 演算法針對輸入資料行類型具有條件約束。 此外,若沒有指定任何值,則會使用預設值作為輸入和輸出資料行名稱。

使用預期的資料行類型

ML.NET 中的機器學習演算法預期收到大小已知浮動向量作為輸入。 當所有資料都已是數字格式,且可一起進行處理時 (例如影像像素),請將 VectorType 屬性套用到您的資料模型。

若資料並非全部都是數字格式,且您希望為每個資料行個別套用不同的資料轉換時,請在所有資料行都已經過處理後使用 Concatenate 方法來將所有個別資料行合併成單一特徵向量,輸出到新資料行。

下列程式碼片段會將 SizeHistoricalPrices 資料行合併成單一特徵向量,輸出到稱為 Features 的新資料行。 因為規模不同,NormalizeMinMax 會套用到 Features 資料行以正常化資料。

// Define Data Prep Estimator
// 1. Concatenate Size and Historical into a single feature vector output to a new column called Features
// 2. Normalize Features vector
IEstimator<ITransformer> dataPrepEstimator =
    mlContext.Transforms.Concatenate("Features", "Size", "HistoricalPrices")
        .Append(mlContext.Transforms.NormalizeMinMax("Features"));

// Create data prep transformer
ITransformer dataPrepTransformer = dataPrepEstimator.Fit(trainData);

// Apply transforms to training data
IDataView transformedTrainingData = dataPrepTransformer.Transform(trainData);

使用預設資料行名稱

ML.NET 演算法會在沒有指定任何項目時使用預設資料行名稱。 所有訓練員都具有一個稱為 featureColumnName 的參數作為演算法的輸入,且當適用時,他們也會針對預期值擁有一個稱為 labelColumnName 的參數。 根據預設,這些值分別是 FeaturesLabel

透過在預先處理期間使用 Concatenate 方法來建立稱為 Features 的新資料行,便不需要在演算法的參數中指定特徵資料行名稱,因為它們已存在於預先處理的 IDataView 中。 標籤資料行是 CurrentPrice,但因為已在資料模型中使用 ColumnName 屬性,ML.NET 會將 CurrentPrice 資料行重新命名成 Label,使其不再需要提供 labelColumnName 參數給機器學習服務演算法的估算。

若您不想要使用預設資料行名稱,請在定義機器學習服務演算法估算時將特徵和標籤資料行的名稱作為參數傳遞,如接下來的程式碼片段所示:

var UserDefinedColumnSdcaEstimator = mlContext.Regression.Trainers.Sdca(labelColumnName: "MyLabelColumnName", featureColumnName: "MyFeatureColumnName");

快取資料

在預設的情況下,如果在處理資料時發現有延遲載入或延遲串流處理資料的情形發生,這表示定型器可能會在定型期間從磁碟載入資料,並逐一查看資料許多遍。 因此,建議針對符合記憶體的資料集進行快取,以減少從磁片載入資料的次數。 使用 AppendCacheCheckpoint 讓快取的完成可作為 EstimatorChain 的一部分。

建議在利用管線中的任何定型器之前使用 AppendCacheCheckpoint

使用以下的 EstimatorChain,在 StochasticDualCoordinateAscent 定型器快取先前估算器的結果前先新增 AppendCacheCheckpoint,以供定型器稍後使用。

// 1. Concatenate Size and Historical into a single feature vector output to a new column called Features
// 2. Normalize Features vector
// 3. Cache prepared data
// 4. Use Sdca trainer to train the model
IEstimator<ITransformer> dataPrepEstimator =
    mlContext.Transforms.Concatenate("Features", "Size", "HistoricalPrices")
        .Append(mlContext.Transforms.NormalizeMinMax("Features"))
        .AppendCacheCheckpoint(mlContext);
        .Append(mlContext.Regression.Trainers.Sdca());

定型機器學習模型

預先處理資料後,請使用 Fit 方法來使用 StochasticDualCoordinateAscent 迴歸演算法定型機器學習模型。

// Define StochasticDualCoordinateAscent regression algorithm estimator
var sdcaEstimator = mlContext.Regression.Trainers.Sdca();

// Build machine learning model
var trainedModel = sdcaEstimator.Fit(transformedTrainingData);

擷取模型參數

在定型模型後,請擷取學習到的 ModelParameters 以進行檢查或重新定型。 LinearRegressionModelParameters 可提供偏差和學習到的相關係數,或是定型模型的權數。

var trainedModelParameters = trainedModel.Model as LinearRegressionModelParameters;

注意

其他模型也具有其工作限定的參數。 例如,K-平均演算法會根據距心將資料放入叢集,KMeansModelParameters 則包含儲存這些所學習到距心的屬性。 若要深入了解,請前往 Microsoft.ML.Trainers API 文件並尋找其名稱中包含 ModelParameters 的類別。

評估模型品質

若要協助選擇最佳的執行模型,評估其在測試資料上的效能非常重要。 請使用 Evaluate 方法來針對定型後的模型測量各種計量。

注意

Evaluate 方法會根據執行的機器學習服務工作類型,產生不同的計量。 如需詳細資訊,請前往 Microsoft.ML.Data API 文件並尋找其名稱中包含 Metrics 的類別。

// Measure trained model performance
// Apply data prep transformer to test data
IDataView transformedTestData = dataPrepTransformer.Transform(testData);

// Use trained model to make inferences on test data
IDataView testDataPredictions = trainedModel.Transform(transformedTestData);

// Extract model metrics and get RSquared
RegressionMetrics trainedModelMetrics = mlContext.Regression.Evaluate(testDataPredictions);
double rSquared = trainedModelMetrics.RSquared;

在先前的程式碼範例中:

  1. 測試資料集已使用先前定義的資料準備轉換進行預先處理。
  2. 定型後的機器學習模型會用來針對測試資料進行預測。
  3. Evaluate 方法中,測試資料集 CurrentPrice 資料行中的值會和新輸出預測的 Score 資料行比較,計算迴歸模型的計量,其中一個的決定係數儲存在 rSquared 變數中。

注意

在此小型範例中,由於資料的限制大小,決定係數是不介於 0 到 1 範圍內的數字。 在現實世界案例中,您應預期介於 0 和 1 之間的值。