[英]How to interperet the 'num_layers' line when using Keras Tuner
我正在閱讀一篇關於在 keras 調諧器中調整超參數的文章。 它包括構建具有以下代碼的 model 的代碼:
def build_model(hp):
"""
Builds model and sets up hyperparameter space to search.
Parameters
----------
hp : HyperParameter object
Configures hyperparameters to tune.
Returns
-------
model : keras model
Compiled model with hyperparameters to tune.
"""
# Initialize sequential API and start building model.
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
# Tune the number of hidden layers and units in each.
# Number of hidden layers: 1 - 5
# Number of Units: 32 - 512 with stepsize of 32
for i in range(1, hp.Int("num_layers", 2, 6)):
model.add(
keras.layers.Dense(
units=hp.Int("units_" + str(i), min_value=32, max_value=512, step=32),
activation="relu")
)
# Tune dropout layer with values from 0 - 0.3 with stepsize of 0.1.
model.add(keras.layers.Dropout(hp.Float("dropout_" + str(i), 0, 0.3, step=0.1)))
# Add output layer.
model.add(keras.layers.Dense(units=10, activation="softmax"))
# Tune learning rate for Adam optimizer with values from 0.01, 0.001, or 0.0001
hp_learning_rate = hp.Choice("learning_rate", values=[1e-2, 1e-3, 1e-4])
# Define optimizer, loss, and metrics
model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss=keras.losses.SparseCategoricalCrossentropy(),
metrics=["accuracy"])
return model
我對num_layers
行的 function 范圍內的數字 1,2 和 6 的含義感到困惑。
如果您看到docs ,則 2 和 6 分別指的是最小值和最大值。 另請注意:
[...] max_value 包含在此參數可以采用的可能值中
所以這一行:
for i in range(1, hp.Int("num_layers", 2, 6)):
基本上意味着:生成x
個Dense
層,其中x
介於 1 和 5 之間,范圍從 2 到 6 是您正在處理/調整的超參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.