如果您不发布模型代码,我无法确定,但在我的情况下,我需要调整的大多数超参数都在模型的输出层中,并且 base_model 是 InceptionResNetv2。你可以在这里看到:
def build_model(hp):
METRICS = [
'accuracy',
tf.keras.metrics.AUC(name='auc'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall')
]
inputs = tf.keras.Input(shape=(512,512,3))
base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(512,512,3), input_tensor=inputs)
base_model.trainable = False
target_conv_layer = list(filter(lambda x: isinstance(x, tf.keras.layers.Conv2D), base_model.layers))[-1].name
conv_layer = base_model.get_layer(target_conv_layer)
x = GlobalAveragePooling2D()(conv_layer.output)
x = tf.keras.layers.GaussianNoise(hp.Float("gn", min_value=0.3, max_value=0.6, step=0.1))(x)
x = Dense(units=hp.Int("units", min_value=128, max_value=512, step=32), activation="relu", kernel_regularizer="l1")(x)
x = Dropout(hp.Float("dropout", min_value=0.1, max_value=0.6, step=0.1))(x)
predictions = Dense(3, activation="softmax")(x)
model = Model(inputs=inputs, outputs=predictions)
lr = hp.Float("lr", min_value=0.0001, max_value=0.01, sampling="log")
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = lr),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=[METRICS])
return model
因此,您可以将此功能与 keras-tuner 一起使用,以收集模型的最佳超参数,因为输出层已使用您的数据集进行训练,并将学习如何对卷积输出中的图像进行分类。
然后您可以继续使用这些超参数进行微调,据我了解,这些超参数将是“一组超参数”。
如果您想进一步调整超参数,您可以使用您找到的当前超参数保存整个模型,然后定义一个新的 build_model 函数,该函数加载到模型中并设置模型以进行微调。这对我有用 - 但是我不确定这是否是最好的方法。新的 build_model 函数看起来像:
def build_model(hp):
model = tf.keras.models.load_model("your_model")
model.trainable = True
for layer in model.layers[:100]:
layer.trainable = False
lr = hp.Float("lr", min_value=0.0001, max_value=0.01, sampling="log")
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = learning_rate),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[METRICS])