-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_search.py
347 lines (296 loc) · 12.8 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
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
"""Grid search implementation for QuestEA (Questionnaire Embeddings Analysis).
This script performs a comprehensive grid search over different parameters to analyze questionnaire data
using various embedding and clustering techniques.
Usage:
python grid_search.py [--logdir=DIR] [--resultdir=DIR] [--testing] [--debug] [--verbose]
Arguments:
--logdir: Directory for tensorboard logs (default: ./tensorboard_runs)
--resultdir: Directory for results (default: ./results_ignore_backups/)
--testing: Run in testing mode with reduced dataset (default: False)
--debug: Enable debug mode with pdb post-mortem (default: False)
--verbose: Enable verbose output (default: False)
The script:
1. Iterates through multiple datasets:
- Hamilton (98 subjects)
- AdolescentDepressionSRQA (400 subjects)
- AdolescentDepressionCU (400 subjects)
- RutledgeSmartphone (1k subjects)
- DASS (6k subjects)
- HEXACO (12k subjects)
- 16PF (20k subjects)
- IPIP (25k+ subjects)
2. For each dataset, tests combinations of:
- Embedding methods:
* Raw features (feat_raw)
* Aggregated features (feat_agg)
* Various SBERT models (all-mpnet-base-v2, clip-ViT, etc.)
* OpenAI embeddings
* Random embeddings for baseline
- Normalization techniques:
* L2 normalization
- Dimensionality reduction:
* PCA
* UMAP
* Beta-VAE
* Dictionary Learning
- Clustering methods:
* K-means
* Spectral Clustering (with cosine affinity)
* Bisecting K-means
3. Evaluates clustering quality using multiple metrics:
- Davies-Bouldin score
- Calinski-Harabasz score
- Silhouette score
4. Saves results and visualizations:
- Clustering predictions
- Embedding visualizations
- Quality metrics
- Comparison plots
Results can be visualized using tensorboard:
tensorboard --logdir=./tensorboard_runs
The tensorboard interface provides:
- Interactive embedding visualizations
- Clustering quality metrics over time
- Pairwise comparison plots between different methods
- Dataset statistics and metadata
"""
import time
import pdb
import os
from pathlib import Path
from sklearn.model_selection import GridSearchCV, ParameterGrid
from tqdm import tqdm
from shutil import rmtree
from QuestEA import QuestEA
from torch.utils.tensorboard import SummaryWriter
import fire
from utils.compare_results import CompareResultsPairwise
from utils.plotter import main as Plotter
from utils.misc import IgnoreInGrid
print("Deleting cache folder")
rmtree("cache")
def do_grid_search(
logdir="./tensorboard_runs",
resultdir="./results_ignore_backups/",
testing=False,
debug=False,
verbose=False,
**kwargs,
):
start = time.time()
if kwargs:
raise Exception(f"Unexpected parameter: '{kwargs}'")
print("Trashing previous logdir")
os.system(f"trash -i {logdir}")
Path(logdir).mkdir(exist_ok=False)
os.system(f"trash -i {resultdir}")
Path(resultdir).mkdir(exist_ok=False)
failed_file = Path(resultdir) / f"failed_{Path(resultdir).name}.txt"
f = open(str(failed_file), "w")
failed = []
Path("logs.txt").touch()
dataset_list = [
# ordered by number of subjets
"Hamilton", # 98
"AdolescentDepressionSRQA", # 400
"AdolescentDepressionCU", # 400
"RutledgeSmartphone", # 1k
"DASS", # 6k
"HEXACO", # 12k
"16PF", # 20k
"IPIP" # 25k+
]
param_grid = ParameterGrid({
# "norm": ["l1", "l2"],
"n_cluster": ["2-9"],
"norm": ["l2"],
"cluster_method": ["kmeans"], #"spectralcosine"], # "bisectingkmeans"], # kmeans, SpectralCosine
"dimred_method": ["pca", "umap", "bvae", "dictionnarylearning"], # pca/umap/nmf/bvae/dictionnarylearning
"mode": [
"llm_random_10",
"llm_random_500",
"feat_raw",
"feat_agg",
"feat_raw_no_norm",
"feat_agg_no_norm",
# sbert models
"llm_all-mpnet-base-v2",
"llm_clip-ViT-L-14",
"llm_clip-ViT-B-32-multilingual-v1",
"llm_all-distilroberta-v1",
"llm_distiluse-base-multilingual-cased-v2",
"llm_multi-qa-distilbert-cos-v1",
"llm_paraphrase-multilingual-mpnet-base-v2",
"llm_msmarco-distilbert-cos-v5",
"llm_average_word_embeddings_glove.6B.300d",
"llm_facebook-dpr-ctx_encoder-multiset-base",
"llm_facebook-dpr-question_encoder-multiset-base",
"llm_jina-embeddings-v2-base-en",
"llm_openai",
],
"n_components": [10],
"sample_to_keep": [5_000],
"testing": [testing],
"result_dir": [resultdir],
"verbose": [verbose],
})
# for each dataset, iterate over the whole parameter grid
n = len(param_grid) * len(dataset_list)
for datasetname in tqdm(dataset_list, desc="dataset", colour="magenta"):
writer = SummaryWriter(logdir + f"/{datasetname}")
param_count = 0
for param in tqdm(param_grid, desc=datasetname, smoothing=0, colour="magenta"):
param_count += 1
tqdm.write(f"Parameters for {datasetname}: '{param}'")
try:
output = QuestEA(
datasetname=datasetname,
**param).output
if output["intrinsic_metrics_plot"] is not None:
writer.add_figure(
tag="Intrinsic metrics plot",
figure=output["intrinsic_metrics_plot"],
global_step=param_count,
)
predictions = output["predictions"]
# store df print values as text
writer.add_text(
tag=f"df_text {param['mode']} {param['norm']} {param['dimred_method']}",
text_string=output["self.df_text"],
)
writer.add_text(
tag=f"df_answ {param['mode']} {param['norm']} {param['dimred_method']}",
text_string=output["self.df_answ"],
)
# store embeddings for each subject
metadata = []
metadata_headers = ["subject_id", "mode", "norm",
"dimred_method"] + [f"pred K={k}" for k in output["n_cluster"]]
for sid in list(set([l[0] for l in predictions.index.tolist()])):
metadata.append(
[
sid,
param["mode"],
param["norm"],
param["dimred_method"],
] + [
predictions.loc[ (sid, n_cluster), "prediction"]
for n_cluster in output["n_cluster"]])
writer.add_embedding(
mat=output["subjects_embeddings"].values,
metadata=metadata,
metadata_header=metadata_headers,
tag=f"EmbeddedSubjects {param['mode']} {param['norm']} {param['dimred_method']}",
)
# and embeddings for the dataset
if output["sentence_embeddings"] is not None:
metadata = []
metadata_headers = ["question_id", "mode", "norm",
"dimred_method"]
q_ids = output["sentence_embeddings"].index.tolist()
for qid in range(len(q_ids)):
metadata.append(
[
q_ids[qid],
param["mode"],
param["norm"],
param["dimred_method"],
])
writer.add_embedding(
mat=output["sentence_embeddings"].values,
metadata=metadata,
metadata_header=metadata_headers,
tag=f"UnansweredQuestionnaire {param['mode']} {param['norm']} {param['dimred_method']}",
)
except IgnoreInGrid as err:
tqdm.write(f"Error: '{err}'")
f.write(f"{datasetname} {param} - {err}\n")
failed.append(f"{datasetname} {param} - {err}")
end = time.time() - start
print(f"Total time so far: {end:2f}s")
tqdm.write("Keep going despite error.")
except Exception as err:
tqdm.write(f"Error: '{err}'")
f.write(f"{datasetname} {param} - {err}\n")
failed.append(f"{datasetname} {param} - {err}")
end = time.time() - start
print(f"Total time so far: {end:2f}s")
writer.flush()
if debug:
pdb.post_mortem()
else:
tqdm.write("Keep going despite error.")
try:
# after doing all the grid runs to one dataset, compare each run
# of this dataset
compared = CompareResultsPairwise(
paths=output["resultdir"],
behavior="both",
testing=testing,
verbose=param["verbose"],
)
# for comp in compared:
# tag = comp["path_to_res_A"] + " vs " + comp["path_to_res_B"]
# tag = tag.replace("RES_", "")
# for it in comp["f1"].keys():
# writer.add_hparams(
# {
# "modeA": comp["modeA"],
# "normA": comp["normA"],
# "cluster_methodA": comp["cluster_methodA"],
# "dimred_methodA": comp["dimred_methodA"],
# "n_componentsA": comp["n_componentsA"],
# "modeB": comp["modeB"],
# "normB": comp["normB"],
# "cluster_methodB": comp["cluster_methodB"],
# "dimred_methodB": comp["dimred_methodB"],
# "n_componentsB": comp["n_componentsB"],
# "K": it,
# },
# {
# "f1": comp["f1"][it],
# "f1_vs_randint": comp["f1_vs_randint"][it],
# "perf_f1_raw": comp["perf_f1_raw"][it],
# "perf_f1_raw_2": comp["f1"][it] - comp["f1_vs_randint"][it],
# "randindex": comp["randindex"][it],
# "randindex_vs_randint": comp["randindex_vs_randint"][it],
# "perf_randindex_raw": comp["perf_randindex_raw"][it],
# "perf_randindex_raw_2": comp["randindex"][it] - comp["randindex_vs_randint"][it],
# },
# )
# writer.add_scalar("perf_f1_mean" + tag, comp["perf_f1_mean"])
# writer.add_scalar("perf_randindex_mean" + tag, comp["perf_randindex_mean"])
for n_cluster in output["n_cluster"]:
figs = Plotter(
paths=output["resultdir"],
open_plot=False,
behavior="both",
k=n_cluster,
)
for k, v in figs.items():
writer.add_figure(
tag=k.replace("_", " ").title(),
figure=v,
global_step=n_cluster,
)
except Exception as err:
tqdm.write(f"Error: '{err}'")
f.write(f"{datasetname} {param} - {err}\n")
failed.append(f"{datasetname} {param} - {err}")
end = time.time() - start
print(f"Total time so far: {end:2f}s")
if debug:
pdb.post_mortem()
else:
tqdm.write("Keep going despite error.")
writer.flush()
writer.close()
f.close()
if not failed:
print("No run failed.")
else:
print("Failed runs:\n" + "\n".join(failed))
end = time.time() - start
print(f"Total time: {end:2f}s")
if __name__ == "__main__":
fire.Fire(do_grid_search)