-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_hyper_optim_optuna.py
155 lines (116 loc) · 4.84 KB
/
models_hyper_optim_optuna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import optuna
import torchmetrics
import torch
import torch.optim as optim
import models
import neural_net_model_train
import os
def createFolder(path: str):
if not os.path.exists(path):
# Create a new directory because it does not exist
os.makedirs(path)
else:
# empty files when a new tunning starts
for file in os.scandir(path):
os.remove(file.path)
def computeMean(values, mean_metric=torchmetrics.MeanMetric(nan_strategy="warn")):
mean_metric.update(values)
mean_value = mean_metric.compute()
mean_metric.reset()
return mean_value
def print_optimization_results(study:optuna.study.study.Study, args_trial):
print("Number of finished trials: {}".format(len(study.trials)))
print("Best trial:")
print('number of the best trial: ',study.best_trial.number)
best_trial = study.best_trial
print(" Value: {}".format(best_trial.value))
print(" Params: ")
for key, value in best_trial.params.items():
print(" {}: {}".format(key, value))
print(" Evaluation function: {}".format(args_trial['evaluation_function']))
def start_optimize_objective(
study: optuna.Study,
args,
path_models_id,
models_interface: models.ModelsInterface,
models_trainning: neural_net_model_train.ModelsTrainning,
):
evaluation_function = args["evaluation_function"]
no_trials = args["no_trials"]
USE_AUTOMATIC_MIXED_PRECISION = args["USE_AUTOMATIC_MIXED_PRECISION"]
# Define a set of hyperparameter values, build the model, train the model, and evaluate the accuracy
def objective(trial: optuna.Trial):
accuracies, losses, f1_scores, auroc_scores = [0], [0], [0], [0]
net_model = models_interface.define_model(trial)
print(repr(net_model))
# print(type(mlp_model))
# print(dir(mlp_model))
params = {
"learning_rate": trial.suggest_float("learning_rate", 1e-5, 1e-1, log=True),
"optimizer": trial.suggest_categorical(
"optimizer", ["Adam", "RMSprop", "SGD"]
),
}
# Generate the optimizers.
optimizer = getattr(optim, params["optimizer"])(
net_model.parameters(), lr=params["learning_rate"]
)
models_trainning.setParameters(
net_model,
optimizer,
trial_optimisation=trial,
evaluation_function=evaluation_function,
)
# use if Tensor Core is present
if USE_AUTOMATIC_MIXED_PRECISION:
prunned_trial = False
try:
(
accuracies,
losses,
f1_scores,
auroc_scores,
) = models_trainning.trainNetModel_AmpOptimized()
except ValueError as ve:
prunned_trial = True
# check if trainning finished and thus it was not prunned
if not prunned_trial:
# save trained model and continue trainning if network model is fine
checkpoint = models_trainning.getModelCheckpoint_AmpOptimized()
models_interface.saveModel(checkpoint, path_models_id, trial.number)
prunned_trial = False
else:
prunned_trial = False
try:
(
accuracies,
losses,
f1_scores,
auroc_scores,
) = models_trainning.trainNetModel()
except ValueError as ve:
prunned_trial = True
# check if trainning finished and thus it was not prunned
if not prunned_trial:
# save trained model and continue trainning if network model is fine
checkpoint = models_trainning.getModelCheckpoint()
models_interface.saveModel(checkpoint, path_models_id, trial.number)
prunned_trial = False
# the evaluation are scaled between 0 and 1
print(
"\nTrain set: Average loss: {:.4f}, Average accuracy: {:.4f}%,\n \t Average f1_score: {:.4f}, Average Area Under ROC: {:.4f} \n".format(
computeMean(losses),
computeMean(accuracies),
computeMean(f1_scores),
computeMean(auroc_scores),
)
)
# turn off because of RNN :https://github.com/pytorch/captum/issues/564 , https://github.com/pytorch/captum/pull/576
torch.backends.cudnn.enabled = False
test_evaluation_function = models_trainning.testNetModel()
torch.backends.cudnn.enabled = True
net_model=None
return test_evaluation_function
study.optimize(objective, n_trials=no_trials)
#when is ruuning on cpu: n_jobs is 2 or more the training fails with errors
#study.optimize(objective ,timeout = 5 * 60 * 60)