共用方式為


超參數微調 (預覽)

超參數微調是尋找機器學習模型在定型期間未學習的參數最佳值的程式,而是在定型程序開始之前由用戶設定。 這些參數通常稱為超參數,範例包括學習速率、神經網路中的隱藏層數目、正規化強度和批次大小。

機器學習模型的效能對超參數的選擇非常敏感,而最佳超參數集可能會因特定問題和數據集而有很大的差異。 因此,超參數微調是機器學習管線中的重要步驟,因為它可能會對模型的精確度和一般化效能產生重大影響。

在 Fabric 中,數據科學家可以利用 FLAML輕量型 Python 連結庫,以有效率地自動化機器學習和 AI 作業,以滿足其超參數微調需求。 在網狀架構筆記本中,用戶可以呼叫 flaml.tune 經濟超參數微調。

重要

這項功能處於預覽狀態

微調工作流程

有三個基本步驟可用來 flaml.tune 完成基本微調工作:

  1. 指定超參數的微調目標。
  2. 指定超參數的搜尋空間。
  3. 指定微調條件約束,包括資源預算的條件約束,以執行微調、組態的條件約束,或 (或多個) 特定計量的條件約束。

微調目標

第一個步驟是指定微調目標。 若要這樣做,您應該先在使用者定義的 evaluation_function函數中指定超參數的評估程式。 函式需要超參數位態做為輸入。 它可以直接傳回純量中的計量值,或傳回計量名稱和計量值組的字典。

在下列範例中,我們可以針對名為 xy的 2 個超參數定義評估函式。

import time

def evaluate_config(config: dict):
    """evaluate a hyperparameter configuration"""
    score = (config["x"] - 85000) ** 2 - config["x"] / config["y"]


    faked_evaluation_cost = config["x"] / 100000
    time.sleep(faked_evaluation_cost)
    # we can return a single float as a score on the input config:
    # return score
    # or, we can return a dictionary that maps metric name to metric value:
    return {"score": score, "evaluation_cost": faked_evaluation_cost, "constraint_metric": config["x"] * config["y"]}

搜尋空間

接下來,我們將指定超參數的搜尋空間。 在搜尋空間中,您必須指定超參數的有效值,以及如何取樣這些值(例如,從統一分佈或記錄統一分佈)。 在下列範例中,我們可以提供超參數 xy的搜尋空間。 兩者的有效值為介於 [1, 100,000] 的整數。 這些超參數會在指定的範圍中統一取樣。

from flaml import tune

# construct a search space for the hyperparameters x and y.
config_search_space = {
    "x": tune.lograndint(lower=1, upper=100000),
    "y": tune.randint(lower=1, upper=100000)
}

# provide the search space to tune.run
tune.run(..., config=config_search_space, ...)

透過 FLAML,使用者可以針對特定超參數自定義網域。 這可讓使用者指定要從中取樣參數的類型有效範圍。 FLAML 支援下列超參數類型:float、integer 和類別。 您可以針對常用的網域看到下列範例:

config = {
    # Sample a float uniformly between -5.0 and -1.0
    "uniform": tune.uniform(-5, -1),

    # Sample a float uniformly between 3.2 and 5.4,
    # rounding to increments of 0.2
    "quniform": tune.quniform(3.2, 5.4, 0.2),

    # Sample a float uniformly between 0.0001 and 0.01, while
    # sampling in log space
    "loguniform": tune.loguniform(1e-4, 1e-2),

    # Sample a float uniformly between 0.0001 and 0.1, while
    # sampling in log space and rounding to increments of 0.00005
    "qloguniform": tune.qloguniform(1e-4, 1e-1, 5e-5),

    # Sample a random float from a normal distribution with
    # mean=10 and sd=2
    "randn": tune.randn(10, 2),

    # Sample a random float from a normal distribution with
    # mean=10 and sd=2, rounding to increments of 0.2
    "qrandn": tune.qrandn(10, 2, 0.2),

    # Sample a integer uniformly between -9 (inclusive) and 15 (exclusive)
    "randint": tune.randint(-9, 15),

    # Sample a random uniformly between -21 (inclusive) and 12 (inclusive (!))
    # rounding to increments of 3 (includes 12)
    "qrandint": tune.qrandint(-21, 12, 3),

    # Sample a integer uniformly between 1 (inclusive) and 10 (exclusive),
    # while sampling in log space
    "lograndint": tune.lograndint(1, 10),

    # Sample a integer uniformly between 2 (inclusive) and 10 (inclusive (!)),
    # while sampling in log space and rounding to increments of 2
    "qlograndint": tune.qlograndint(2, 10, 2),

    # Sample an option uniformly from the specified choices
    "choice": tune.choice(["a", "b", "c"]),
}

