-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyperparameter_search.py
executable file
·416 lines (354 loc) · 18 KB
/
Hyperparameter_search.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python
# coding: utf-8
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # Lower log level to get less clutter
from BERT_per_lvl import run_experiment # Import functions to run experiments
import tensorflow as tf
from tensorboard.plugins.hparams import api as hp
import numpy as np
import sys
# This file should be redone with nicer functions but not time anymore to corroborate the results after changing functions
# Functions in functions not good but otherwise HParams broke
def hyp_search_lvl1_flatt():
"""
Run hyperparameter on the amazon dataset for the flat approach on level 1
:return: HParam run logs
"""
# Set HParams up
HP_MAX_LENGTH = hp.HParam('max_length', hp.Discrete([64, 100, 256, 512]))
HP_BATCH_SIZE = hp.HParam('batch_size', hp.Discrete([45, 20, 40, 50]))
METRIC_ACCURACY = 'accuracy_score'
METRIC_f1 = 'f1_score'
# Simulate config file
arguments = {'model_name': 'bert-base-uncased',
'max_length': 100,
'epochs': 40, #
'batch_size': 40,
'repetitions': 1,
'data_path': 'amazon',
'lvl': 1,
'labels': None,
'test_labels': None,
'hierar': 'flatt',
'lable_type': '_',
'test_labels_type': '_'}
# Get config values
model_name = arguments['model_name']
lvl = arguments['lvl']
data_path = arguments['data_path']
hierar = arguments['hierar']
lable_type = arguments['lable_type']
test_labels_type = arguments['test_labels_type']
# Create custom summary for the HParam logs
with tf.summary.create_file_writer("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(
lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning').as_default():
hp.hparams_config(
hparams=[HP_MAX_LENGTH, HP_BATCH_SIZE],
metrics=[hp.Metric(METRIC_ACCURACY, display_name='accuracy_score'),
hp.Metric(METRIC_f1, display_name='f1_score')],
)
def run(run_dir, hparams, arguments):
"""
Run experiments twice on a set of hparams and log the metrics
:param run_dir: path of log file
:param hparams: dict with parameters to test in this run
:param arguments: config file for the experiment
:return: log of run is saved to path
"""
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
arguments['max_length'] = hparams[HP_MAX_LENGTH] # arguments['max_length']
arguments['batch_size'] = hparams[HP_BATCH_SIZE] # arguments['epochs']
f1_score_1, accuracy_score_1 = run_experiment(arguments, hyp_search=True, )
f1_score_2, accuracy_score_2 = run_experiment(arguments, hyp_search=True, )
f1_score, accuracy_score = np.mean([f1_score_1, f1_score_2]), np.mean([accuracy_score_1, accuracy_score_2])
tf.summary.scalar(METRIC_ACCURACY, accuracy_score, step=1)
tf.summary.scalar(METRIC_f1, f1_score, step=1)
# Experiment counter
session_num = 0
for max_length in HP_MAX_LENGTH.domain.values[::-1]:
for batch_size in HP_BATCH_SIZE.domain.values[::-1]:
hparams = {
HP_MAX_LENGTH: max_length,
HP_BATCH_SIZE: batch_size,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
try:
run("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning/' + run_name, hparams, arguments)
except tf.errors.ResourceExhaustedError as e: # If out of memory error abort this run and test with new hypeparameters.
print("Out of memory")
session_num += 1
# Functions in functions not good but otherwise HParams broke
def hyp_search_lvl2_flatt():
"""
Run hyperparameter on the amazon dataset for the flat approach on level 2
:return: HParam run logs
"""
# Set HParams up
HP_MAX_LENGTH = hp.HParam('max_length', hp.Discrete([100, 256, 512]))
HP_BATCH_SIZE = hp.HParam('batch_size', hp.Discrete([45, 50, 40, 60]))
METRIC_ACCURACY = 'accuracy_score'
METRIC_f1 = 'f1_score'
# Simulate config file
arguments = {'model_name': 'bert-base-uncased',
'max_length': 100,
'epochs': 40, #
'batch_size': 40,
'repetitions': 1,
'data_path': 'amazon',
'lvl': 2,
'labels': None,
'test_labels': None,
'hierar': 'flatt',
'lable_type': '_',
'test_labels_type': '_'}
# Get config values
model_name = arguments['model_name']
lvl = arguments['lvl']
data_path = arguments['data_path']
hierar = arguments['hierar']
lable_type = arguments['lable_type']
test_labels_type = arguments['test_labels_type']
# Create custom summary for the HParam logs
with tf.summary.create_file_writer("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning').as_default():
hp.hparams_config(
hparams=[HP_MAX_LENGTH, HP_BATCH_SIZE],
metrics=[hp.Metric(METRIC_ACCURACY, display_name='accuracy_score'),
hp.Metric(METRIC_f1, display_name='f1_score')],
)
def run(run_dir, hparams, arguments):
"""
Run experiments twice on a set of hparams and log the metrics
:param run_dir: path of log file
:param hparams: dict with parameters to test in this run
:param arguments: config file for the experiment
:return: log of run is saved to path
"""
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
arguments['max_length'] = hparams[HP_MAX_LENGTH] # arguments['max_length']
arguments['batch_size'] = hparams[HP_BATCH_SIZE] # arguments['epochs']
f1_score_1, accuracy_score_1 = run_experiment(arguments, hyp_search=True, )
f1_score_2, accuracy_score_2 = run_experiment(arguments, hyp_search=True, )
f1_score, accuracy_score = np.mean([f1_score_1, f1_score_2]), np.mean([accuracy_score_1, accuracy_score_2])
tf.summary.scalar(METRIC_ACCURACY, accuracy_score, step=1)
tf.summary.scalar(METRIC_f1, f1_score, step=1)
# Experiment counter
session_num = 0
for max_length in HP_MAX_LENGTH.domain.values[::-1]:
for batch_size in HP_BATCH_SIZE.domain.values[::-1]:
hparams = {
HP_MAX_LENGTH: max_length,
HP_BATCH_SIZE: batch_size,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
try:
run("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning/' + run_name, hparams, arguments)
except tf.errors.ResourceExhaustedError as e: # If out of memory error abort this run and test with new hypeparameters.
print("Out of memory")
session_num += 1
# Functions in functions not good but otherwise HParams broke
def hyp_search_lvl2_target_target():
"""
Run hyperparameter on the amazon dataset for the target trained and tested per-level approach on level 2
:return: HParam run logs
"""
HP_MAX_LENGTH = hp.HParam('max_length', hp.Discrete([100, 256, 512]))
HP_BATCH_SIZE = hp.HParam('batch_size', hp.Discrete([45, 50, 40, 60]))
METRIC_ACCURACY = 'accuracy_score'
METRIC_f1 = 'f1_score'
# Simulate config file
arguments = {'model_name': 'bert-base-uncased',
'max_length': 100,
'epochs': 40, #
'batch_size': 40,
'repetitions': 1,
'data_path': 'amazon',
'lvl': 2,
'labels': [['Target', 'Cat1']],
'test_labels': [['Target', 'Cat1']],
'hierar': 'hierarchical',
'lable_type': 'Target',
'test_labels_type': 'Target'}
# Get config values
model_name = arguments['model_name']
lvl = arguments['lvl']
data_path = arguments['data_path']
hierar = arguments['hierar']
lable_type = arguments['lable_type']
test_labels_type = arguments['test_labels_type']
# Create custom summary for the HParam logs
with tf.summary.create_file_writer("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(
lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning').as_default():
hp.hparams_config(
hparams=[HP_MAX_LENGTH, HP_BATCH_SIZE],
metrics=[hp.Metric(METRIC_ACCURACY, display_name='accuracy_score'),
hp.Metric(METRIC_f1, display_name='f1_score')],
)
def run(run_dir, hparams, arguments):
"""
Run experiments twice on a set of hparams and log the metrics
:param run_dir: path of log file
:param hparams: dict with parameters to test in this run
:param arguments: config file for the experiment
:return: log of run is saved to path
"""
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
arguments['max_length'] = hparams[HP_MAX_LENGTH] # arguments['max_length']
arguments['batch_size'] = hparams[HP_BATCH_SIZE] # arguments['epochs']
f1_score_1, accuracy_score_1 = run_experiment(arguments, hyp_search=True, )
f1_score_2, accuracy_score_2 = run_experiment(arguments, hyp_search=True, )
f1_score, accuracy_score = np.mean([f1_score_1, f1_score_2]), np.mean([accuracy_score_1, accuracy_score_2])
tf.summary.scalar(METRIC_ACCURACY, accuracy_score, step=1)
tf.summary.scalar(METRIC_f1, f1_score, step=1)
# Experiment counter
session_num = 0
for max_length in HP_MAX_LENGTH.domain.values[::-1]:
for batch_size in HP_BATCH_SIZE.domain.values[::-1]:
hparams = {
HP_MAX_LENGTH: max_length,
HP_BATCH_SIZE: batch_size,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
try:
run("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(
lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning/' + run_name, hparams, arguments)
except tf.errors.ResourceExhaustedError as e: # If out of memory error abort this run and test with new hypeparameters.
print("Out of memory")
session_num += 1
# Functions in functions not good but otherwise HParams broke
def hyp_search_lvl2_predicted_predicted(path_predicted):
"""
Run hyperparameter on the amazon dataset for the predicted trained and tested per-level approach on level 2
:return: HParam run logs
"""
HP_MAX_LENGTH = hp.HParam('max_length', hp.Discrete([64, 100, 256, 512]))
HP_BATCH_SIZE = hp.HParam('batch_size', hp.Discrete([10, 45, 20, 40, 50, 60]))
METRIC_ACCURACY = 'accuracy_score'
METRIC_f1 = 'f1_score'
# Simulate config file
arguments = {'model_name': 'bert-base-uncased',
'max_length': 100,
'epochs': 40, #
'batch_size': 40,
'repetitions': 1,
'data_path': 'amazon',
'lvl': 2,
'labels': [[path_predicted]],
'test_labels': [[path_predicted]],
'hierar': 'hierarchical',
'lable_type': 'Predicted',
'test_labels_type': 'Predicted'}
# Get config values
model_name = arguments['model_name']
lvl = arguments['lvl']
data_path = arguments['data_path']
hierar = arguments['hierar']
lable_type = arguments['lable_type']
test_labels_type = arguments['test_labels_type']
# Create custom summary for the HParam logs
with tf.summary.create_file_writer("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(
lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning').as_default():
hp.hparams_config(
hparams=[HP_MAX_LENGTH, HP_BATCH_SIZE],
metrics=[hp.Metric(METRIC_ACCURACY, display_name='accuracy_score'),
hp.Metric(METRIC_f1, display_name='f1_score')],
)
def run(run_dir, hparams, arguments):
"""
Run experiments twice on a set of hparams and log the metrics
:param run_dir: path of log file
:param hparams: dict with parameters to test in this run
:param arguments: config file for the experiment
:return: log of run is saved to path
"""
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
arguments['max_length'] = hparams[HP_MAX_LENGTH] # arguments['max_length']
arguments['batch_size'] = hparams[HP_BATCH_SIZE] # arguments['epochs']
f1_score_1, accuracy_score_1 = run_experiment(arguments, hyp_search=True, )
f1_score_2, accuracy_score_2 = run_experiment(arguments, hyp_search=True, )
f1_score, accuracy_score = np.mean([f1_score_1, f1_score_2]), np.mean([accuracy_score_1, accuracy_score_2])
tf.summary.scalar(METRIC_ACCURACY, accuracy_score, step=1)
tf.summary.scalar(METRIC_f1, f1_score, step=1)
# Experiment counter
session_num = 0
for max_length in HP_MAX_LENGTH.domain.values[::-1]:
for batch_size in HP_BATCH_SIZE.domain.values[::-1]:
hparams = {
HP_MAX_LENGTH: max_length,
HP_BATCH_SIZE: batch_size,
}
run_name = "run-%d" % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
try:
run("hyperparameters_search/" + model_name + "/" + data_path + "/lvl" + str(
lvl) + "/trained_" + hierar + "_" + lable_type + "/tested_" + test_labels_type + '/hparam_tuning/' + run_name, hparams, arguments)
except tf.errors.ResourceExhaustedError as e: # If out of memory error abort this run and test with new hypeparameters.
print("Out of memory")
session_num += 1
def main():
"""
Runs hyperparameter search on the amazon dataset for the flatt and per-level approaches depending on the command line arguments.
Run Hyperparameter_search.py to do a grid-search over the predifined hyperparameters. Hyperparameters can only be done over amazon and per_lvl, but neither on DBpedia nor on per_label.
Give one or more options to search hyperparameters: Flat_lvl1, Flat_lvl2, tgt_pred, tgt_tgt, pred_pred.
For runs containing pred (predictions) give the rep_and_histo.npz path that should be used for the input predictions.
For example run "python Hyperparameter_search.py Flat_lvl2 tgt_tgt pred_pred saved_models/bert-base-uncased/amazon/lvl1/trained_flatt__/100T_60e_45b/Run3/tested__/rep_and_histo.npz"
for the hyp-search on amazon level2 flat (Flat_lvl2), target trained and predicted on level 2 (tgt_tgt), and trained and tested with the predicted label input of the flat level 1 (pred_pred saved_models/bert-base-uncased/amazon/lvl1/trained_flatt__/100T_60e_45b/Run3/tested__/rep_and_histo.npz)
"""
print("Tensorflow version: ", tf.__version__)
# rtx 3080 tf 2.4.0-rc4 bug
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpu_devices[0], True)
os.environ["TOKENIZERS_PARALLELISM"] = "false" # avoids Hugging Face process forking bug https://github.com./ThilinaRajapakse/simpletransformers/issues/515
list_args = sys.argv[1:] # Read command line arguments
if len(list_args) < 1: # No given parameters
print(
"Give one or more options to search hyperparameters:\n Flat_lvl1, Flat_lvl2, tgt_pred, tgt_tgt, pred_pred \n for runs containing pre give config and path to model")
sys.exit(2)
for i, conf in enumerate(list_args):
if conf == "Flat_lvl1":
hyp_search_lvl1_flatt()
print("hyp_search_lvl1_flatt done")
print("#" * 150)
print("#" * 150)
print("#" * 150)
print("#" * 150)
elif conf == "Flat_lvl2":
hyp_search_lvl2_flatt()
print("hyp_search_lvl2_flatt done")
print("#" * 150)
print("#" * 150)
print("#" * 150)
print("#" * 150)
continue
elif conf == "tgt_tgt":
hyp_search_lvl2_target_target()
print("hyp_search_lvl2_target_target done")
print("#" * 150)
print("#" * 150)
print("#" * 150)
print("#" * 150)
elif conf == "pred_pred":
print(list_args)
hyp_search_lvl2_predicted_predicted(list_args[i + 1])
print("hyp_search_lvl2_prediction_prediction done")
print("#" * 150)
print("#" * 150)
print("#" * 150)
print("#" * 150)
continue
else:
print("Wrong input options to search hyperparameters:\n Flat_lvl1, Flat_lvl2, tgt_pred, tgt_tgt, pred_pred")
return 1
print("Search done for", list_args)
if __name__ == "__main__":
main()