-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optuna.py
148 lines (108 loc) · 3.83 KB
/
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
import os
import numpy as np
import tensorflow as tf
import tensorflow_addons as tfa
from loguru import logger
import config
from Train import train
from Model import EEGNet
class OptunaTrainer:
def __init__(self, checkpointPath, epochs, batchsize, logPath=None):
self.checkpointPath = checkpointPath
self.logpath = logPath
self.epochs = epochs
self.batchsize = batchsize
def __call__(self, trial, dataset, crossVal=False, **kwargs):
if isinstance(dataset, tuple):
dataset = {
"noname": dataset
}
info = "Trial #{} metric values:\n".format(trial.number)
metrics = []
for key, value in dataset.items():
if "augmenter" in kwargs:
kwargs["augmenter"].setState(False)
shape = list(value[0].shape[-2:])
shape[1] = int(config.window[1] * config.sampleRate) - int(config.window[0] * config.sampleRate)
model = self.buildModel(trial, shape)
auc, precision = train(
model=model,
dataset=value,
weightsPath=self.checkpointPath,
epochs=self.epochs,
batchsize=self.batchsize,
crossVal=crossVal,
**kwargs
)
info += "{}: auc {:.2f} pr {:.2f}\t".format(key, auc, precision)
metrics.append((auc, precision))
metrics = np.array(metrics)
mean = np.mean(metrics, axis=0).round(2)
median = np.median(metrics, axis=0).round(2)
for i, metric in enumerate(["auc", "precision"]):
info += "\nMetric - {}. Mean: {}\tMedian: {}".format(metric, mean[i], median[i])
logger.info(info)
logger.info(trial.params)
return mean[0]
@staticmethod
def chooseOptimizer(trial):
kwargs = {}
optimizer_options = ["RMSprop", "Adam"]
optimizer_selected = trial.suggest_categorical("optimizer", optimizer_options)
if optimizer_selected == "RMSprop":
kwargs["learning_rate"] = trial.suggest_loguniform("rmsprop_learning_rate", 1e-5, 1e-1)
kwargs["decay"] = trial.suggest_discrete_uniform("rmsprop_decay", 0.85, 0.99, 0.01)
kwargs["momentum"] = trial.suggest_loguniform("rmsprop_momentum", 1e-5, 1e-1)
elif optimizer_selected == "Adam":
kwargs["learning_rate"] = trial.suggest_loguniform("adam_learning_rate", 1e-5, 1e-1)
optimizer = getattr(tf.optimizers, optimizer_selected)(**kwargs)
return optimizer
@staticmethod
def chooseLoss(trial):
loss_functions = {
"binary_crossentropy": tf,
"sigmoid_focal_crossentropy": tfa
}
loss_selected = trial.suggest_categorical("loss", list(loss_functions.keys()))
loss = getattr(loss_functions[loss_selected].losses, loss_selected)
return loss
def buildModel(self, trial, shape):
samples = shape[-1]
assert samples // 2 > 16
temporalLength = int(trial.suggest_discrete_uniform("temporal_length", 16, samples // 2, 4))
dropoutRate = trial.suggest_discrete_uniform("dropout_rate", 0.1, 0.5, 0.05)
D = trial.suggest_int("depth_multiplier", 1, 4)
poolKernel = int(trial.suggest_discrete_uniform("pool_kernel", 4, 16, 2))
model = EEGNet(
categoriesN=2,
electrodes=shape[0],
samples=shape[1],
temporalLength=temporalLength,
dropoutRate=dropoutRate,
D=D,
poolPad="same",
poolKernel=poolKernel
)
optimizer = self.chooseOptimizer(trial)
loss = self.chooseLoss(trial)
model.compile(
loss=loss,
optimizer=optimizer,
metrics=["accuracy"]
)
return model
def studyInfo(study, bestN=7, file=None):
logger.info("Number of finished trials: {}", len(study.trials))
logger.info("Best {} trials:".format(bestN))
trials = sorted(study.trials, key=lambda elem: elem.value, reverse=True)[:bestN]
for i, trial in enumerate(trials):
logger.info("Trial {}", i)
logger.info("\tValue: {:.2f}", trial.value)
logger.info("\tParams: ")
for key, value in trial.params.items():
logger.info("\t\t{}: {}", key, value)
if file is not None:
os.makedirs(os.path.dirname(file), exist_ok=True)
studyDF = study.trials_dataframe()
studyDF.to_csv(file)
logger.info("Study file has been written to {}", file)