-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
222 lines (179 loc) · 6.62 KB
/
main.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
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
#!/usr/bin/env python3
"""
MAIN
"""
import torch
from pathlib import Path
from tasks import images_cls
from tasks import speech_cls
from tasks import text_cls
from tester import Tester
import pytorch_helper as ph
import helper as h
import random
def grid_search(task, args):
"""
Performs the grid search. Finds the best parameters for the given task and the given grid
Args:
task: a tuple with
task_name: str the name of the task (images_cls, text_cls, speech_cls)
task_model: torch.Module
train_dataset: torch.utils.data.DataLoader
test_dataset: not used here!
scoring_func: a lambda function that returns the score for a model and labeled data.
Returns:
best_params_per_optimizer: an object indexed by the optimizer name, with
the best parameters found using the grid search.
"""
(task_name, task_model, train_dataset, X, scoring_func) = task
if args.verbose:
print("=" * 60 + f"\nGrid Search for tasks : {task_name}")
param_filename = h.get_param_filepath(task_name, best=False)
print(param_filename)
combinations = h.get_params_combinations(param_filename)
# start of the grid search
if args.verbose:
print(
"Testing {} combinations in total".format(
sum([len(i) for i in combinations.values()])
)
)
print(f"Performing grid search on {len(train_dataset)} examples.")
best_params_per_optimizer = {}
for optim, params in combinations.items():
best_param = None
best_cv_accuracy = None
best_cv_epoch = None
for i, param in enumerate(params):
if args.verbose:
print(f"\n{i}. Testing {optim} with {param}")
tester = Tester(
args=args,
task_name=task_name,
train_dataset=train_dataset,
test_dataset=None,
task_model=task_model,
optimizer=optim,
param=param,
scoring_func=scoring_func,
num_epochs=h.get_default_num_epochs(task_name),
)
# Run the cross validation phase
(
train_losses,
train_accuracies,
val_losses,
val_accuracies,
test_losses,
test_accuracies,
) = tester.run(do_cv=True)
# Update the best parameter combination if we specified the --overwrite_best_param argument
best_param, best_cv_epoch, best_cv_accuracy = h.compute_best_parameter(
val_accuracies=val_accuracies,
best_param=best_param,
best_cv_epoch=best_cv_epoch,
best_cv_accuracy=best_cv_accuracy,
param=param,
optimizer=optim,
verbose=True,
)
best_params_per_optimizer[optim] = {
"num_epochs": best_cv_epoch,
"param": best_param,
}
return best_params_per_optimizer
def main():
# setting-up logs
LOG_DIRECTORY = Path("log")
LOG_DIRECTORY.mkdir(exist_ok=True)
LOG_FILENAME = LOG_DIRECTORY / "results.log"
"""logging.basicConfig(
filename=LOG_FILENAME,
filemode="a",
format="%(asctime)s, %(message)s",
datefmt="%d-%b %H:%M",
level=logging.INFO,
)
"""
# Get arguments
args = h.parse_arguments()
# Set the torch seed
torch.manual_seed(args.seed)
torch.manual_seed(args.seed)
random.seed(args.seed)
tasks_to_evaluate = []
if args.task_name == "images_cls" or args.task_name == "all":
task = (
"images_cls",
images_cls.get_model(),
*ph.split_train_test(
images_cls.get_full_dataset(args.sample_size), args.train_size_ratio
),
images_cls.get_scoring_function(),
)
tasks_to_evaluate.append(task)
if args.task_name == "speech_cls" or args.task_name == "all":
task = (
"speech_cls",
speech_cls.get_model(),
*ph.split_train_test(
speech_cls.get_full_dataset(args.sample_size), args.train_size_ratio
),
speech_cls.get_scoring_function(),
)
tasks_to_evaluate.append(task)
if args.task_name == "text_cls" or args.task_name == "all":
task = (
"text_cls",
text_cls.get_model(),
*text_cls.get_train_test_dataset(args.sample_size),
text_cls.get_scoring_function(),
)
tasks_to_evaluate.append(task)
if len(tasks_to_evaluate) == 0:
raise ValueError(
"task_name must be either 'images_cls', 'speech_cls', 'text_cls' or 'all'."
)
if args.grid_search:
for task in tasks_to_evaluate:
best_params = grid_search(task, args)
if args.overwrite_best_param:
if args.verbose:
print(
"Override best parameters for task {} with {}".format(
task[0], best_params
)
)
# We want to overwrite the best parameters
h.override_best_parameters(task[0], best_params)
# TODO. Do w
else:
for task in tasks_to_evaluate:
(task_name, task_model, train_dataset, test_dataset, scoring_func) = task
best_params_task = h.get_best_parameters(task_name)
if args.optimizer == "all":
all_optimizers = h.str_2_optimizer.keys()
else:
all_optimizers = [args.optimizer]
for optim_name in all_optimizers:
best_param_optim = best_params_task[optim_name]
best_num_epochs = best_param_optim["num_epochs"]
print(
"=" * 60
+ f"\nEvaluate {task_name} with optim {optim_name} on {args.num_runs} runs with best parameters"
f" {best_param_optim} for {best_num_epochs} epochs."
)
tester = Tester(
args=args,
task_name=task_name,
train_dataset=train_dataset,
test_dataset=test_dataset,
task_model=task_model,
optimizer=optim_name,
param=best_param_optim["param"],
scoring_func=scoring_func,
num_epochs=best_num_epochs,
)
tester.run(num_runs=args.num_runs)
if __name__ == "__main__":
main()