28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 | class HyperparameterTuning:
"""
A class dedicated to performing hyperparameter tuning using Bayesian optimization for various types of models.
It supports both cross-validation and single validation set approaches, incorporates early stopping, and allows
for live loss plotting during the training process for interactive usage.
Configuration for the hyperparameter space can be loaded from
external configuration files in yam format.
The class is designed to handle both GPU and CPU environments.
Attributes:
dataset: Dataset used for model training and validation.
model_class: Class of the model for which hyperparameters are being tuned.
target_variables: List of target variables the model predicts.
batch_variables: List of batch variables used in model training, if applicable.
config_name: Configuration name which specifies the hyperparameter search space.
n_iter: Number of iterations for the Bayesian optimization process.
plot_losses: Boolean indicating whether to plot losses during training.
cv_splits: Number of cross-validation splits (if user requested cross-validation)
use_loss_weighting: Boolean indicating whether to use loss weighting in the model.
early_stop_patience: Number of epochs with no improvement after which training will be stopped early.
device_type: Type of device ('gpu' or 'cpu') to be used for training.
gnn_conv_type: Specific convolution type if using Graph Neural Networks, otherwise None.
input_layers: Specific input layers for models that require detailed layer setup.
output_layers: Specific output layers for models that require detailed layer setup.
Methods:
__init__(dataset, model_class, config_name, target_variables, batch_variables=None, surv_event_var=None,
surv_time_var=None, n_iter=10, config_path=None, plot_losses=False, val_size=0.2, use_cv=False,
cv_splits=5, use_loss_weighting=True, early_stop_patience=-1, device_type=None, gnn_conv_type=None,
input_layers=None, output_layers=None): Initializes the hyperparameter tuner with specific settings.
get_batch_space(min_size=16, max_size=128): Determines the batch size search space based on the dataset size.
setup_trainer(params, current_step, total_steps, full_train=False): Sets up the trainer with appropriate callbacks
and configurations for either full training or validation based training.
objective(params, current_step, total_steps, full_train=False): Evaluates a set of parameters to determine the
performance of the model using the specified parameters.
perform_tuning(hpo_patience=0): Executes the hyperparameter tuning process, optionally with patience for early
stopping based on no improvement in performance.
init_early_stopping(): Initializes the early stopping mechanism to stop training when validation loss does not
improve for a specified number of epochs.
load_and_convert_config(config_path): Loads a configuration file and converts it into a format suitable for
specifying search spaces in Bayesian optimization.
"""
def __init__(self, dataset, model_class, config_name, target_variables,
batch_variables = None, surv_event_var = None, surv_time_var = None,
n_iter = 10, config_path = None, plot_losses = False,
val_size = 0.2, use_cv = False, cv_splits = 5,
use_loss_weighting = True, early_stop_patience = -1,
device_type = None, gnn_conv_type = None,
input_layers = None, output_layers = None, num_workers = 2):
self.dataset = dataset # dataset for model initiation
self.loader_dataset = dataset # dataset for defining data loaders (this can be model specific)
self.model_class = model_class
self.target_variables = target_variables
self.device_type = device_type
if self.device_type is None:
self.device_type = "gpu" if torch.cuda.is_available() else "cpu"
self.surv_event_var = surv_event_var
self.surv_time_var = surv_time_var
self.batch_variables = batch_variables
self.config_name = config_name
self.n_iter = n_iter
self.plot_losses = plot_losses # Whether to show live loss plots (useful in interactive mode)
self.val_size = val_size
self.use_cv = use_cv
self.n_splits = cv_splits
self.progress_bar = RichProgressBar(
theme = RichProgressBarTheme(
progress_bar = 'green1',
metrics = 'yellow', time='gray',
progress_bar_finished='red'))
self.early_stop_patience = early_stop_patience
self.use_loss_weighting = use_loss_weighting
self.gnn_conv_type = gnn_conv_type
self.input_layers = input_layers
self.output_layers = output_layers
self.num_workers = num_workers
self.DataLoader = torch.utils.data.DataLoader # use torch data loader by default
if self.model_class.__name__ == 'MultiTripletNetwork':
self.loader_dataset = TripletMultiOmicDataset(self.dataset, self.target_variables[0])
# If config_path is provided, use it
if config_path:
external_config = self.load_and_convert_config(config_path)
if self.config_name in external_config:
self.space = external_config[self.config_name]
else:
raise ValueError(f"'{self.config_name}' not found in the provided config file.")
else:
if self.config_name in search_spaces:
self.space = search_spaces[self.config_name]
# get batch sizes (a function of dataset size)
self.space.append(self.get_batch_space())
else:
raise ValueError(f"'{self.config_name}' not found in the default config.")
def get_batch_space(self, min_size = 32, max_size = 128):
m = int(np.log2(len(self.dataset) * 0.8))
st = int(np.log2(min_size))
end = int(np.log2(max_size))
if m < end:
end = m
s = Categorical([np.power(2, x) for x in range(st, end+1)], name = 'batch_size')
return s
def setup_trainer(self, params, current_step, total_steps, full_train = False):
# Configure callbacks and trainer for the current fold
mycallbacks = [self.progress_bar]
if self.plot_losses:
mycallbacks.append(LiveLossPlot(hyperparams=params, current_step=current_step, total_steps=total_steps))
# when training on a full dataset; no cross-validation or no validation splits;
# we don't do early stopping
early_stop_callback = None
if self.early_stop_patience > 0 and full_train == False:
early_stop_callback = self.init_early_stopping()
mycallbacks.append(early_stop_callback)
trainer = pl.Trainer(
#deterministic = True,
precision = '16-mixed', # mixed precision training
max_epochs=int(params['epochs']),
gradient_clip_val=1.0,
gradient_clip_algorithm='norm',
log_every_n_steps=5,
callbacks=mycallbacks,
default_root_dir="./",
logger=False,
enable_checkpointing=False,
devices=1,
accelerator=self.device_type
)
return trainer, early_stop_callback
def objective(self, params, current_step, total_steps, full_train = False):
# Unpack or construct specific model arguments
model_args = {
"config": params,
"dataset": self.dataset,
"target_variables": self.target_variables,
"batch_variables": self.batch_variables,
"surv_event_var": self.surv_event_var,
"surv_time_var": self.surv_time_var,
"use_loss_weighting": self.use_loss_weighting,
"device_type": self.device_type,
}
if self.model_class.__name__ == 'GNN':
model_args['gnn_conv_type'] = self.gnn_conv_type
if self.model_class.__name__ == 'CrossModalPred':
model_args['input_layers'] = self.input_layers
model_args['output_layers'] = self.output_layers
if full_train:
# Train on the full dataset
full_loader = self.DataLoader(self.loader_dataset, batch_size=int(params['batch_size']),
shuffle=True, pin_memory=True, drop_last=True)
model = self.model_class(**model_args)
trainer, _ = self.setup_trainer(params, current_step, total_steps, full_train = True)
trainer.fit(model, train_dataloaders=full_loader)
return model # Return the trained model
else:
validation_losses = []
epochs = []
if self.use_cv: # if the user asks for cross-validation
kf = KFold(n_splits=self.n_splits, shuffle=True)
split_iterator = kf.split(self.loader_dataset)
else: # otherwise do a single train/validation split
# Compute the number of samples for training based on the ratio
num_val = int(len(self.loader_dataset) * self.val_size)
num_train = len(self.loader_dataset) - num_val
train_subset, val_subset = random_split(self.loader_dataset, [num_train, num_val])
# create single split format similar to KFold
split_iterator = [(list(train_subset.indices), list(val_subset.indices))]
i = 1
model = None # save the model if not using cross-validation
for train_index, val_index in split_iterator:
print(f"[INFO] {'training cross-validation fold' if self.use_cv else 'training validation split'} {i}")
train_subset = torch.utils.data.Subset(self.loader_dataset, train_index)
val_subset = torch.utils.data.Subset(self.loader_dataset, val_index)
train_loader = self.DataLoader(train_subset, batch_size=int(params['batch_size']),
pin_memory=True, shuffle=True, drop_last=True, num_workers = self.num_workers, prefetch_factor = None,
persistent_workers = self.num_workers > 0)
val_loader = self.DataLoader(val_subset, batch_size=int(params['batch_size']),
pin_memory=True, shuffle=False, num_workers = self.num_workers, prefetch_factor = None,
persistent_workers = self.num_workers > 0)
model = self.model_class(**model_args)
trainer, early_stop_callback = self.setup_trainer(params, current_step, total_steps)
print(f"[INFO] hpo config:{params}")
trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=val_loader)
if early_stop_callback.stopped_epoch:
epochs.append(early_stop_callback.stopped_epoch)
else:
epochs.append(int(params['epochs']))
validation_result = trainer.validate(model, dataloaders=val_loader)
val_loss = validation_result[0]['val_loss']
validation_losses.append(val_loss)
i += 1
if not self.use_cv:
model = model
# Calculate average validation loss across all folds
avg_val_loss = np.mean(validation_losses)
avg_epochs = int(np.mean(epochs))
return avg_val_loss, avg_epochs, model
def perform_tuning(self, hpo_patience = 0):
opt = Optimizer(dimensions=self.space, n_initial_points=10, acq_func="gp_hedge", acq_optimizer="auto")
best_loss = np.inf
best_params = None
best_epochs = 0
best_model = None
# keep track of the streak of HPO iterations without improvement
no_improvement_count = 0
with tqdm(total=self.n_iter, desc='Tuning Progress') as pbar:
for i in range(self.n_iter):
np.int = int # Ensure int type is correctly handled
suggested_params_list = opt.ask()
suggested_params_dict = {param.name: value for param, value in zip(self.space, suggested_params_list)}
loss, avg_epochs, model = self.objective(suggested_params_dict, current_step=i+1, total_steps=self.n_iter)
if self.use_cv:
print(f"[INFO] average 5-fold cross-validation loss {loss} for params: {suggested_params_dict}")
opt.tell(suggested_params_list, loss)
if loss < best_loss:
best_loss = loss
best_params = suggested_params_list
best_epochs = avg_epochs
best_model = model
no_improvement_count = 0 # Reset the no improvement counter
else:
no_improvement_count += 1 # Increment the no improvement counter
# Print result of each iteration
pbar.set_postfix({'Iteration': i+1, 'Best Loss': best_loss})
pbar.update(1)
# Early stopping condition
if no_improvement_count >= hpo_patience & hpo_patience > 0:
print(f"No improvement in best loss for {hpo_patience} iterations, stopping hyperparameter optimisation early.")
break # Break out of the loop
best_params_dict = {param.name: value for param, value in zip(self.space, best_params)} if best_params else None
print(f"[INFO] current best val loss: {best_loss}; best params: {best_params_dict} since {no_improvement_count} hpo iterations")
# Convert best parameters from list to dictionary and include epochs
best_params_dict = {param.name: value for param, value in zip(self.space, best_params)}
best_params_dict['epochs'] = best_epochs
if self.use_cv:
# Build a final model based on best parameters if using cross-validation
print(f"[INFO] Building a final model using best params: {best_params_dict}")
best_model = self.objective(best_params_dict, current_step=0, total_steps=1, full_train=True)
return best_model, best_params_dict
def init_early_stopping(self):
"""Initialize the early stopping callback."""
return EarlyStopping(
monitor='val_loss',
patience=self.early_stop_patience,
verbose=False,
mode='min'
)
def load_and_convert_config(self, config_path):
# Ensure the config file exists
if not os.path.isfile(config_path):
raise ValueError(f"Config file '{config_path}' doesn't exist.")
# Read the config file
if config_path.endswith('.yaml') or config_path.endswith('.yml'):
with open(config_path, 'r') as file:
loaded_config = yaml.safe_load(file)
else:
raise ValueError("Unsupported file format. Use .yaml or .yml")
# Convert to skopt space
search_space_user = {}
for model, space_definition in loaded_config.items():
space = []
for entry in space_definition:
entry_type = entry.pop("type")
if entry_type == "Integer":
space.append(Integer(**entry))
elif entry_type == "Real":
space.append(Real(**entry))
elif entry_type == "Categorical":
space.append(Categorical(**entry))
else:
raise ValueError(f"Unknown space type: {entry_type}")
search_space_user[model] = space
return search_space_user
|