KerasTuner:超參數調整 / 開發者指南 / 調整搜尋空間

調整搜尋空間

作者:Luca Invernizzi, James Long, Francois Chollet, Tom O'Malley, Haifeng Jin
建立日期 2019/05/31
上次修改日期 2021/10/27
說明:調整超參數的子集,而不變更超模型。

在 Colab 中檢視 GitHub 原始碼

!pip install keras-tuner -q

在本指南中,我們將展示如何在不直接變更 HyperModel 程式碼的情況下調整搜尋空間。例如,您可以只調整部分超參數並保持其餘參數固定,或者您可以覆寫編譯引數,例如 optimizerlossmetrics


超參數的預設值

在調整搜尋空間之前,了解每個超參數都有預設值非常重要。當我們在調整搜尋空間時不調整超參數時,會使用此預設值作為超參數值。

每當您註冊超參數時,可以使用 default 引數來指定預設值

hp.Int("units", min_value=32, max_value=128, step=32, default=64)

如果您不這樣做,超參數總是有預設的預設值(對於 Int,它等於 min_value)。

在以下模型建構函式中,我們將 units 超參數的預設值指定為 64。

import keras
from keras import layers
import keras_tuner
import numpy as np


def build_model(hp):
    model = keras.Sequential()
    model.add(layers.Flatten())
    model.add(
        layers.Dense(
            units=hp.Int("units", min_value=32, max_value=128, step=32, default=64)
        )
    )
    if hp.Boolean("dropout"):
        model.add(layers.Dropout(rate=0.25))
    model.add(layers.Dense(units=10, activation="softmax"))
    model.compile(
        optimizer=keras.optimizers.Adam(
            learning_rate=hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
        ),
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )
    return model

我們將在本教學課程的其餘部分中重複使用此搜尋空間,方法是覆寫超參數而不定義新的搜尋空間。


搜尋一些並固定其餘

如果您有現有的超模型,並且只想搜尋部分超參數,並保持其餘參數固定,則無需變更模型建構函式或 HyperModel 中的程式碼。您可以將 HyperParameters 作為 hyperparameters 引數傳遞給調整器建構函式,其中包含您要調整的所有超參數。指定 tune_new_entries=False 以防止其調整其他超參數,否則將會使用這些超參數的預設值。

在以下範例中,我們只調整 learning_rate 超參數,並變更其類型和值範圍。

hp = keras_tuner.HyperParameters()

# This will override the `learning_rate` parameter with your
# own selection of choices
hp.Float("learning_rate", min_value=1e-4, max_value=1e-2, sampling="log")

tuner = keras_tuner.RandomSearch(
    hypermodel=build_model,
    hyperparameters=hp,
    # Prevents unlisted parameters from being tuned
    tune_new_entries=False,
    objective="val_accuracy",
    max_trials=3,
    overwrite=True,
    directory="my_dir",
    project_name="search_a_few",
)

# Generate random data
x_train = np.random.rand(100, 28, 28, 1)
y_train = np.random.randint(0, 10, (100, 1))
x_val = np.random.rand(20, 28, 28, 1)
y_val = np.random.randint(0, 10, (20, 1))

# Run the search
tuner.search(x_train, y_train, epochs=1, validation_data=(x_val, y_val))
Trial 3 Complete [00h 00m 01s]
val_accuracy: 0.20000000298023224
Best val_accuracy So Far: 0.25
Total elapsed time: 00h 00m 03s

如果您總結搜尋空間,您將只看到一個超參數。

tuner.search_space_summary()
Search space summary
Default search space size: 1
learning_rate (Float)
{'default': 0.0001, 'conditions': [], 'min_value': 0.0001, 'max_value': 0.01, 'step': None, 'sampling': 'log'}

固定一些並調整其餘

在上面的範例中,我們展示了如何只調整部分超參數並保持其餘參數固定。您也可以反其道而行:只固定部分超參數並調整其餘所有超參數。

在以下範例中,我們固定了 learning_rate 超參數的值。傳遞帶有 Fixed 條目(或任意數量的 Fixed 條目)的 hyperparameters 引數。另外請記住指定 tune_new_entries=True,這允許我們調整其餘超參數。

