forked from neuroailab/Neural-Alignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.py
659 lines (546 loc) · 17.4 KB
/
flags.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
#### Cloud TPU Cluster Resolvers
flags.DEFINE_string(
'gcp_project',
default=None,
help='Project name for the Cloud TPU-enabled project. If not specified, we '
'will attempt to automatically detect the GCE project from metadata.')
flags.DEFINE_string(
'tpu_zone',
default=None,
help='GCE zone where the Cloud TPU is located in. If not specified, we '
'will attempt to automatically detect the GCE project from metadata.')
flags.DEFINE_string(
'tpu_name',
default=None,
help='Name of the Cloud TPU for Cluster Resolvers. You must specify either'
' this flag or --master.')
flags.DEFINE_integer(
'num_shards',
default=8,
help='Number of shards (TPU cores).')
flags.DEFINE_integer(
'iterations_per_loop',
default=100,
help='Number of interior TPU cycles to run before returning to the host. '
'This is different from the number of steps run before each eval '
'and should primarily be used only if you need more incremental '
'logging during training. Setting this to -1 will set the '
'iterations_per_loop to be as large as possible (i.e. perform every '
'call to train in a single TPU loop.')
#### Basic train config params
flags.DEFINE_string(
'data_dir',
default=None,
help='The directory where the ImageNet input data is stored.')
flags.DEFINE_string(
'cache_dir',
default=None,
help='Where the local tfutils cache will be.')
flags.DEFINE_integer(
'checkpoint_max',
default=5,
help='Number of checkpoints to keep in gcloud cache dir (for TPU only).')
flags.DEFINE_string(
'gpu',
default='0',
help='Comma separated list of which gpu indices to use.'
'For instance, a possible value for this flag is "0,1,5,6" ')
flags.DEFINE_integer(
'port',
default=29029,
help='Port where the MongoDB is accesible at.')
flags.DEFINE_string(
'dbname',
default='neur-al',
help='DBName to be used. Might want to be overridden to keep experiments'
' separate')
flags.DEFINE_string(
'exp_id_suffix',
default='',
help='suffix for experiment ID for position saving and tfutils logging')
flags.DEFINE_bool(
'skip_check',
default=True,
help='Whether to skip tfutils version check.')
flags.DEFINE_bool(
'validate_first',
default=True,
help='Whether to validate at first step (turn off when debugging).')
flags.DEFINE_bool(
'do_restore',
default=False,
help='Whether to restore the latest checkpoint from the DB and resume '
'training')
flags.DEFINE_integer(
'load_step',
default=None,
help='The step to load from. If None, loads the latest checkpoint.')
flags.DEFINE_bool(
'load_db',
default=False,
help='Whether to restore from a different database to continue training.')
flags.DEFINE_integer(
'load_port',
default=None,
help='Port number to load from.')
flags.DEFINE_string(
'load_dbname',
default=None,
help='DBName to be loaded from.')
flags.DEFINE_string(
'load_collname',
default=None,
help='Collname to be loaded from.')
flags.DEFINE_string(
'load_exp_id',
default=None,
help='Exp id to be loaded from.')
flags.DEFINE_string(
'load_checkpoint',
default=None,
help='Checkpoint to load from')
flags.DEFINE_string(
'val_exp_id',
default=None,
help='Exp id to be saved to for validation.')
flags.DEFINE_float(
'train_epochs',
default=90, # Roughly 450412 steps with a batch size of 256
help='The number of steps to use for training.')
flags.DEFINE_integer(
'train_batch_size',
default=256,
help='Batch size for training.')
flags.DEFINE_integer(
'minibatch_size',
default=None,
help='Batch size for training if you want to minibatch.')
flags.DEFINE_integer(
'eval_batch_size',
default=1024,
help='Batch size for evaluation.')
flags.DEFINE_float(
'epochs_per_checkpoint',
default=2,
help='Controls how often (in epochs) checkpoints are generated.')
flags.DEFINE_string(
'load_params_file',
default=None,
help='Path to a pickle to load the tfutils params dictionary from.'
'If not set, params will be parsed from the flags as usual. ')
flags.DEFINE_string(
'save_params_file',
default=None,
help='Path to a pickle to save the tfutils params dictionary to. '
'If not set, will not save the params. ')
flags.DEFINE_string(
'save_to_gfs',
default=None,
help='Comma separated list of keys to save to gfs')
#### Optimizer flags
flags.DEFINE_string(
'optimizer',
default='gd',
help='The optimizer to use for training. Must be one of:\n'
'* gd\n'
'* momentum\n'
'* adagrad\n'
'* rmsprop\n'
'* adam\n'
'* radam\n'
'* swats\n'
'* swrats (SWATS with RAdam)\n')
flags.DEFINE_string(
'alignment_optimizer',
default=None,
help='A separate optimizer to use for training the alignment loss. '
'If None, uses optimizer above. \n'
'Must be one of:\n'
'* gd\n'
'* momentum\n'
'* adagrad\n'
'* rmsprop\n'
'* adam\n'
'* radam\n'
'* swats\n'
'* swrats (SWATS with RAdam)\n')
flags.DEFINE_bool(
'use_noisy_global_opt',
default=False,
help='Whether to wrap the global optimizer in the noisy optimizer')
flags.DEFINE_bool(
'use_noisy_alignment_opt',
default=False,
help='Whether to wrap the alignment optimizer in the noisy optimizer')
flags.DEFINE_string(
'noisy_global_opt_distribution',
default=None,
help='Name of the distribution for noisy global optimizer to use. '
'Must be None (use layer input) or one of:\n'
'* uniform\n'
'* normal\n')
flags.DEFINE_float(
'noisy_global_opt_variance',
default=1.0,
help='Variance of noisy global optimizer distribution if one is given\n')
flags.DEFINE_string(
'noisy_alignment_opt_distribution',
default=None,
help='Name of the distribution for noisy alignment optimizer to use. '
'Must be None (use layer input) or one of:\n'
'* uniform\n'
'* normal\n')
flags.DEFINE_float(
'noisy_alignment_opt_variance',
default=1.0,
help='Variance of noisy alignment optimizer distribution if one is given\n')
flags.DEFINE_string(
'noisy_opt_distribution',
default=None,
help='Name of the distribution for all noisy optimizers to use. '
'Must be None (use layer input) or one of:\n'
'* uniform\n'
'* normal\n')
flags.DEFINE_float(
'noisy_opt_variance',
default=1.0,
help='Variance of all noisy optimizers distribution if one is given\n')
flags.DEFINE_bool(
'grad_clip',
default=False,
help='Whether to clip gradients.')
flags.DEFINE_float(
'grad_clipping_value',
default=1.0,
help='Gradient clipping value')
flags.DEFINE_string(
'grad_clipping_method', default='norm',
help='How to clip. [value | norm]')
#### Learning rate flags
flags.DEFINE_float(
'learning_rate',
default=0.001,
help='base learning assuming a batch size of 256.'
'For other batch sizes it is scaled linearly with batch size.')
flags.DEFINE_float(
'alignment_learning_rate',
default=0.001,
help='If using separate alignment optimizer, base alignment loss learning'
' assuming a batch size of 256.'
'For other batch sizes it is scaled linearly with batch size.')
flags.DEFINE_bool(
'rescale_lr',
default=False,
help='whether to rescale the LR')
flags.DEFINE_bool(
'alignment_rescale_lr',
default=False,
help='whether to rescale the alignment loss')
flags.DEFINE_bool(
'constant_lr',
default=True,
help='whether to have a constant learning rate')
flags.DEFINE_bool(
'alignment_constant_lr',
default=True,
help='whether to have a constant learning rate for the alignment loss')
flags.DEFINE_integer(
'base_batch_size',
default=256,
help='base batch size to rescale learning rate by.'
'For other batch sizes it is scaled linearly with batch size.')
flags.DEFINE_bool(
'manual_lr',
default=False,
help='whether to manually drop the LR')
flags.DEFINE_bool(
'alignment_manual_lr',
default=False,
help='whether to manually drop the LR for the alignment loss')
flags.DEFINE_float(
'warmup_epochs',
default=5,
help='Number of epochs to do learning rate warmup '
'(defaults to 5 if not specified).')
flags.DEFINE_float(
'alignment_warmup_epochs',
default=5,
help='Number of epochs to do learning rate warmup for alignment loss '
'(defaults to 5 if not specified).')
flags.DEFINE_integer(
'drop',
default=0,
help='The current learning rate drop index')
flags.DEFINE_integer(
'alignment_drop',
default=None,
help='The current learning rate drop index for the alignment loss. '
'If None, it is synced with the categorization loss drop flag.')
flags.DEFINE_integer(
'boundary_step',
default=None,
help='The step from which the drop in the larning rate should start')
flags.DEFINE_integer(
'alignment_boundary_step',
default=None,
help='The step from which the drop in the larning rate should start for '
'the alignment loss')
##### Dataset flags
flags.DEFINE_string(
'dataset',
default='mnist',
help='Name of the dataset to use for training. Must be one of:\n'
'* mnist\n'
'* imagenet\n')
flags.DEFINE_integer(
'image_size',
default=None,
help='Image size for ImageNet. '
'Default is None, which means 224x224 sized images')
#### Model flags
flags.DEFINE_string(
'model',
default='fc',
help='Name of the model architecture to use. Must be one of:\n'
'* fc \n'
'* resnet18 \n')
flags.DEFINE_bool(
'use_resnet_v2',
default=False,
help='Whether to use resnet v2 models. (Default is to use v1)')
flags.DEFINE_bool(
'tf_layers',
default=False,
help='Whether to override our custom layers with tf_layers.'
'For debugging purposes only. Should be equivalent to alignment=None')
flags.DEFINE_string(
'layers_list',
default=None,
help='Comma separated list of number of units in hidden layers')
flags.DEFINE_string(
'activation',
default='sigmoid',
help='Activation used for hidden layers in model on of:\n'
'* sigmoid\n'
'* tanh\n'
'* relu\n')
flags.DEFINE_bool(
'regularize_weights_via_model',
default=True,
help='Whether to add weight decay on the model function')
#### Alignment flags
flags.DEFINE_string(
'alignment',
default=None,
help='Name of the alignment to use. Must be None (Backprop) or one of:\n'
'* feedback\n'
'* symmetric\n'
'* activation\n'
'* mirror\n'
'* information\n'
'* kolen_pollack\n')
flags.DEFINE_bool(
'save_alignment_coefficients',
default=False,
help='Whether to save the alpha, beta and gamma coefficients during '
'train-time validations (only on GPU)')
flags.DEFINE_float(
'alpha',
default=None,
help='Alpha weight of the alignment')
flags.DEFINE_float(
'alpha_start',
default=0.0,
help='Fraction (between 0 and 1) of training to start non-zero alpha weight')
flags.DEFINE_float(
'alpha_stop',
default=1.0,
help='Fraction (between 0 and 1) of training to end non-zero alpha weight')
flags.DEFINE_float(
'alpha_cycle',
default=None,
help='Number of epochs for triangular cycle period')
flags.DEFINE_float(
'alpha_schedule_rate',
default=0.0,
help='Rate for growth or decay of alpha weight in non-zero regime')
flags.DEFINE_string(
'alpha_schedule_type',
default=None,
help='Type of schedule for alpha weight. One of \n'
'* linear\n'
'* exponential\n'
'* cyclic\n')
flags.DEFINE_float(
'beta',
default=None,
help='Beta weight of the alignment')
flags.DEFINE_float(
'beta_start',
default=0.0,
help='Fraction (between 0 and 1) of training to start non-zero beta weight')
flags.DEFINE_float(
'beta_stop',
default=1.0,
help='Fraction (between 0 and 1) of training to end non-zero beta weight')
flags.DEFINE_float(
'beta_cycle',
default=None,
help='Number of epochs for triangular cycle period')
flags.DEFINE_float(
'beta_schedule_rate',
default=0.0,
help='Rate for growth or decay of beta weight in non-zero regime')
flags.DEFINE_string(
'beta_schedule_type',
default=None,
help='Type of schedule for beta weight. One of \n'
'* linear\n'
'* exponential\n'
'* cyclic\n')
flags.DEFINE_float(
'gamma',
default=None,
help='Gamma weight of the alignment (so far only used for Information '
'Alignment)')
flags.DEFINE_float(
'gamma_start',
default=0.0,
help='Fraction (between 0 and 1) of training to start non-zero gamma weight')
flags.DEFINE_float(
'gamma_stop',
default=1.0,
help='Fraction (between 0 and 1) of training to end non-zero gamma weight')
flags.DEFINE_float(
'gamma_cycle',
default=None,
help='Number of epochs for triangular cycle period')
flags.DEFINE_float(
'gamma_schedule_rate',
default=0.0,
help='Rate for growth or decay of gamma weight in non-zero regime')
flags.DEFINE_string(
'gamma_schedule_type',
default=None,
help='Type of schedule for gamma weight. One of \n'
'* linear\n'
'* exponential\n'
'* cyclic\n')
flags.DEFINE_bool(
'reconstruction_reversal',
default=False,
help='Whether to use the backwards reconstruction instead of forward'
' reconstruction for the "null" primitive')
flags.DEFINE_bool(
'reconstruction_amp',
default=False,
help='Whether to use input and reconstruction in "amp" primitive'
' instead of forward and backward projection')
flags.DEFINE_bool(
'use_sparse',
default=False,
help='Whether to use the sparse primitive instead of decay'
' primnitive for information alignment')
flags.DEFINE_bool(
'update_forward',
default=False,
help='Boolean defining whether alignment loss will modify the forward weights')
flags.DEFINE_string(
'input_distribution',
default=None,
help='Name of the input distribution to use. '
'Must be None (use layer input) or one of:\n'
'* uniform\n'
'* normal\n')
flags.DEFINE_float(
'input_stddev',
default=1.0,
help='If using a non-None input distribution, then uses this value as the '
'standard deviation. (Defaults to 1.0)')
flags.DEFINE_bool(
'use_bias_forward',
default=True,
help='Boolean defining whether to use biases in forward layers')
flags.DEFINE_bool(
'use_bias_backward',
default=True,
help='Boolean defining whether to use biases in backward layers')
flags.DEFINE_string(
'activation_fn_override',
default=None,
help='Activation to override default in alignment regularization computations:\n'
'* sigmoid\n'
'* tanh\n'
'* relu\n')
flags.DEFINE_bool(
'activation_forward',
default=False,
help='Boolean defining whether alignment loss should be calculated using '
'the activations')
flags.DEFINE_bool(
'activation_backward',
default=False,
help='Boolean defining whether alignment loss should be calculated using '
'the activations')
flags.DEFINE_bool(
'bn_trainable',
default=True,
help='Boolean defining whether batch normalization variables should be '
'trainable')
flags.DEFINE_bool(
'batch_center_backward_input',
default=False,
help='Boolean defining whether to batch center the backward inputs to layers')
flags.DEFINE_bool(
'center_input',
default=False,
help='Boolean defining whether to center the forward inputs to layers')
flags.DEFINE_bool(
'normalize_input',
default=False,
help='Boolean defining whether to normalize the forward inputs to layers')
flags.DEFINE_bool(
'batch_center_forward_output',
default=False,
help='Boolean defining whether to batch center the forward outputs of layers')
flags.DEFINE_bool(
'center_forward_output',
default=False,
help='Boolean defining whether to center the forward outputs of layers')
flags.DEFINE_bool(
'normalize_forward_output',
default=False,
help='Boolean defining whether to normalize the forward outputs of layers')
flags.DEFINE_bool(
'center_backward_output',
default=False,
help='Boolean defining whether to center the backwards outputs of layers')
flags.DEFINE_bool(
'normalize_backward_output',
default=False,
help='Boolean defining whether to normalize the backwards outputs of layers')
flags.DEFINE_float(
'loss_rate',
default=1.0,
help='Loss weighting (categorization + reg)')
flags.DEFINE_float(
'alignment_rate',
default=1.0,
help='Alignment loss weighting')
flags.DEFINE_float(
'delay_epochs',
default=None,
help='Float defining number of epochs to delay applying loss update and '
'only apply alignment update')
flags.DEFINE_integer(
'alternate_step_freq',
default=None,
help='Integer defining number of steps (must be at least 1) to apply loss '
'update per alignment update')
flags.DEFINE_bool(
'constant_rate',
default=True,
help='Boolean defining whether to have a constant rate or apply a schedule')