-
Notifications
You must be signed in to change notification settings - Fork 11
/
spamGAN_config_minimal.py
567 lines (498 loc) · 14.6 KB
/
spamGAN_config_minimal.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
import tensorflow
# Overarching
gen_test = False # Whether training or testing the generator perplexity
clas_test = False # Whether training or testing the classifier test performance
clas_test_ckpt = None # Which checkpoint to use for classifier testing
clas_pred_output = None # Where to save classifier test predictions
# Saving/logging Config
restore_model= False # Whether to reinitialize or restore weights
clear_run_logs = False # Whether to delete prior run logs
log_dir= '/tmp/' # Where to store logs
checkpoint_dir= '/tmp' # Where to store ckpt files
load_checkpoint_file = None # Which checkpoint to load
# Logging frequency/verbosity
log_verbose_mle = True
log_verbose_rl = True
batches_per_summary = 100
batches_per_text_summary = 100
# Number of epochs to run
g_unlab_every_n = 20 # Balance of exposure to labeled/unlabeled datasets
g_pretrain_epochs = 1# 60
d_pretrain_epochs = 1# 60
d_pretrain_critic_epochs = 0#20
c_pretrain_epochs =1 # 20
adversarial_epochs = 1 # How many total adversarial epochs, for all components
# During adversarial training, how many epochs to run for discriminator/classifier
disc_adv = 1
clas_adv = 10
gen_adv_epoch = 4 # Number of generator adversarial epochs
g_unlab_every_n_adv = -1 # Frequency of generator ML epochs with unlabeled data
gen_mle_adv_epoch = 2 # Number of generator ML epochs with labeled data
adv_train_max_gen_examples = 1000 # Maximum size of training epoch for gen in adv
adv_disc_max_ex = 5000 # Maximum size of training epoch for disc in adv
adv_gen_train_with_unsup = False # Whether or not to use unlabeled examples in adv
# Early stopping parameters
gen_patience=20
gen_es_tolerance = 0.005
clas_es_tolerance = 0.005
clas_patience = 10
# Controls ML/generation max sentence length (in words)
max_decoding_length = 128
max_decoding_length_infer = 128
annealing_length = 128 # Can use shorter sentences for initial training
adversarial_length = 128 # Can use shorter sentences for adversarial training
use_unsup=False # Whether unsupervised data is used at all for this model
sampling_temperature = 1.0 # Sampling temperature in generation
linear_decay_pg_weights = True # Place more importance on initial sentence rewards
# Context configs
prior_prob=0.5 # probability of class 1 in generated/unlabeled data.
noise_size=10 # dim of noise vector
# Training tweaks
disc_label_smoothing_epsilon = 0.05 # label smoothing for discriminator
# Set to experiement with clipping policy gradients at various points
adv_max_clip = 100
min_log_prob = 0.1
max_log_prob = 100
min_pg_loss = -200
max_pg_loss = 200
add_sentence_progress = True # Includes indicator of sentence length (decays 1 to 0)
# avoids mode collapse to extremely short sentences
clas_loss_on_fake_lambda = 1.0 # Balancing param on real/generated clas
disc_crit_train_on_fake_only = True # Only train disc crit on generated sentences
clas_crit_train_on_fake_only = True # Only train disc crit on generated sentences
reward_blending = 'f1' # Additive vs f1 clas-disc reward blending
clas_min_ent_lambda = 1.0 # Controls strength of entropy minimization
clas_has_own_embedder = True # Pool or share embedders
disc_has_own_embedder = True
# Different loss functions
mle_loss_in_adv = True # Whether or not to include ML optimization in adversarial
# Relative weighting of discriminator and classifier in pg loss
discriminator_loss_lambda = 1.0
classifier_loss_lambda = 1.0
norm_advantages = True # Normalize advantages
let_discriminator_train_embedder = True # whether discriminator can update embedder
train_data = {
"num_epochs": 1,
"batch_size": 128,
"allow_smaller_final_batch": True,
"shuffle": True,
"shuffle_buffer_size": None,
"shard_and_shuffle": False,
"num_parallel_calls": 1,
"prefetch_buffer_size": 0,
"max_dataset_size": -1,
"seed": None,
"name": "train_data",
'datasets' : [
{
"files" : ['./minrun_train_reviews.txt'],
'vocab_file' : './minrun_opspam_vocab.txt',
'max_seq_length' : 128,
'length_filter_mode' : 'truncate',
'bos_token' : '<BOS>',
'delimiter' : ' ',
'eos_token' : '<EOS>',
'data_name' : 'x',
'pad_to_max_seq_length' : True
},
{
'files' : './minrun_train_labels.txt',
'data_type' : 'int',
'data_name' : 'label'
}
]
}
clas_train_data = {
"num_epochs": 1,
"batch_size": 128,
"allow_smaller_final_batch": True,
"shuffle": True,
"shuffle_buffer_size": None,
"shard_and_shuffle": False,
"num_parallel_calls": 1,
"prefetch_buffer_size": 0,
"max_dataset_size": -1,
"seed": None,
"name": "train_data",
'datasets' : [
{
"files" : ['./minrun_train_reviews.txt'],
'vocab_file' : './minrun_opspam_vocab.txt',
'max_seq_length' : 128,
'length_filter_mode' : 'truncate',
'bos_token' : '<BOS>',
'delimiter' : ' ',
'eos_token' : '<EOS>',
'data_name' : 'x',
'pad_to_max_seq_length' : True
},
{
'files' : ['./minrun_train_labels.txt'],
'data_type' : 'int',
'data_name' : 'label'
}
]
}
val_data = {
"num_epochs": 1,
"batch_size": 50,
"allow_smaller_final_batch": True,
"shuffle": True,
"shuffle_buffer_size": None,
"shard_and_shuffle": False,
"num_parallel_calls": 1,
"prefetch_buffer_size": 0,
"max_dataset_size": -1,
"seed": None,
"name": "val_data",
'datasets' : [
{
"files" : ['./minrun_val_reviews.txt'],
'vocab_file' : './minrun_opspam_vocab.txt',
'max_seq_length' : 128,
'length_filter_mode' : 'truncate',
'bos_token' : '<BOS>',
'delimiter' : ' ',
'eos_token' : '<EOS>',
'data_name' : 'x',
'pad_to_max_seq_length' : True
},
{
'files' : ['./minrun_val_labels.txt'],
'data_type' : 'int',
'data_name' : 'label'
}
]
}
test_data = {
"num_epochs": 1,
"batch_size": 64,
"allow_smaller_final_batch": True,
"shuffle": False,
"shuffle_buffer_size": None,
"shard_and_shuffle": False,
"num_parallel_calls": 1,
"prefetch_buffer_size": 0,
"max_dataset_size": -1,
"seed": None,
"name": "test_data",
'datasets' : [
{
"files" : 'minrun_test_reviews.txt',
'vocab_file' : 'minrun_opspam_vocab.txt',
'max_seq_length' : 128,
'length_filter_mode' : 'truncate',
'bos_token' : '<BOS>',
'delimiter' : ' ',
'eos_token' : '<EOS>',
'data_name' : 'x',
'pad_to_max_seq_length' : True
},
{
'files' : 'minrun_test_labels.txt',
'data_type' : 'int',
'data_name' : 'label'
}
]
}
# EMBEDDER HPARAMS
emb_hparams = {
"dim": 50,
"dropout_rate": 0.2,
"dropout_strategy": 'element',
"trainable": True,
"initializer": {
"type": "random_uniform_initializer",
"kwargs": {
"minval": -0.1,
"maxval": 0.1,
"seed": None
}
},
"regularizer": {
"type": "L1L2",
"kwargs": {
"l1": 0.,
"l2": 0
}
},
"name": "gen_embedder",
}
disc_emb_hparams = {
"dim": 50,
"dropout_rate": 0.4,
"dropout_strategy": 'element',
"trainable": True,
"initializer": {
"type": "random_uniform_initializer",
"kwargs": {
"minval": -0.1,
"maxval": 0.1,
"seed": None
}
},
"regularizer": {
"type": "L1L2",
"kwargs": {
"l1": 0.,
"l2": 0
}
},
"name": "disc_embedder",
}
clas_emb_hparams = {
"dim": 50,
"dropout_rate": 0.4,
"dropout_strategy": 'element',
"trainable": True,
"initializer": {
"type": "random_uniform_initializer",
"kwargs": {
"minval": -0.1,
"maxval": 0.1,
"seed": None
}
},
"regularizer": {
"type": "L1L2",
"kwargs": {
"l1": 0.,
"l2": 0
}
},
"name": "clas_embedder",
}
# GENERATOR HPARAMS
g_decoder_hparams = {
"rnn_cell": {
"type": tensorflow.contrib.cudnn_rnn.CudnnCompatibleGRUCell,
"kwargs": {
"num_units": 1024,
},
"num_layers": 2,
"dropout": {
"input_keep_prob": 1,
"output_keep_prob": 0.5,
"state_keep_prob": 1.0,
"variational_recurrent": True,
"input_size": [emb_hparams['dim'] + noise_size + 1,
1024]
},
"residual": False,
"highway": False,
},
"max_decoding_length_train": None,
"max_decoding_length_infer": None,
"helper_train": {
"type": "TrainingHelper",
"kwargs": {}
},
"helper_infer": {
"type": "SampleEmbeddingHelper",
"kwargs": {}
},
"name": "g_decoder"
}
# DISCRIMINATOR HPARAMS
disc_hparams = {
'encoder' : {
"rnn_cell": {
'type':tensorflow.contrib.cudnn_rnn.CudnnCompatibleGRUCell,
'kwargs': {'num_units': 512},
'num_layers': 2,
'dropout': {'input_keep_prob': 1.0,
'output_keep_prob': 0.5,
'state_keep_prob': 1,
'variational_recurrent': True,
'input_size': [emb_hparams['dim'] + 1, 512],
'@no_typecheck': ['input_keep_prob',
'output_keep_prob',
'state_keep_prob']},
'residual': False,
'highway': False,
'@no_typecheck': ['type']},
"output_layer": {
"num_layers": 1,
"layer_size": 1,
"activation": "identity",
"final_layer_activation": None,
"other_dense_kwargs": None,
"dropout_layer_ids": [],
"dropout_rate": 0.5,
"variational_dropout": True
},
'name' : 'discriminator',
}
}
disc_crit_hparams = {
'units' : 1,
'activation' : 'linear'
}
# CLASSIFIER HPARAMS
clas_hparams = {
'encoder' : {
"rnn_cell": {
'type':tensorflow.contrib.cudnn_rnn.CudnnCompatibleGRUCell,
'kwargs': {'num_units': 128},
'num_layers': 2,
'dropout': {'input_keep_prob': 1.0,
'output_keep_prob': 0.5,
'state_keep_prob': 1,
'variational_recurrent': True,
'input_size': [emb_hparams['dim'], 128],
'@no_typecheck': ['input_keep_prob',
'output_keep_prob',
'state_keep_prob']},
'residual': False,
'highway': False,
'@no_typecheck': ['type']},
"output_layer": {
"num_layers": 1,
"layer_size": 1,
"activation": "identity",
"final_layer_activation": None,
"other_dense_kwargs": None,
"dropout_layer_ids": [],
"dropout_rate": 0.5,
"variational_dropout": True
},
'name' : 'classifier',
}
}
clas_crit_hparams = {
'units':1,
'activation':'linear'
}
# OPTIMIZER HPARAMS
g_opt_mle_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 5e-3,
"learning_rate": 0.001
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':5}
},
"gradient_noise_scale": None,
"name": None
}
g_opt_pg_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 1e-7,
"learning_rate": 0.00005
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':5}
},
"gradient_noise_scale": None,
"name": None
}
c_opt_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 1e-4,
"learning_rate": 0.0001
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':1}
},
"gradient_noise_scale": None,
"name": None
}
d_opt_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 1e-4,
"learning_rate": 0.0001,
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':1}
},
"gradient_noise_scale": None,
"name": None
}
d_crit_opt_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 1e-3,
"learning_rate": 0.001
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':1e6}
},
"gradient_noise_scale": None,
"name": None
}
c_crit_opt_hparams = {
"optimizer": {
"type": tensorflow.contrib.opt.AdamWOptimizer,
"kwargs": {
'weight_decay' : 1e-3,
"learning_rate": 0.001
}
},
"learning_rate_decay": {
"type": "",
"kwargs": {},
"min_learning_rate": 0.0,
"start_decay_step": 0,
"end_decay_step": 1e10
},
"gradient_clip": {
"type": tensorflow.clip_by_global_norm,
"kwargs": {'clip_norm':1e6}
},
"gradient_noise_scale": None,
"name": None
}