-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_gyafc.py
498 lines (431 loc) · 19.3 KB
/
eval_gyafc.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
import torch
import time
from data_utils_yelp import DataUtil
from models import StyleTransformer
from train import get_lengths
from cnn_classify import test, CNNClassify, BiLSTMClassify
from lm_lstm import LSTM_LM, lm_ppl
import os
from utils import tensor2text, list2text
from evaluator import EvaluatorGyafc
import numpy as np
class Config():
train_src_file0 = 'data/gyafc/cleaned_train_0.txt'
train_src_file1 = 'data/gyafc/cleaned_train_1.txt'
train_trg_file = 'data/gyafc/train.attr'
dev_src_file0 = 'data/gyafc/cleaned_test_0.txt'
dev_src_file1 = 'data/gyafc/cleaned_test_1.txt'
dev_trg_file = 'data/gyafc/dev.attr'
dev_trg_file0 = 'data/gyafc/dev_0.attr'
dev_trg_file1 = 'data/gyafc/dev_1.attr'
dev_trg_ref = 'data/gyafc/cleaned_dev_ref.txt'
trg_vocab = 'data/gyafc/attr.vocab'
data_path = './data/gyafc/'
log_dir = 'runs/exp'
save_path = './save'
pretrained_embed_path = './embedding/'
device = torch.device('cuda' if True and torch.cuda.is_available() else 'cpu')
discriminator_method = 'Multi' # 'Multi' or 'Cond'
load_pretrained_embed = False
min_freq = 3
max_length = 32
embed_size = 256
d_model = 256
h = 4
num_styles = 2
num_classes = num_styles + 1 if discriminator_method == 'Multi' else 2
num_layers = 4
batch_size = 64
lr_F = 0.0001
lr_D = 0.0001
L2 = 0
iter_D = 10
iter_F = 5
F_pretrain_iter = 500#57500
log_steps = 5
eval_steps = 200
learned_pos_embed = True
dropout = 0
drop_rate_config = [(0.3, 0), (0.4, 230), (0.5, 11500)]
temperature_config = [(1, 0), (1, 1150), (0.8, 4600), (0.6, 11500)]
slf_factor = 0.1
cyc_factor = 0.2
adv_factor = 1
inp_shuffle_len = 0
inp_unk_drop_fac = 0
inp_rand_drop_fac = 0
inp_drop_prob = 1
decode = False
#max_len = 10000
lambda_span = 10000
word_mass = 0.5
word_mask = 0.8
word_keep = 0.1
word_rand = 0.1
albert_kd = False
kd_alpha = 0.5
kd_temperature = 5
bert_dump0 = 'data/targets/teacher0'
bert_dump1 = 'data/targets/teacher1'
translate = True
ckpt = 'save/Apr03055150/ckpts/1150_F.pth'
model_name = 'best-no-para-and-kd-hm1-Apr03055150-1150testset-4ref'
beam_size = 1
valid_file_0 = False#'save/Mar31141244/ckpts/300_0'#'baseline_outputs/gyafc/styins/0to1'
valid_file_1 = False#'save/Mar31141244/ckpts/300_1'#'baseline_outputs/gyafc/styins/1to0'
paraphrase = False
direct_paraphrase = False
def get_lengths(tokens, eos_idx):
lengths = torch.cumsum(tokens == eos_idx, 1)
lengths = (lengths == 0).long().sum(-1)
lengths = lengths + 1 # +1 for <eos> token
return lengths
def auto_eval(config, data, model_F, model_name, temperature=1):
if model_F:
model_F.eval()
vocab_size = len(data.tokenizer)
eos_idx = config.eos_id
config.save_folder = config.save_path + '/' + model_name
os.makedirs(config.save_folder)
print('Save Path:', config.save_folder)
def inference(data, raw_style):
gold_text = []
raw_output = []
rev_output = []
while True:
if raw_style == 0:
inp_tokens, _ , eop = data.next_dev0(dev_batch_size = 128, sort = False)
else:
inp_tokens, _ , eop = data.next_dev1(dev_batch_size = 128, sort = False)
inp_lengths = get_lengths(inp_tokens, eos_idx)
raw_styles = torch.full_like(inp_tokens[:, 0], raw_style)
rev_styles = 1 - raw_styles
with torch.no_grad():
raw_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
raw_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
with torch.no_grad():
rev_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
rev_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
gold_text += tensor2text(data, inp_tokens.cpu())
raw_output += tensor2text(data, raw_log_probs.argmax(-1).cpu())
rev_output += tensor2text(data, rev_log_probs.argmax(-1).cpu())
if eop: break
return gold_text, raw_output, rev_output
def inference_direct_paraphrase(data, raw_style):
gold_text = []
raw_output = []
rev_output = []
while True:
if raw_style == 0:
inp_tokens, _ , eop = data.next_dev0(dev_batch_size = 128, sort = False)
else:
inp_tokens, _ , eop = data.next_dev1(dev_batch_size = 128, sort = False)
inp_lengths = get_lengths(inp_tokens, eos_idx)
raw_styles = torch.full_like(inp_tokens[:, 0], raw_style)
para_styles = torch.ones_like(raw_styles) * 2
with torch.no_grad():
raw_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
raw_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
with torch.no_grad():
rev_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
para_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
gold_text += tensor2text(data, inp_tokens.cpu())
raw_output += tensor2text(data, raw_log_probs.argmax(-1).cpu())
rev_output += tensor2text(data, rev_log_probs.argmax(-1).cpu())
if eop: break
return gold_text, raw_output, rev_output
def inference_paraphrase(data, raw_style):
gold_text = []
raw_output = []
rev_output = []
while True:
if raw_style == 0:
inp_tokens, _ , eop = data.next_dev0(dev_batch_size = 128, sort = False)
else:
inp_tokens, _ , eop = data.next_dev1(dev_batch_size = 128, sort = False)
inp_lengths = get_lengths(inp_tokens, eos_idx)
raw_styles = torch.full_like(inp_tokens[:, 0], raw_style)
rev_styles = 1 - raw_styles
para_styles = torch.ones_like(rev_styles) * 2
with torch.no_grad():
raw_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
raw_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
with torch.no_grad():
gen_log_probs = model_F(
inp_tokens,
None,
inp_lengths,
para_styles,
generate=True,
differentiable_decode=True,
temperature=temperature,
)
gen_soft_tokens = gen_log_probs.exp()
gen_lengths = get_lengths(gen_soft_tokens.argmax(-1), eos_idx)
rev_log_probs = model_F(
gen_soft_tokens,
inp_tokens,
gen_lengths,
rev_styles,
generate=True,
differentiable_decode=False,
temperature=temperature,
)
gold_text += tensor2text(data, inp_tokens.cpu())
raw_output += tensor2text(data, raw_log_probs.argmax(-1).cpu())
rev_output += tensor2text(data, rev_log_probs.argmax(-1).cpu())
if eop: break
return gold_text, raw_output, rev_output
if config.translate == True:
if config.direct_paraphrase:
gold_text0, raw_output0, rev_output0 = inference_direct_paraphrase(data, 0)
gold_text1, raw_output1, rev_output1 = inference_direct_paraphrase(data, 1)
elif config.paraphrase == True:
gold_text0, raw_output0, rev_output0 = inference_paraphrase(data, 0)
gold_text1, raw_output1, rev_output1 = inference_paraphrase(data, 1)
else:
gold_text0, raw_output0, rev_output0 = inference(data, 0)
gold_text1, raw_output1, rev_output1 = inference(data, 1)
gold_text = (gold_text0, gold_text1)
raw_output = (raw_output0,raw_output1)
rev_output = (rev_output0, rev_output1)
valid_file_0 = os.path.join( config.save_folder + '/', str(model_name) + '_0')
valid_file_1 = os.path.join( config.save_folder + '/', str(model_name) + '_1')
out_file_0 = open(valid_file_0, 'w', encoding='utf-8')
out_file_1 = open(valid_file_1, 'w', encoding='utf-8')
for i in range(len(rev_output[0])):
line_0 = rev_output[0][i].strip()
out_file_0.write(line_0 + '\n')
out_file_0.flush()
for i in range(len(rev_output[1])):
line_1 = rev_output[1][i].strip()
out_file_1.write(line_1 + '\n')
out_file_1.flush()
out_file_0.close()
out_file_1.close()
else:
rev_output = []
valid_file_0 = config.valid_file_0
valid_file_1 = config.valid_file_1
with open(valid_file_0, 'r', encoding='utf-8') as f:
src_lines_0 = f.read().split('\n')
rev_output.append(src_lines_0)
with open(valid_file_1, 'r', encoding='utf-8') as f:
src_lines_1 = f.read().split('\n')
rev_output.append(src_lines_1)
evaluator = EvaluatorGyafc()
ref_text = evaluator.yelp_ref
#acc_neg = evaluator.yelp_acc_0(rev_output[0])
acc_mod, _ = test(evaluator.classifier, data, 128, valid_file_0, config.dev_trg_file0, negate = True)
acc_cla, _ = test(evaluator.classifier, data, 128, valid_file_1, config.dev_trg_file1, negate = True)
#acc_pos = evaluator.yelp_acc_1(rev_output[1])
bleu_mod = evaluator.yelp_ref_bleu_0(rev_output[0])
bleu_cla = evaluator.yelp_ref_bleu_1(rev_output[1])
_ , ppl_mod = 0, 0#lm_ppl(evaluator.lm1, data, 128, valid_file_0, config.dev_trg_file0) #evaluator.yelp_ppl(rev_output[0])
_ , ppl_cla = 0, 0#lm_ppl(evaluator.lm0, data, 128, valid_file_1, config.dev_trg_file1) #evaluator.yelp_ppl(rev_output[1])
sim_mod = 0#evaluator.ref_similarity_0(valid_file_0, str(model_name))
sim_cla = 0#evaluator.ref_similarity_1(valid_file_1, str(model_name))
bartscore_mod = 0#evaluator.ref_bartscore_0(rev_output[0])
bartscore_cla = 0#evaluator.ref_bartscore_1(rev_output[1])
print(('[auto_eval] acc_cla: {:.4f} acc_mod: {:.4f} ' + \
'bleu_cla: {:.4f} bleu_mod: {:.4f} ' + \
'sim_cla: {:.4f} sim_mod: {:.4f} ' + \
'bartscore_cla: {:.4f} bartscore_mod: {:.4f} ' + \
'ppl_cla: {:.4f} ppl_mod: {:.4f}\n').format(
acc_cla, acc_mod, bleu_cla, bleu_mod, sim_cla, sim_mod, bartscore_cla, bartscore_mod, ppl_cla, ppl_mod
))
# save output
eval_log_file = config.save_folder + '/eval_log.txt'
with open(eval_log_file, 'a') as fl:
print(('{:18s}: acc_cla: {:.4f} acc_mod: {:.4f} ' + \
'bleu_cla: {:.4f} bleu_mod: {:.4f} ' + \
'sim_cla: {:.4f} sim_mod: {:.4f} ' + \
'bartscore_cla: {:.4f} bartscore_mod: {:.4f} ' + \
'ppl_cla: {:.4f} ppl_mod: {:.4f}\n').format(
model_name, acc_cla, acc_mod, bleu_cla, bleu_mod, sim_cla, sim_mod, bartscore_cla, bartscore_mod, ppl_cla, ppl_mod
), file=fl)
if config.translate == True:
save_file = config.save_folder + '/' + str(model_name) + '.txt'
with open(save_file, 'w') as fw:
print(('[auto_eval] acc_cla: {:.4f} acc_mod: {:.4f} ' + \
'bleu_cla: {:.4f} bleu_mod: {:.4f} ' + \
'sim_cla: {:.4f} sim_mod: {:.4f} ' + \
'bartscore_cla: {:.4f} bartscore_mod: {:.4f} ' + \
'ppl_cla: {:.4f} ppl_mod: {:.4f}\n').format(
acc_cla, acc_mod, bleu_cla, bleu_mod, sim_cla, sim_mod, bartscore_cla, bartscore_mod, ppl_cla, ppl_mod
), file=fw)
for idx in range(len(rev_output[0])):
print('*' * 20, 'classic sample', '*' * 20, file=fw)
print('[gold]', gold_text[0][idx], file=fw)
print('[raw ]', raw_output[0][idx], file=fw)
print('[rev ]', rev_output[0][idx], file=fw)
print('[ref ]', ref_text[0][idx], file=fw)
print('*' * 20, '********', '*' * 20, file=fw)
for idx in range(len(rev_output[1])):
print('*' * 20, 'modern sample', '*' * 20, file=fw)
print('[gold]', gold_text[1][idx], file=fw)
print('[raw ]', raw_output[1][idx], file=fw)
print('[rev ]', rev_output[1][idx], file=fw)
print('[ref ]', ref_text[1][idx], file=fw)
print('*' * 20, '********', '*' * 20, file=fw)
def beam_eval(config, data, model_F, model_name, temperature=1):
model_F.eval()
vocab_size = len(data.tokenizer)
eos_idx = config.eos_id
config.save_folder = config.save_path + '/' + model_name
os.makedirs(config.save_folder)
print('Save Path:', config.save_folder)
def beam_inference(data, raw_style):
gold_text = []
rev_output = []
while True:
if raw_style == 0:
inp_tokens, _ , eop = data.next_dev0(dev_batch_size = 256, sort = False)
else:
inp_tokens, _ , eop = data.next_dev1(dev_batch_size = 256, sort = False)
inp_lengths = get_lengths(inp_tokens, eos_idx)
raw_styles = torch.full_like(inp_tokens[:, 0], raw_style)
rev_styles = 1 - raw_styles
hyps = []
batch_size = inp_tokens.size(0)
for i in range(batch_size):
x = inp_tokens[i,:].unsqueeze(0)
inp_length = inp_lengths[i].unsqueeze(0)
rev_style = rev_styles[i].unsqueeze(0)
if config.paraphrase:
para_style = torch.ones_like(rev_style)*2
gen_log_probs = model_F(
x,
None,
inp_length,
para_style,
generate=True,
differentiable_decode=True,
temperature=1,
)
gen_soft_tokens = gen_log_probs.exp()
gen_lengths = get_lengths(gen_soft_tokens.argmax(-1), config.eos_id)
hyp = model_F.translate_sent(gen_soft_tokens, gen_lengths, rev_style, temperature, max_len=config.max_length, beam_size=config.beam_size, poly_norm_m=1)[0]
else:
hyp = model_F.translate_sent(x, inp_length, rev_style, temperature, max_len=config.max_length, beam_size=config.beam_size, poly_norm_m=1)[0]
hyps.append(hyp.y)
gold_text += tensor2text(data, inp_tokens.cpu())
rev_output += list2text(data, hyps)
if eop: break
return gold_text, rev_output
gold_text0, rev_output0 = beam_inference(data, 0)
gold_text1, rev_output1 = beam_inference(data, 1)
gold_text = (gold_text0, gold_text1)
rev_output = (rev_output0, rev_output1)
valid_file_0 = os.path.join( config.save_folder + '/' , str(model_name) + '_0')
valid_file_1 = os.path.join( config.save_folder + '/' , str(model_name) + '_1')
out_file_0 = open(valid_file_0, 'w', encoding='utf-8')
out_file_1 = open(valid_file_1, 'w', encoding='utf-8')
for i in range(len(rev_output[0])):
line_0 = rev_output[0][i].strip()
out_file_0.write(line_0 + '\n')
out_file_0.flush()
for i in range(len(rev_output[1])):
line_1 = rev_output[1][i].strip()
out_file_1.write(line_1 + '\n')
out_file_1.flush()
out_file_0.close()
out_file_1.close()
evaluator = EvaluatorGyafc()
ref_text = evaluator.yelp_ref
#acc_neg = evaluator.yelp_acc_0(rev_output[0])
acc_mod, _ = test(evaluator.classifier, data, 128, valid_file_0, config.dev_trg_file0, negate = True)
acc_cla, _ = test(evaluator.classifier, data, 128, valid_file_1, config.dev_trg_file1, negate = True)
#acc_pos = evaluator.yelp_acc_1(rev_output[1])
bleu_mod = evaluator.yelp_ref_bleu_0(rev_output[0])
bleu_cla = evaluator.yelp_ref_bleu_1(rev_output[1])
_ , ppl_mod = 0, 0#lm_ppl(evaluator.lm1, data, 128, valid_file_0, config.dev_trg_file0) #evaluator.yelp_ppl(rev_output[0])
_ , ppl_cla = 0, 0#lm_ppl(evaluator.lm0, data, 128, valid_file_1, config.dev_trg_file1) #evaluator.yelp_ppl(rev_output[1])
sim_mod = 0#evaluator.ref_similarity_0(valid_file_0, str(model_name))
sim_cla = 0#evaluator.ref_similarity_1(valid_file_1, str(model_name))
bartscore_mod = 0#evaluator.ref_bartscore_0(rev_output[0])
bartscore_cla = 0#evaluator.ref_bartscore_1(rev_output[1])
for k in range(5):
idx = np.random.randint(len(rev_output[0]))
print('*' * 20, 'classic sample', '*' * 20)
print('[gold]', gold_text[0][idx])
print('[rev ]', rev_output[0][idx])
print('[ref ]', ref_text[0][idx])
print('*' * 20, '********', '*' * 20)
for k in range(5):
idx = np.random.randint(len(rev_output[1]))
print('*' * 20, 'modern sample', '*' * 20)
print('[gold]', gold_text[1][idx])
print('[rev ]', rev_output[1][idx])
print('[ref ]', ref_text[1][idx])
print('*' * 20, '********', '*' * 20)
print(('[auto_eval] acc_cla: {:.4f} acc_mod: {:.4f} ' + \
'bleu_cla: {:.4f} bleu_mod: {:.4f} ' + \
'sim_cla: {:.4f} sim_mod: {:.4f} ' + \
'bartscore_cla: {:.4f} bartscore_mod: {:.4f} ' + \
'ppl_cla: {:.4f} ppl_mod: {:.4f}\n').format(
acc_cla, acc_mod, bleu_cla, bleu_mod, sim_cla, sim_mod, bartscore_cla, bartscore_mod, ppl_cla, ppl_mod
))
# save output
eval_log_file = config.save_folder + '/eval_log.txt'
with open(eval_log_file, 'a') as fl:
print(('iter{:18s}: acc_cla: {:.4f} acc_mod: {:.4f} ' + \
'bleu_cla: {:.4f} bleu_mod: {:.4f} ' + \
'sim_cla: {:.4f} sim_mod: {:.4f} ' + \
'bartscore_cla: {:.4f} bartscore_mod: {:.4f} ' + \
'ppl_cla: {:.4f} ppl_mod: {:.4f}\n').format(
model_name, acc_cla, acc_mod, bleu_cla, bleu_mod, sim_cla, sim_mod, bartscore_cla, bartscore_mod, ppl_cla, ppl_mod
), file=fl)
def main():
config = Config()
data = DataUtil(config)
print('Vocab size:', config.src_vocab_size)
if config.valid_file_0:
assert config.translate == False
assert config.beam_size == 1
model_F = None
else:
state_dict = torch.load(config.ckpt)
model_F = StyleTransformer(config, data).to(config.device)
model_F.load_state_dict(state_dict)
if config.beam_size == 1:
auto_eval(config, data, model_F, config.model_name)
else:
beam_eval(config, data, model_F, config.model_name)
if __name__ == '__main__':
main()