这会有所帮助。https://keras.io/guides/keras_tuner/custom_tuner/自定义调谐器可以是“超参数化” tf 数据集管道的方式。这是我使用的代码片段,它可以工作。
class MyTuner(kt.BayesianOptimization):
def run_trial(self, trial, train_ds, *args, **kwargs):
hp = trial.hyperparameters
train_ds = train_ds.shuffle(batch_size * 8).repeat().batch(batch_size).prefetch(buffer_size=AUTO)
hp_constract_factor = hp.Float('contrast_factor', min_value=0.01, max_value=0.2, sampling='log')
random_flip = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')
random_contrast = tf.keras.layers.experimental.preprocessing.RandomContrast(hp_constract_factor)
train_ds = train_ds.map(lambda x, y: (random_flip(x, training=True), y), num_parallel_calls=AUTO)
train_ds = train_ds.map(lambda x, y: (random_contrast(x, training=True), y), num_parallel_calls=AUTO)
return super(MyTuner, self).run_trial(trial, train_ds, *args, **kwargs)
tuner = MyTuner(
model_builder,
objective='val_sparse_categorical_accuracy',
max_trials=50,
executions_per_trial=1,
directory='keras_tuner',
project_name='classifier_data_aug_custom_tuner'
)
tuner.search(...)