Shi*_*ash 8 python deep-learning keras
我正在尝试使用 Keras Tuner 进行超参数微调。我想最大化auc。任何人都可以帮助我使用kerastuner.Objective自定义指标吗?
EXECUTIONS_PER_TRIAL = 5
b_tuner = BayesianOptimization(
tune_nn_model,
objective='val_binary_accuracy',
max_trials=MAX_TRIALS,
executions_per_trial=EXECUTIONS_PER_TRIAL,
directory='test_dir101897',
project_name='b_tune_nn',
seed=12347
)
Run Code Online (Sandbox Code Playgroud)
我尝试定义一个自定义函数,例如:
from sklearn import metrics
from keras import backend as K
def auc(y_true, y_pred):
auc = tf.metrics.auc(y_true, y_pred)[1]
K.get_session().run(tf.local_variables_initializer())
return auc
Run Code Online (Sandbox Code Playgroud)
并将其插入
objective='val_auc'
Run Code Online (Sandbox Code Playgroud)
但这不起作用
感谢 @Shiva 上面提供的 GitHub 页面,我尝试使用 Keras 调谐器获取验证数据的 AUC,并且它有效。我的模型是 LSTM,我已经使该类MyHyperModel能够batch_size 按照此处所述进行调整。如果您想使用固定的batch_size. 您可以取消注释任何其他指标,并以相同的方式基于它们进行正则化。
# make X_train, y_train, X_valid, y_valid
mask_value=-9999.99
epochs=200
class MyHyperModel(kt.HyperModel):
def build(self, hp):
hp_lstm_units = hp.Int('units', min_value=16, max_value=128, step=16)
hp_dropout_rate = hp.Float('drop_out_rate', min_value=0, max_value=0.6)
hp_recurrent_dropout_rate = hp.Float('recurrent_dropout_rate', min_value=0, max_value=0.6)
hp_initial_learning_rate = hp.Float('initial_learning_rate', min_value=1e-3, max_value=1e-1, sampling='log')
hp_decay = hp.Int('decay', min_value=10, max_value=100, step=10 )
# model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Masking(mask_value=mask_value, input_shape = (X_train.shape[1], X_train.shape[2])))
model.add(tf.keras.layers.LSTM(hp_lstm_units,
dropout=hp_dropout_rate, recurrent_dropout=hp_recurrent_dropout_rate))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),
optimizer=keras.optimizers.SGD(learning_rate=hp_initial_learning_rate, decay=hp_decay),
metrics=[
# tf.keras.metrics.TruePositives(name='tp'),
# tf.keras.metrics.FalsePositives(name='fp'),
# tf.keras.metrics.TrueNegatives(name='tn'),
# tf.keras.metrics.FalseNegatives(name='fn'),
# tf.keras.metrics.BinaryAccuracy(name='accuracy'),
# tf.keras.metrics.Precision(name='precision'),
# tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc'),
])
return model
def fit(self, hp):
hp_batch_size = hp.Int('batch_size', min_value=8, max_value=128, step=8)
return model.fit(
*args,
batch_size=hp_batch_size,
**kwargs)
tuner = kt.BayesianOptimization(
MyHyperModel(),
objective=kt.Objective('val_auc', direction='max'),
overwrite=True,
max_trials=100,
directory="MyDirectory",
project_name="MyProject",
)
tuner.search(X_train, y_train, epochs=200, validation_data=(X_valid, y_valid))
Run Code Online (Sandbox Code Playgroud)