hp = keras_tuner.HyperParameters()
hp.Fixed("learning_rate", value=1e-4)

tuner = keras_tuner.RandomSearch(
    build_model,
    hyperparameters=hp,
    tune_new_entries=True,
    objective="val_accuracy",
    max_trials=3,
    overwrite=True,
    directory="my_dir",
    project_name="fix_a_few",
)

tuner.search(x_train, y_train, epochs=1, validation_data=(x_val, y_val))
Trial 3 Complete [00h 00m 01s]
val_accuracy: 0.15000000596046448
Best val_accuracy So Far: 0.15000000596046448
Total elapsed time: 00h 00m 03s

如果您總結搜尋空間,您將看到 learning_rate 被標記為固定,並且正在調整其餘超參數。

tuner.search_space_summary()
Search space summary
Default search space size: 3
learning_rate (Fixed)
{'conditions': [], 'value': 0.0001}
units (Int)
{'default': 64, 'conditions': [], 'min_value': 32, 'max_value': 128, 'step': 32, 'sampling': 'linear'}
dropout (Boolean)
{'default': False, 'conditions': []}

覆寫編譯引數

如果您有一個超模型,您想要變更現有的最佳化工具、損失或指標,您可以將這些引數傳遞給調整器建構函式

tuner = keras_tuner.RandomSearch(
    build_model,
    optimizer=keras.optimizers.Adam(1e-3),
    loss="mse",
    metrics=[
        "sparse_categorical_crossentropy",
    ],
    objective="val_loss",
    max_trials=3,
    overwrite=True,
    directory="my_dir",
    project_name="override_compile",
)

tuner.search(x_train, y_train, epochs=1, validation_data=(x_val, y_val))
Trial 3 Complete [00h 00m 01s]
val_loss: 29.39796257019043
Best val_loss So Far: 29.39630699157715
Total elapsed time: 00h 00m 04s

如果您取得最佳模型,您可以看到損失函數已變更為 MSE。

tuner.get_best_models()[0].loss
/usr/local/python/3.10.13/lib/python3.10/site-packages/keras/src/saving/saving_lib.py:388: UserWarning: Skipping variable loading for optimizer 'adam', because it has 2 variables whereas the saved optimizer has 10 variables. 
  trackable.load_own_variables(weights_store.get(inner_path))

'mse'

調整預建超模型的搜尋空間

您也可以將這些技巧與 KerasTuner 中的預建模型一起使用,例如 HyperResNetHyperXception。但是,若要查看這些預建 HyperModel 中的超參數是什麼,您必須閱讀原始碼。

在以下範例中,我們只調整 HyperXceptionlearning_rate,並固定所有其餘超參數。由於 HyperXception 的預設損失是 categorical_crossentropy,這預期標籤會是獨熱編碼,這與我們的原始整數標籤資料不符,因此我們需要透過覆寫編譯引數中的 losssparse_categorical_crossentropy 來變更它。

hypermodel = keras_tuner.applications.HyperXception(input_shape=(28, 28, 1), classes=10)

hp = keras_tuner.HyperParameters()

# This will override the `learning_rate` parameter with your
# own selection of choices
hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])

tuner = keras_tuner.RandomSearch(
    hypermodel,
    hyperparameters=hp,
    # Prevents unlisted parameters from being tuned
    tune_new_entries=False,
    # Override the loss.
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
    objective="val_accuracy",
    max_trials=3,
    overwrite=True,
    directory="my_dir",
    project_name="helloworld",
)

# Run the search
tuner.search(x_train, y_train, epochs=1, validation_data=(x_val, y_val))
tuner.search_space_summary()
Trial 3 Complete [00h 00m 19s]
val_accuracy: 0.15000000596046448
Best val_accuracy So Far: 0.20000000298023224
Total elapsed time: 00h 00m 58s
Search space summary
Default search space size: 1
learning_rate (Choice)
{'default': 0.01, 'conditions': [], 'values': [0.01, 0.001, 0.0001], 'ordered': True}