-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_optimization.py
387 lines (365 loc) · 10.2 KB
/
main_optimization.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import random
import re
from typing import Tuple
import numpy as np
import torch
from torch.utils.data import DataLoader
import optimization
import utils
import visualization
from data import TripletData
Array = np.ndarray
Tensor = torch.Tensor
os.environ["PYTHONIOENCODING"] = "UTF-8"
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
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 (3AFC) or target-matching (2AFC) triplet task",
)
aa("--triplets_dir", type=str, help="directory from where to load triplets")
aa(
"--results_dir",
type=str,
default="./results/",
help="optional specification of results directory (if not provided will resort to ./results/init_dim/optim/mixture/seed/spike/slab/pi)",
)
aa(
"--plots_dir",
type=str,
default="./plots/",
help="optional specification of directory for plots (if not provided will resort to ./plots/init_dim/optim/mixture/seed/spike/slab/pi)",
)
aa(
"--epochs",
metavar="T",
type=int,
default=2000,
help="maximum number of epochs to run VICE optimization",
)
aa(
"--burnin",
type=int,
default=500,
help="minimum number of epochs to run VICE optimization",
)
aa("--eta", type=float, default=0.001, help="learning rate to be used in optimizer")
aa(
"--init_dim",
metavar="D",
type=int,
default=100,
help="initial dimensionality of the latent space",
)
aa(
"--batch_size",
metavar="B",
type=int,
default=128,
help="number of triplets sampled during each step (i.e., mini-batch size)",
)
aa(
"--optim",
type=str,
default="adam",
choices=["adam", "adamw", "sgd"],
help="optimizer to train VICE",
)
aa(
"--mixture",
type=str,
default="gaussian",
choices=["gaussian", "laplace"],
help="whether to use a Gaussian or Laplacian mixture for the spike-and-slab prior",
)
aa(
"--mc_samples",
type=int,
default=10,
help="number of weight samples to use for MC sampling",
)
aa("--spike", type=float, default=0.25, help="sigma for spike distribution")
aa(
"--slab",
type=float,
default=1.0,
help="sigma for slab distribution (should be smaller than spike)",
)
aa(
"--pi",
type=float,
default=0.5,
help="scalar value that determines the relative weight of the spike and slab distributions respectively",
)
aa(
"--k",
type=int,
default=5,
choices=[5, 10],
help="minimum number of items that have non-zero weight for a latent dimension (according to importance scores)",
)
aa(
"--ws",
type=int,
default=500,
help="determines for how many epochs the number of latent dimensions (after pruning) is not allowed to vary",
)
aa(
"--steps",
type=int,
default=50,
help="perform validation and save model parameters every <steps> epochs",
)
aa(
"--device",
type=str,
default="cpu",
help="whether training should be performed on CPU or GPU (i.e., CUDA).",
)
aa(
"--num_threads",
type=int,
default=4,
help="number of threads used for intraop parallelism on CPU; use only if device is CPU",
)
aa(
"--num_workers",
type=int,
default=8,
help="number of workers used for loading data",
)
aa(
"--rnd_seed",
type=int,
default=42,
help="random seed for reproducibility of results",
)
aa(
"--verbose",
action="store_true",
help="whether to display print statements about model performance during training",
)
args = parser.parse_args()
return args
def create_dirs(
results_dir: str,
plots_dir: str,
init_dim: int,
optim: str,
mixture: str,
spike: float,
slab: float,
pi: float,
rnd_seed: int,
) -> Tuple[str, str, str]:
"""Create directories for results, plots, and optimization parameters."""
print("\n...Creating directories.\n")
results_dir = os.path.join(
results_dir,
f"{init_dim}d",
optim,
mixture,
str(spike),
str(slab),
str(pi),
f"seed{rnd_seed:02d}",
)
if not os.path.exists(results_dir):
os.makedirs(results_dir, exist_ok=True)
plots_dir = os.path.join(
plots_dir,
f"{init_dim}d",
optim,
mixture,
str(spike),
str(slab),
str(pi),
f"seed{rnd_seed:02d}",
)
if not os.path.exists(plots_dir):
os.makedirs(plots_dir, exist_ok=True)
model_dir = os.path.join(results_dir, "model")
return results_dir, plots_dir, model_dir
def get_nobjects(train_triplets: Tensor) -> int:
"""Get number of unique items in the data."""
n_objects = torch.max(train_triplets).item()
if torch.min(train_triplets).item() == 0:
n_objects += 1
return n_objects
def run(
results_dir: str,
plots_dir: str,
triplets_dir: str,
task: str,
epochs: int,
burnin: int,
eta: float,
batch_size: int,
init_dim: int,
optim: str,
mixture: str,
mc_samples: int,
spike: float,
slab: float,
pi: float,
k: int,
ws: int,
steps: int,
num_workers: int,
device: torch.device,
rnd_seed: int,
verbose: bool = True,
) -> None:
"""Perform VICE training."""
# load triplets into memory
train_triplets, val_triplets = utils.load_data(
device=device, triplets_dir=triplets_dir
)
N = train_triplets.shape[0]
n_objects = get_nobjects(train_triplets)
train_triplets = TripletData(
triplets=train_triplets,
n_objects=n_objects,
)
val_triplets = TripletData(
triplets=val_triplets,
n_objects=n_objects,
)
train_batches = DataLoader(
dataset=train_triplets,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
drop_last=False,
pin_memory=True,
)
val_batches = DataLoader(
dataset=val_triplets,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
drop_last=False,
pin_memory=False,
)
print(f"\nNumber of train batches: {len(train_batches)}\n")
results_dir, plots_dir, model_dir = create_dirs(
results_dir=results_dir,
plots_dir=plots_dir,
init_dim=init_dim,
optim=optim,
mixture=mixture,
spike=spike,
slab=slab,
pi=pi,
rnd_seed=rnd_seed,
)
# initialize VICE model
vice = optimization.VICE(
task=task,
n_train=N,
n_objects=n_objects,
init_dim=init_dim,
optim=optim,
eta=eta,
batch_size=batch_size,
epochs=epochs,
burnin=burnin,
mc_samples=mc_samples,
mixture=mixture,
spike=spike,
slab=slab,
pi=pi,
k=k,
ws=ws,
steps=steps,
model_dir=model_dir,
results_dir=results_dir,
device=device,
verbose=verbose,
init_weights=True,
)
# move model to current device
vice.to(device)
# start VICE training
vice.fit(train_batches=train_batches, val_batches=val_batches)
# get performance scores
train_accs = vice.train_accs
val_accs = vice.val_accs
loglikelihoods = vice.loglikelihoods
complexity_losses = vice.complexity_losses
latent_dimensions = vice.latent_dimensions
# get (detached) VICE parameters
params = vice.detached_params
visualization.plot_single_performance(
plots_dir=plots_dir, val_accs=val_accs, train_accs=train_accs, steps=steps
)
visualization.plot_complexities_and_loglikelihoods(
plots_dir=plots_dir,
loglikelihoods=loglikelihoods,
complexity_losses=complexity_losses,
)
visualization.plot_latent_dimensions(
plots_dir=plots_dir, latent_dimensions=latent_dimensions
)
# compress model params and store as binary files
with open(os.path.join(results_dir, "parameters.npz"), "wb") as f:
np.savez_compressed(f, loc=params["loc"], scale=params["scale"])
if __name__ == "__main__":
# parse arguments and set random seeds
args = parseargs()
np.random.seed(args.rnd_seed)
random.seed(args.rnd_seed)
torch.manual_seed(args.rnd_seed)
if re.search(r"cuda", args.device):
device = torch.device(args.device)
torch.cuda.manual_seed_all(args.rnd_seed)
try:
current_device = int(args.device[-1])
except ValueError:
current_device = 1
try:
torch.cuda.set_device(current_device)
except RuntimeError:
current_device = 0
torch.cuda.set_device(current_device)
device = torch.device(f"cuda:{current_device}")
print(f"\nPyTorch CUDA version: {torch.version.cuda}")
print(f"Process is running on *cuda:{current_device}*\n")
else:
os.environ["OMP_NUM_THREADS"] = str(args.num_threads)
torch.set_num_threads(args.num_threads)
device = torch.device(args.device)
run(
results_dir=args.results_dir,
plots_dir=args.plots_dir,
triplets_dir=args.triplets_dir,
task=args.task,
epochs=args.epochs,
burnin=args.burnin,
eta=args.eta,
batch_size=args.batch_size,
init_dim=args.init_dim,
optim=args.optim,
mixture=args.mixture,
mc_samples=args.mc_samples,
spike=args.spike,
slab=args.slab,
pi=args.pi,
k=args.k,
ws=args.ws,
steps=args.steps,
num_workers=args.num_workers,
device=device,
rnd_seed=args.rnd_seed,
verbose=args.verbose,
)