Repository navigation
Expand file tree
/
Copy pathmain_inference.py
More file actions
345 lines (300 loc) · 10.3 KB
/
Copy pathmain_inference.py
File metadata and controls
345 lines (300 loc) · 10.3 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import random
import re
from collections import defaultdict
from typing import List
import numpy as np
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import optimization
import utils
from data import TripletData
os.environ["PYTHONIOENCODING"] = "UTF-8"
os.environ["OMP_NUM_THREADS"] = "1"
Array = np.ndarray
def parseargs():
parser = argparse.ArgumentParser()
def aa(*args, **kwargs):
parser.add_argument(*args, **kwargs)
aa(
"--task",
type=str,
default="odd-one-out",
choices=["odd-one-out", "target-matching"],
help="whether to perform an odd-one-out (no anchor) or pair-matching (anchor) triplet task",
)
aa(
"--n_objects",
type=int,
default=1854,
help="number of unique items/objects/stimuli in dataset",
)
aa(
"--init_dim",
type=int,
default=100,
help="initial dimensionality of the VICE latent space",
)
aa(
"--batch_size",
metavar="B",
type=int,
default=128,
help="number of triplets in each mini-batch",
)
aa(
"--mixture",
type=str,
default="gaussian",
choices=["gaussian", "laplace"],
help="whether to use a mixture of Gaussians or Laplacians for the spike-and-slab prior",
)
aa(
"--mc_samples",
type=int,
default=25,
choices=[5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
help="number of weight samples to use for MC sampling at inference time",
)
aa("--results_dir", type=str, help="results directory (root directory for models)")
aa(
"--triplets_dir",
type=str,
help="directory from where to load validation and held-out test set",
)
aa("--device", type=str, default="cpu", choices=["cpu", "cuda"])
aa("--rnd_seed", type=int, default=42, help="random seed for reproducibility")
args = parser.parse_args()
return args
def get_model_paths(PATH: str) -> List[str]:
regex = r"(?=^model)(?=.*epoch)(?=.*tar$)"
model_paths = []
for root, _, files in os.walk(PATH):
files = sorted(list(filter(lambda f: re.search(regex, f), files)))
if files:
model_paths.append("/".join(root.split("/")[:-1]))
return model_paths
def entropy(p: Array) -> Array:
return np.sum(np.where(p == 0, 0, p * np.log(p)))
def cross_entropy(p: Array, q: Array) -> Array:
return -np.sum(p * np.log(q))
def kld(p: Array, q: Array) -> Array:
return entropy(p) + cross_entropy(p, q)
def compute_divergences(human_pmfs: dict, model_pmfs: dict, metric: str) -> Array:
assert len(human_pmfs) == len(
model_pmfs
), "\nNumber of triplets in human and model distributions must correspond.\n"
divergences = np.zeros(len(model_pmfs))
for i, (triplet, p) in enumerate(human_pmfs.items()):
q = np.asarray(model_pmfs[triplet])
if metric == "kld":
div = kld(p, q)
else:
div = cross_entropy(p, q)
divergences[i] += div
return divergences
def prune_weights(model: optimization.VICE, indices: Array) -> optimization.VICE:
for p in model.parameters():
p.data = p.data[torch.from_numpy(indices)]
return model
def pruning(
model: optimization.VICE, k: int = 5, alpha: float = 0.05
) -> optimization.VICE:
params = model.detached_params
loc = params["loc"]
scale = params["scale"]
p_vals = utils.compute_pvals(loc, scale)
rejections = utils.fdr_corrections(p_vals, alpha)
importance = utils.get_importance(rejections).ravel()
signal = np.where(importance > k)[0]
pruned_model = prune_weights(model, signal)
return pruned_model
def get_models(
vice_paths: List[str],
task: str,
mixture: str,
n_objects: int,
init_dim: int,
batch_size: int,
mc_samples: int,
results_dir: str,
device: torch.device,
k: int = 5,
) -> list:
vice_instances = []
for vice_path in vice_paths:
seed = vice_path.split("/")[-1]
vice = getattr(optimization, "VICE")(
task=task,
n_train=None,
n_objects=n_objects,
init_dim=init_dim,
optim=None,
eta=None,
batch_size=batch_size,
burnin=None,
ws=None,
k=k,
epochs=None,
mc_samples=mc_samples,
mixture=mixture,
spike=1.0,
slab=1.0,
pi=1.0,
steps=None,
model_dir=None,
results_dir=results_dir,
device=device,
init_weights=True,
)
try:
vice = utils.load_model(model=vice, PATH=vice_path, device=device)
vice_instances.append((seed, vice))
except RuntimeError:
raise Exception(
f"\nVICE parameters were incorrectly stored for: {vice_path}\n"
)
return vice_instances
def inference(
task: str,
n_objects: int,
init_dim: int,
batch_size: int,
mixture: str,
mc_samples: int,
results_dir: str,
triplets_dir: str,
device: torch.device,
) -> None:
in_path = os.path.join(results_dir, f"{init_dim}d")
vice_paths = get_model_paths(in_path)
seeds, vice_models = zip(
*get_models(
vice_paths,
task,
mixture,
n_objects,
init_dim,
batch_size,
mc_samples,
results_dir,
device,
)
)
test_triplets = utils.load_data(
device=device, triplets_dir=triplets_dir, inference=True
)
_, val_triplets = utils.load_data(
device=device, triplets_dir=triplets_dir, inference=False
)
val_triplets = TripletData(
triplets=val_triplets,
n_objects=n_objects,
)
test_triplets = TripletData(
triplets=test_triplets,
n_objects=n_objects,
)
val_batches = DataLoader(
dataset=val_triplets,
batch_size=batch_size,
shuffle=False,
num_workers=0,
drop_last=False,
pin_memory=False,
)
test_batches = DataLoader(
dataset=test_triplets,
batch_size=batch_size,
shuffle=False,
num_workers=0,
drop_last=False,
pin_memory=False,
)
print(f"\nNumber of validation batches in current process: {len(val_batches)}\n")
print(f"Number of test batches in current process: {len(test_batches)}\n")
val_losses = {}
test_accs = {}
test_losses = {}
vice_choices = {}
vice_pmfs_all = defaultdict(dict)
for seed, vice, vice_path in tqdm(
zip(seeds, vice_models, vice_paths), desc="Models"
):
pruned_vice = pruning(vice)
val_loss, _ = pruned_vice.evaluate(val_batches)
test_acc, test_loss, probas, vice_pmfs, triplet_choices = pruned_vice.inference(
test_batches
)
val_losses[seed] = val_loss
test_accs[seed] = test_acc
test_losses[seed] = test_loss
vice_pmfs_all[seed] = vice_pmfs
vice_choices[seed] = triplet_choices
print(f"\nTest accuracy for current random seed: {test_acc}")
print(f"Test loss for current random seed: {test_loss}\n")
with open(os.path.join(vice_path, "test_probas.npy"), "wb") as f:
np.save(f, probas)
seeds, losses = zip(*sorted(val_losses.items(), key=lambda kv: kv[1], reverse=False))
print(f"\nRandom seeds: {seeds}")
print(f"Validation losses: {losses}\n")
median_model = seeds[int(len(seeds) // 2) - 1]
test_accs_ = list(test_accs.values())
avg_test_acc = np.mean(test_accs_)
median_test_acc = np.median(test_accs_)
max_test_acc = np.max(test_accs_)
print(f"\nMean accuracy on held-out test set: {avg_test_acc}")
print(f"Median accuracy on held-out test set: {median_test_acc}")
print(f"Max accuracy on held-out test set: {max_test_acc}\n")
out_path = os.path.join(in_path, "evaluation_metrics")
if not os.path.exists(out_path):
os.makedirs(out_path, exist_ok=True)
# sort accuracies in ascending order and cross-entropy errors in descending order
test_accs = dict(sorted(test_accs.items(), key=lambda kv: kv[1], reverse=False))
test_losses = dict(sorted(test_losses.items(), key=lambda kv: kv[1], reverse=True))
print(f"\nTest accuracies: {test_accs})")
print(f"Test losses: {test_losses}\n")
# NOTE: we leverage the model that is slightly better than the median model (since we have 20 random seeds, the median is the average between model 10 and 11)
# median_model = list(test_losses.keys())[int(len(test_losses)//2)-1]
median_model_pmfs = vice_pmfs_all[median_model]
median_model_choices = vice_choices[median_model]
utils.pickle_file(median_model_choices, out_path, "triplet_choices")
utils.pickle_file(median_model_pmfs, out_path, "model_choice_pmfs")
utils.pickle_file(test_accs, out_path, "test_accuracies")
utils.pickle_file(test_losses, out_path, "test_losses")
human_pmfs = utils.unpickle_file(triplets_dir, "human_choice_pmfs")
klds = compute_divergences(human_pmfs, median_model_pmfs, metric="kld")
cross_entropies = compute_divergences(
human_pmfs, median_model_pmfs, metric="cross-entropy"
)
np.savetxt(os.path.join(out_path, "klds_median.txt"), klds)
np.savetxt(os.path.join(out_path, "cross_entropies_median.txt"), cross_entropies)
klds, cross_entropies = {}, {}
for seed, vice_pmfs in vice_pmfs_all.items():
klds[seed] = np.mean(compute_divergences(human_pmfs, vice_pmfs, metric="kld"))
cross_entropies[seed] = np.mean(
compute_divergences(human_pmfs, vice_pmfs, metric="cross-entropy")
)
utils.pickle_file(klds, out_path, "klds_all")
utils.pickle_file(cross_entropies, out_path, "cross_entropies_all")
if __name__ == "__main__":
# parse arguments and set random seed
args = parseargs()
random.seed(args.rnd_seed)
np.random.seed(args.rnd_seed)
device = torch.device(args.device)
inference(
task=args.task,
n_objects=args.n_objects,
init_dim=args.init_dim,
batch_size=args.batch_size,
mixture=args.mixture,
mc_samples=args.mc_samples,
results_dir=args.results_dir,
triplets_dir=args.triplets_dir,
device=device,
)