记录如何使用BayesianOptimization对神经网络进行调参。
# 划分数据集
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(seq_train, train_label, test_size=0.2, stratify=train_label, random_state=1234)
from tensorflow.keras import layers, models
from tensorflow.keras import metrics
from tensorflow.keras.callbacks import EarlyStopping
input_dim=min(len(word_indexs)+1, TOP_K)
output_dim=128
maxlen=seq_train.shape[1]
## 搭建模型
def get_model(units, learning_rate, drop_rate):
inputs = layers.Input(shape=(maxlen, ))
x = layers.Embedding(input_dim=input_dim, output_dim=output_dim, input_length=maxlen)(inputs)
x = layers.Dropout(drop_rate)(x)
x = layers.Flatten()(x)
x = layers.Dense(units, activation='relu')(x)
x = layers.Dropout(drop_rate)(x)
outputs = layers.Dense(1, activation='sigmoid')(x)
model = models.Model(inputs=inputs, outputs=outputs, name='MLP_opt')
optimizer = tf.keras.optimizers.Adam(learning_rate)
#编译compile
model.compile(optimizer = optimizer, #优化函数
loss = "binary_crossentropy", #损失函数
metrics=["accuracy", metrics.Precision(), metrics.Recall()])
return model
# 训练模型
def train_model(units, learning_rate, drop_rate):
model = get_model(units, learning_rate, drop_rate)
# 提前停止
EarlyStop=EarlyStopping(monitor='val_loss',
patience=2,
verbose=1,
mode='auto')
# 模型拟合
model.fit(x_train,
y_train,
epochs=5,
verbose=0,
batch_size=32,
validation_data=(x_val, y_val),
callbacks = [EarlyStop],
)
# 验证集结果 使用验证集F1作为优化指标
scores = model.evaluate(x_val, y_val)
f1 = 2*(scores[2]*scores[3]) / (scores[2]+scores[3]+1e-10) ## 加小尾巴防止除0
print('验证集Accuracy:', scores[1], '验证集Precision:', scores[2], '验证集Recall:', scores[3])
print('验证集f1:', f1)
return f1 ## 优化指标
from bayes_opt import BayesianOptimization
# 定义参数搜索空间
pbounds = {"units": (32, 64), ## 两个参数 上下边界值
"learning_rate": (0.0001, 0.001),
"drop_rate": (0.2, 0.5)
# "batch_size": (32, 64) ## 无法调
}
# 贝叶斯优化器
optimizer = BayesianOptimization(f=train_model,
pbounds=pbounds,
verbose=2,
random_state=1,)
# 开始优化
optimizer.maximize()
## 打印优化后的参数
print(optimizer.max)