-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid_search.py
156 lines (132 loc) · 6.53 KB
/
grid_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
import json
import os
from pathlib import Path
import matplotlib.pyplot as plt
from bayes_opt import BayesianOptimization
from embedding_head import *
from utils import set_random_seeds
SEEDS = [2, 1984]
EPOCHS = 30
RETRAIN_ON_BEST = False
METRIC = 'val_f1_score'
OPTIMIZER_SEED = 42
INIT_POINTS = 1
N_ITER = 0
PBOUNDS = {}
if METRIC in ['precision', 'recall', 'val_f1_score']:
lr_scheduler_mode = 'max'
elif METRIC in ['train_loss', 'val_loss']:
lr_scheduler_mode = 'min'
else:
raise ValueError('Invalid metric')
def create_training(training_dataloader, qd_dataloader):
def black_box_function(hidden_units=HIDDEN_UNITS,
emb_out=EMB_OUT,
dropout_rate=DROPOUT_RATE,
lr=LR,
pe_weight=PE_WEIGHT,
factor=FACTOR,
threshold=THRESHOLD,
patience=PATIENCE,
cooldown=COOLDOWN,
cosine_loss_margin=COSINE_LOSS_MARGIN,
dynamic_cutoff=DYNAMIC_CUTOFF,
max_docs=MAX_DOCS,
ratio_max_similarity=RATIO_MAX_SIMILARITY,
pe_cutoff=PE_CUTOFF,
sample_size=SAMPLE_SIZE):
scores = []
for s in SEEDS:
set_random_seeds(s)
model = (EmbeddingHead(hidden_units=int(hidden_units), emb_out=int(emb_out), dropout_rate=dropout_rate)
.to('cuda'))
_optimizer = torch.optim.Adam(model.parameters(), lr=lr)
_lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(_optimizer, mode=lr_scheduler_mode, factor=factor,
patience=int(patience), cooldown=int(cooldown),
threshold=threshold)
set_sample_size(int(sample_size))
scores.append(max(train(model=model,
train_dataloader=training_dataloader,
validation_dataloader=qd_dataloader,
num_epochs=EPOCHS,
optimizer=_optimizer,
# loss_function=_loss_function, # DEFAULT
cosine_loss_margin=cosine_loss_margin,
lr_scheduler=_lr_scheduler,
lr_scheduler_mode=lr_scheduler_mode,
# val_loss_function=_val_loss_function, # DEFAULT
# score_function=_score_function, # DEFAULT
dynamic_cutoff=dynamic_cutoff,
pe_weight=pe_weight,
max_docs=int(max_docs),
ratio_max_similarity=ratio_max_similarity,
pe_cutoff=int(pe_cutoff),
metric=METRIC,
save_weights=True,
verbose=True
)[METRIC]
)
)
print(f'Target metric score for seed {s}: {scores[-1]:.6f}')
return sum(scores) / len(scores)
return black_box_function
if __name__ == '__main__':
training_dataloader, qd_dataloader = create_dataloaders('train')
pbounds = PBOUNDS
optimizer = BayesianOptimization(
f=create_training(training_dataloader, qd_dataloader),
pbounds=pbounds,
random_state=OPTIMIZER_SEED,
)
print(f'Beginning optimization of {len(pbounds)} params with {len(SEEDS)} seeds...')
optimizer.maximize(
init_points=INIT_POINTS,
n_iter=N_ITER,
)
print("Best parameters combination: \n\t{}".format(optimizer.max))
plt.figure(figsize=(15, 5))
plt.plot(range(1, 1 + len(optimizer.space.target)), optimizer.space.target, "-o")
plt.grid(True)
plt.xlabel("Iteration", fontsize=12)
plt.ylabel("Black box function", fontsize=12)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.title("Grid search results for parameters: " + ", ".join(list(pbounds.keys())), fontsize=16)
plt.show(block=False)
plt.pause(0.05)
plot_dir = Path.joinpath(Path('Checkpoints'), Path('GridPlots'))
os.makedirs(plot_dir, exist_ok=True)
plt.savefig(Path.joinpath(plot_dir, Path(f'Plot_for_GD_Target_{optimizer.max["target"]}.png')))
# Convert to int the parameters that should be int of the best combination
should_be_int = ['hidden_units', 'emb_out', 'sample_size', 'patience', 'cooldown', 'pe_cutoff', 'max_docs']
best_params = {}
for param in optimizer.max['params']:
if param in should_be_int:
best_params[param] = int(optimizer.max['params'][param])
else:
best_params[param] = optimizer.max['params'][param]
if RETRAIN_ON_BEST:
print('Re-training on best parameters...')
model = (EmbeddingHead(hidden_units=best_params['hidden_units'] if 'hidden_units' in best_params else HIDDEN_UNITS,
emb_out=best_params['emb_out'] if 'emb_out' in best_params else EMB_OUT,
dropout_rate=best_params['dropout_rate'] if 'dropout_rate' in best_params else DROPOUT_RATE)
.to('cuda'))
optimizer = torch.optim.Adam(model.parameters(), lr=best_params['lr'] if 'lr' in best_params else LR)
lr_scheduler = (torch.optim.lr_scheduler.
ReduceLROnPlateau(optimizer, mode=lr_scheduler_mode,
factor=best_params['factor'] if 'factor' in best_params else FACTOR,
patience=best_params['patience'] if 'patience' in best_params else PATIENCE,
cooldown=best_params['cooldown'] if 'cooldown' in best_params else COOLDOWN,
threshold=best_params['threshold'] if 'threshold' in best_params else THRESHOLD)
)
train(model,
training_dataloader,
qd_dataloader,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
num_epochs=EPOCHS * 2,
metric=METRIC,
save_weights=True,
verbose=True,
**best_params
)