若要深入瞭解如何在搜尋空間內自定義網域,請瀏覽 有關自定義搜尋空間的 FLAML 檔。

微調條件約束

最後一個步驟是指定微調工作的條件約束。 的一個值得注意的屬性 flaml.tune 是,它能夠在所需的資源條件約束內完成微調程式。 若要這樣做,使用者可以使用 自變數或在使用 num_samples 自變數的試驗數目方面,提供time_budget_s時鐘時間(以秒為單位)的資源條件約束。

# Set a resource constraint of 60 seconds wall-clock time for the tuning.
flaml.tune.run(..., time_budget_s=60, ...)

# Set a resource constraint of 100 trials for the tuning.
flaml.tune.run(..., num_samples=100, ...)

# Use at most 60 seconds and at most 100 trials for the tuning.
flaml.tune.run(..., time_budget_s=60, num_samples=100, ...)

若要深入瞭解新增設定條件約束,您可以流覽 FLAML 檔以取得進階微調選項

將它放在一起

定義微調準則之後,就可以執行微調試用版。 若要追蹤試用的結果,我們可以利用 MLFlow自動記錄來擷取 每個回合的計量和參數。 此程式代碼會擷取整個超參數微調試用版,並醒目提示 FLAML 所探索的每個超參數組合。

import mlflow
mlflow.set_experiment("flaml_tune_experiment")
mlflow.autolog(exclusive=False)

with mlflow.start_run(nested=True, run_name="Child Run: "):
    analysis = tune.run(
        evaluate_config,  # the function to evaluate a config
        config=config_search_space,  # the search space defined
        metric="score",
        mode="min",  # the optimization mode, "min" or "max"
        num_samples=-1,  # the maximal number of configs to try, -1 means infinite
        time_budget_s=10,  # the time budget in seconds
    )

注意

啟用 MLflow 自動記錄時,應該在 MLFlow 執行時自動記錄計量、參數和模型。 不過,這會因架構而異。 可能無法記錄特定模型的計量和參數。 例如,不會記錄 XGBoost、LightGBM、Spark 和 SynapseML 模型的計量。 您可以使用 MLFlow 自動記錄檔,深入瞭解從每個架構擷取哪些計量和參數。

使用 Apache Spark 進行平行微調

此功能 flaml.tune 支援調整 Apache Spark 和單一節點學習工具。 此外,當微調單一節點學習者(例如 Scikit-Learn 學習者)時,您也可以藉由設定 use_spark = True來平行處理微調以加速微調程式。 針對 Spark 叢集,FLAML 預設會針對每個執行程式啟動一個試用版。 您也可以使用 n_concurrent_trials 自變數來自定義並行試用版的數目。


analysis = tune.run(
    evaluate_config,  # the function to evaluate a config
    config=config_search_space,  # the search space defined
    metric="score",
    mode="min",  # the optimization mode, "min" or "max"
    num_samples=-1,  # the maximal number of configs to try, -1 means infinite
    time_budget_s=10,  # the time budget in seconds
    use_spark=True,
)
print(analysis.best_trial.last_result)  # the best trial's result
print(analysis.best_config)  # the best config

若要深入瞭解如何平行處理微調線索,您可以瀏覽適用於平行 Spark 作業FLAML 檔。

將結果可視化

模組 flaml.visualization 提供公用程式函式,以使用 Plotly 繪製優化程式。 藉由利用 Plotly,用戶可以以互動方式探索其 AutoML 實驗結果。 若要使用這些繪圖函式,只要提供優化 flaml.AutoMLflaml.tune.tune.ExperimentAnalysis 物件做為輸入即可。

您可以在筆記本內使用下列函式:

  • plot_optimization_history:繪製實驗中所有試驗的優化歷程記錄。
  • plot_feature_importance:為數據集中的每個功能繪製重要性。
  • plot_parallel_coordinate:在實驗中繪製高維度參數關聯性。
  • plot_contour:將參數關聯性繪製為實驗中的輪廓圖。
  • plot_edf:繪製實驗的目標值EDF(經驗分佈函數)。
  • plot_timeline:繪製實驗的時間軸。
  • plot_slice:將參數關聯性繪製為研究中的配量圖。
  • plot_param_importance:繪製實驗的超參數重要性。

下一步