-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.py
379 lines (299 loc) · 11 KB
/
config.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
import copy
def get_config(dataset, model, use_baseline):
return {
"dataset": dataset,
"model": model,
**get_config_base(dataset, model, use_baseline)
}
def get_config_base(dataset, model, use_baseline):
if dataset in ["2uniforms", "8gaussians", "checkerboard", "2spirals", "rings"]:
return get_2d_config(dataset, model, use_baseline)
elif dataset in ["power", "gas", "hepmass", "miniboone"]:
return get_uci_config(dataset, model, use_baseline)
elif dataset in ["mnist", "fashion-mnist", "cifar10", "svhn"]:
return get_images_config(dataset, model, use_baseline)
else:
assert False, f"Invalid dataset {dataset}"
def get_2d_config(dataset, model, use_baseline):
if model in ["maf", "flat-realnvp"]:
config = {
"num_density_layers": 20 if use_baseline else 5,
"g_hidden_channels": [50] * 4,
"st_nets": [10] * 2,
"p_nets": [50] * 4,
"q_nets": [50] * 4,
}
elif model == "sos":
config = {
"num_density_layers": 3 if use_baseline else 2,
"num_polynomials_per_layer": 2,
"polynomial_degree": 4,
"st_nets": [40] * 2,
"p_nets": [40] * 4,
"q_nets": [40] * 4
}
elif model == "nsf":
config = {
"num_density_layers": 2,
"num_density_layers": 2,
"num_bins": 64 if use_baseline else 24,
"num_hidden_channels": 32,
"num_hidden_layers": 2,
"tail_bound": 5,
"autoregressive": False,
"dropout_probability": 0.,
"st_nets": [24] * 2,
"p_nets": [24] * 3,
"q_nets": [24] * 3
}
elif model == "bnaf":
config = {
"num_density_layers": 1,
"num_hidden_layers": 2,
"hidden_channels_factor": 50 if use_baseline else 45,
"activation": "soft-leaky-relu",
"st_nets": [24] * 2,
"p_nets": [24] * 3,
"q_nets": [24] * 3,
"max_epochs": 1000,
"max_bad_valid_epochs": 1000,
"test_batch_size": 1000
}
else:
assert False, f"Invalid model `{model}' for dataset `{dataset}'"
return {
"num_u_channels": 0 if use_baseline else 1,
"dequantize": False,
"batch_norm": False,
"max_epochs": 1000,
"max_grad_norm": None,
"early_stopping": True,
"max_bad_valid_epochs": 1000,
"train_batch_size": 1000,
"valid_batch_size": 1000,
"test_batch_size": 10000,
"opt": "adam",
"lr": 1e-2,
"lr_schedule": "none",
"weight_decay": 0.,
"epochs_per_test": 5,
"num_train_elbo_samples": 10 if not use_baseline else 1,
"num_valid_elbo_samples": 10 if not use_baseline else 1,
"num_test_elbo_samples": 100 if not use_baseline else 1,
**config,
}
def something(num_datapoints, batch_size, max_steps, increase_factor):
steps_per_epoch = num_datapoints // batch_size
max_epochs = max_steps // steps_per_epoch
return max_epochs * 10
num_datapoints = 29_556
original_batch_size = 128
original_steps_per_epoch = num_datapoints // original_batch_size
original_max_steps = 200_000
original_max_epochs = original_max_steps // original_steps_per_epoch
increase_factor = 10
def get_uci_config(dataset, model, use_baseline):
if model in ["maf", "flat-realnvp"]:
if dataset in ["gas", "power"]:
config = {
"num_u_channels": 0 if use_baseline else 2,
"num_density_layers": 10,
"g_hidden_channels": [200] * 2 if use_baseline else [100] * 2,
"st_nets": [100] * 2,
"p_nets": [200] * 2,
"q_nets": [200] * 2,
}
elif dataset in ["hepmass", "miniboone"]:
if use_baseline:
num_u_channels = 0
elif dataset == "hepmass":
num_u_channels = 5
else:
num_u_channels = 10
config = {
"num_u_channels": num_u_channels,
"num_density_layers": 10,
"g_hidden_channels": [512] * 2 if use_baseline else [128] * 2,
"st_nets": [128] * 2,
"p_nets": [512] * 2,
"q_nets": [512] * 2
}
elif model == "sos":
assert use_baseline
config = {
"num_u_channels": 0,
"num_density_layers": 8,
"g_hidden_channels": [200] * 2,
"num_polynomials_per_layer": 5,
"polynomial_degree": 4,
"lr": 1e-3,
"opt": "sgd"
}
elif model == "nsf":
if dataset in ["power", "gas"]:
config = {
"num_u_channels": 0 if use_baseline else 2,
"num_density_layers": 10 if use_baseline else 7,
"num_hidden_layers": 2,
"num_hidden_channels": 256,
"num_bins": 8,
"dropout_probability": 0. if dataset == "power" else 0.1,
"st_nets": [120] * 2,
"p_nets": [240] * 2,
"q_nets": [240] * 2,
"lr": 0.0005,
"train_batch_size": 5120
}
# We convert the presecribed number of steps into epochs
if dataset == "gas":
config["max_epochs"] = (400_000 * 512) // 852_174
elif dataset == "power":
config["max_epochs"] = (400_000 * 512) // 1_615_917
# We run for a bit longer to ensure convergence
config["max_epochs"] += 100
elif dataset == "hepmass":
config = {
"num_u_channels": 0 if use_baseline else 5,
"num_density_layers": 20 if use_baseline else 10,
"num_hidden_layers": 1,
"num_hidden_channels": 128,
"num_bins": 8,
"dropout_probability": 0.2,
"st_nets": [64] * 2,
"p_nets": [192] * 2,
"q_nets": [192] * 2,
# We increase the lr and batch size by a factor of 10 from the prescribed values
"lr": 0.0005 * 10,
"train_batch_size": 256 * 10,
# We convert the presecribed number of steps into epochs, and run for 400
# epochs extra because we don't quite converge otherwise.
"max_epochs": (400_000 * 256) // 315_123 + 400
}
elif dataset == "miniboone":
config = {
"num_u_channels": 0 if use_baseline else 10,
"num_density_layers": 10 if use_baseline else 4,
"num_hidden_layers": 1,
"num_hidden_channels": 32,
"num_bins": 4,
"dropout_probability": 0.2,
"st_nets": [32] * 2,
"p_nets": [64] * 2,
"q_nets": [64] * 2,
# We increase the lr and batch size by a factor of 10 from the prescribed values
"lr": 0.0003 * 10,
"train_batch_size": 128 * 10,
# We convert the presecribed number of steps into epochs
"max_epochs": (200_000 * 128) // 29_556
}
config = {
**config,
"tail_bound": 3,
"autoregressive": True,
"batch_norm": False,
"max_grad_norm": 5,
"lr_schedule": "cosine"
}
else:
assert False, f"Invalid model `{model}' for dataset `{dataset}''"
config = {
"dequantize": False,
"batch_norm": True,
"batch_norm_apply_affine": True,
"batch_norm_use_running_averages": False,
"early_stopping": True,
"train_batch_size": 1000,
"valid_batch_size": 5000,
"test_batch_size": 5000,
"opt": "adam",
"lr": 1e-3,
"lr_schedule": "none",
"weight_decay": 0.,
"max_bad_valid_epochs": 5000,
"max_epochs": 5000,
"max_grad_norm": None,
"epochs_per_test": 5,
"num_train_elbo_samples": 1 if not use_baseline else 1,
"num_valid_elbo_samples": 5 if not use_baseline else 1,
"num_test_elbo_samples": 10 if not use_baseline else 1,
**config
}
return config
def get_images_config(dataset, model, use_baseline):
if model == "multiscale-realnvp":
if use_baseline:
config = {
"g_hidden_channels": [64] * 8,
"num_u_channels": 0
}
else:
config = {
"g_hidden_channels": [64] * 4,
"num_u_channels": 1,
"st_nets": [8] * 2,
"p_nets": [64] * 2,
"q_nets": [64] * 2
}
config["early_stopping"] = True
config["train_batch_size"] = 100
config["valid_batch_size"] = 500
config["test_batch_size"] = 500
config["opt"] = "adam"
config["lr"] = 1e-4
config["weight_decay"] = 0.
if dataset in ["cifar10", "svhn"]:
config["logit_tf_lambda"] = 0.05
config["logit_tf_scale"] = 256
elif dataset in ["mnist", "fashion-mnist"]:
config["logit_tf_lambda"] = 1e-6
config["logit_tf_scale"] = 256
elif model == "glow":
if use_baseline:
config = {
"num_scales": 3,
"num_steps_per_scale": 32,
"g_num_hidden_channels": 512,
"num_u_channels": 0,
"valid_batch_size": 500,
"test_batch_size": 500
}
else:
config = {
"num_scales": 2,
"num_steps_per_scale": 32,
"g_num_hidden_channels": 256,
"num_u_channels": 1,
"st_nets": 64,
"p_nets": 128,
"q_nets": 128,
"valid_batch_size": 100,
"test_batch_size": 100
}
config["early_stopping"] = False
config["train_batch_size"] = 64
config["opt"] = "adamax"
config["lr"] = 5e-4
if dataset in ["cifar10"]:
config["weight_decay"] = 0.1
else:
config["weight_decay"] = 0.
config["centering_tf_scale"] = 256
else:
assert False, f"Invalid model {model} for dataset {dataset}"
config = {
**config,
"dequantize": True,
"batch_norm": True,
"batch_norm_apply_affine": use_baseline,
"batch_norm_use_running_averages": True,
"batch_norm_momentum": 0.1,
"lr_schedule": "none",
"max_bad_valid_epochs": 50,
"max_grad_norm": None,
"max_epochs": 1000,
"epochs_per_test": 1,
"num_train_elbo_samples": 1 if not use_baseline else 1,
"num_valid_elbo_samples": 5 if not use_baseline else 1,
"num_test_elbo_samples": 10 if not use_baseline else 1
}
return config