-
Notifications
You must be signed in to change notification settings - Fork 5
/
manip_e2e_clean.py
695 lines (550 loc) · 25.5 KB
/
manip_e2e_clean.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#!/usr/bin/env python3
"""
Textontent Manipulation
3-gated copy net.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# pylint: disable=invalid-name, no-member, too-many-locals
import importlib
import os
import numpy as np
import tensorflow as tf
import texar as tx
import pickle
from copy_net import CopyNetWrapper
from texar.core import get_train_op
from utils_e2e import *
from get_xx import get_match
from get_xy import get_align
# from ie import get_precrec
flags = tf.flags
flags.DEFINE_string("data_type", "e2e", "Dataset to evaluate: nba or e2e")
flags.DEFINE_string("config_data", "config_data_e2e", "The data config.")
flags.DEFINE_string("config_model", "config_model", "The model config.")
flags.DEFINE_string("config_train", "config_train", "The training config.")
flags.DEFINE_float("rec_w", 0.8, "Weight of reconstruction loss.")
flags.DEFINE_float("rec_w_rate", 0., "Increasing rate of rec_w.")
flags.DEFINE_boolean("add_bleu_weight", False, "Whether to multiply BLEU weight"
" onto the first loss.")
flags.DEFINE_string("expr_name", "nba", "The experiment name. "
"Used as the directory name of run.")
flags.DEFINE_string("restore_from", "", "The specific checkpoint path to "
"restore from. If not specified, the latest checkpoint in "
"expr_name is used.")
flags.DEFINE_boolean("copy_x", False, "Whether to copy from x.")
flags.DEFINE_boolean("copy_y_", False, "Whether to copy from y'.")
flags.DEFINE_boolean("coverage", False, "Whether to add coverage onto the copynets.")
flags.DEFINE_float("exact_cover_w", 0., "Weight of exact coverage loss.")
flags.DEFINE_float("eps", 1e-10, "epsilon used to avoid log(0).")
flags.DEFINE_integer("disabled_vocab_size", 0, "Disabled vocab size.")
flags.DEFINE_boolean("attn_x", False, "Whether to attend x.")
flags.DEFINE_boolean("attn_y_", False, "Whether to attend y'.")
flags.DEFINE_boolean("sd_path", False, "Whether to add structured data path.")
flags.DEFINE_float("sd_path_multiplicator", 1., "Structured data path multiplicator.")
flags.DEFINE_float("sd_path_addend", 0., "Structured data path addend.")
flags.DEFINE_boolean("align", False, "Whether it is to get alignment.")
flags.DEFINE_boolean("output_align", False, "Whether to output alignment.")
flags.DEFINE_boolean("verbose", False, "verbose.")
flags.DEFINE_boolean("eval_ie", False, "Whether evaluate IE.")
flags.DEFINE_integer("eval_ie_gpuid", 0, "ID of GPU on which IE runs.")
FLAGS = flags.FLAGS
copy_flag = FLAGS.copy_x or FLAGS.copy_y_
attn_flag = FLAGS.attn_x or FLAGS.attn_y_
if FLAGS.output_align:
FLAGS.align = True
config_data = importlib.import_module(FLAGS.config_data)
config_model = importlib.import_module(FLAGS.config_model)
config_train = importlib.import_module(FLAGS.config_train)
expr_name = FLAGS.expr_name
restore_from = FLAGS.restore_from
data_type = FLAGS.data_type
dir_summary = os.path.join(expr_name, 'log')
dir_model = os.path.join(expr_name, 'ckpt')
dir_best = os.path.join(expr_name, 'ckpt-best')
ckpt_model = os.path.join(dir_model, 'model.ckpt')
ckpt_best = os.path.join(dir_best, 'model.ckpt')
def get_optimistic_restore_variables(ckpt_path, graph=tf.get_default_graph()):
reader = tf.train.NewCheckpointReader(ckpt_path)
saved_shapes = reader.get_variable_to_shape_map()
var_names = sorted([
(var.name, var.name.split(':')[0]) for var in tf.global_variables()
if var.name.split(':')[0] in saved_shapes])
restore_vars = []
for var_name, saved_var_name in var_names:
var = graph.get_tensor_by_name(var_name)
var_shape = var.get_shape().as_list()
if var_shape == saved_shapes[saved_var_name]:
restore_vars.append(var)
return restore_vars
def get_optimistic_saver(ckpt_path, graph=tf.get_default_graph()):
return tf.train.Saver(
get_optimistic_restore_variables(ckpt_path, graph=graph))
def print_alignment(data, sent, score):
print(' ' * 20 + ' '.join(map('{:>12}'.format, data[0])))
for j, sent_token in enumerate(sent[0]):
print('{:>20}'.format(sent_token) + ' '.join(map(
lambda x: '{:12.2e}'.format(x) if x != 0 else ' ' * 12,
score[:, j])))
def batch_print_alignment(datas, sents, scores):
datas, sents = map(
lambda texts_lengths: map(
lambda text_length:
(text_length[0][:text_length[1]], text_length[1]),
zip(*texts_lengths)),
(datas, sents))
for data, sent, score in zip(datas, sents, scores):
score = score[:data[1], :sent[1]]
print_alignment(data, sent, score)
def get_match_align(text00, text01, text02, text10, text11, text12, sent_text):
"""Combining match and align. All texts must not contain BOS.
"""
matches = get_match(text00, text01, text02, text10, text11, text12)
aligns = get_align(text10, text11, text12, sent_text)
match = {i: j for i, j in matches}
n = len(text00)
m = len(sent_text)
ret = np.zeros([n, m], dtype=np.float32)
for i in range(n):
try:
k = match[i]
except KeyError:
continue
align = aligns[k]
ret[i][:len(align)] = align
if FLAGS.verbose:
print(' ' * 20 + ' '.join(map(
'{:>12}'.format, strip_special_tokens_of_list(text00))))
for j, sent_token in enumerate(strip_special_tokens_of_list(sent_text)):
print('{:>20}'.format(sent_token) + ' '.join(map(
lambda x: '{:>12}'.format(x) if x != 0 else ' ' * 12,
ret[:, j])))
return ret
def batch_get_match_align(*texts):
return np.array(batchize(get_match_align)(*texts), dtype=np.float32)
def build_model(data_batch, data, step):
batch_size, num_steps = [
tf.shape(data_batch["x_value_text_ids"])[d] for d in range(2)]
vocab = data.vocab('y_aux')
id2str = '<{}>'.format
bos_str, eos_str = map(id2str, (vocab.bos_token_id, vocab.eos_token_id))
def single_bleu(ref, hypo):
ref = [id2str(u if u != vocab.unk_token_id else -1) for u in ref]
hypo = [id2str(u) for u in hypo]
ref = tx.utils.strip_special_tokens(
' '.join(ref), strip_bos=bos_str, strip_eos=eos_str)
hypo = tx.utils.strip_special_tokens(
' '.join(hypo), strip_eos=eos_str)
return 0.01 * tx.evals.sentence_bleu(references=[ref], hypothesis=hypo)
def batch_bleu(refs, hypos):
return np.array(
[single_bleu(ref, hypo) for ref, hypo in zip(refs, hypos)],
dtype=np.float32)
def lambda_anneal(step_stage):
print('==========step_stage is {}'.format(step_stage))
if step_stage <= 1:
rec_weight = 1
elif step_stage > 1 and step_stage < 2:
rec_weight = FLAGS.rec_w - step_stage * 0.1
return np.array(rec_weight, dtype = tf.float32)
# losses
losses = {}
# embedders
embedders = {
name: tx.modules.WordEmbedder(
vocab_size=data.vocab(name).size, hparams=hparams)
for name, hparams in config_model.embedders.items()}
# encoders
y_encoder = tx.modules.BidirectionalRNNEncoder(
hparams=config_model.y_encoder)
x_encoder = tx.modules.BidirectionalRNNEncoder(
hparams=config_model.x_encoder)
def concat_encoder_outputs(outputs):
return tf.concat(outputs, -1)
def encode(ref_flag):
y_str = y_strs[ref_flag]
y_ids = data_batch['{}_text_ids'.format(y_str)]
y_embeds = embedders['y_aux'](y_ids)
y_sequence_length = data_batch['{}_length'.format(y_str)]
y_enc_outputs, _ = y_encoder(
y_embeds, sequence_length=y_sequence_length)
y_enc_outputs = concat_encoder_outputs(y_enc_outputs)
x_str = x_strs[ref_flag]
x_ids = {
field: data_batch['{}_{}_text_ids'.format(x_str, field)][:, 1:-1]
for field in x_fields}
x_embeds = tf.concat(
[embedders['x_{}'.format(field)](x_ids[field]) for field in x_fields],
axis=-1)
x_sequence_length = data_batch[
'{}_{}_length'.format(x_str, x_fields[0])] - 2
x_enc_outputs, _ = x_encoder(
x_embeds, sequence_length=x_sequence_length)
x_enc_outputs = concat_encoder_outputs(x_enc_outputs)
return y_ids, y_embeds, y_enc_outputs, y_sequence_length, \
x_ids, x_embeds, x_enc_outputs, x_sequence_length
encode_results = [encode(ref_flag) for ref_flag in range(2)]
y_ids, y_embeds, y_enc_outputs, y_sequence_length, \
x_ids, x_embeds, x_enc_outputs, x_sequence_length = \
zip(*encode_results)
# get rnn cell
rnn_cell = tx.core.layers.get_rnn_cell(config_model.rnn_cell)
def get_decoder(cell, y__ref_flag, x_ref_flag, tgt_ref_flag,
beam_width=None):
output_layer_params = \
{'output_layer': tf.identity} if copy_flag else \
{'vocab_size': vocab.size}
if attn_flag: # attention
if FLAGS.attn_x and FLAGS.attn_y_:
memory = tf.concat(
[y_enc_outputs[y__ref_flag],
x_enc_outputs[x_ref_flag]],
axis=1)
memory_sequence_length = None
elif FLAGS.attn_y_:
memory = y_enc_outputs[y__ref_flag]
memory_sequence_length = y_sequence_length[y__ref_flag]
elif FLAGS.attn_x:
memory = x_enc_outputs[x_ref_flag]
memory_sequence_length = x_sequence_length[x_ref_flag]
else:
raise Exception(
"Must specify either y__ref_flag or x_ref_flag.")
attention_decoder = tx.modules.AttentionRNNDecoder(
cell=cell,
memory=memory,
memory_sequence_length=memory_sequence_length,
hparams=config_model.attention_decoder,
**output_layer_params)
if not copy_flag:
return attention_decoder
cell = attention_decoder.cell if beam_width is None else \
attention_decoder._get_beam_search_cell(beam_width)
if copy_flag: # copynet
kwargs = {
'y__ids': y_ids[y__ref_flag][:, 1:],
'y__states': y_enc_outputs[y__ref_flag][:, 1:],
'y__lengths': y_sequence_length[y__ref_flag] - 1,
'x_ids': x_ids[x_ref_flag]['value'],
'x_states': x_enc_outputs[x_ref_flag],
'x_lengths': x_sequence_length[x_ref_flag],
}
if tgt_ref_flag is not None:
kwargs.update({
'input_ids': data_batch[
'{}_text_ids'.format(y_strs[tgt_ref_flag])][:, :-1]})
memory_prefixes = []
if FLAGS.copy_y_:
memory_prefixes.append('y_')
if FLAGS.copy_x:
memory_prefixes.append('x')
if beam_width is not None:
kwargs = {
name: tile_batch(value, beam_width)
for name, value in kwargs.items()}
def get_get_copy_scores(memory_ids_states_lengths, output_size):
memory_copy_states = [
tf.layers.dense(
memory_states,
units=output_size,
activation=None,
use_bias=False)
for _, memory_states, _ in memory_ids_states_lengths]
def get_copy_scores(query, coverities=None):
ret = []
if FLAGS.copy_y_:
memory = memory_copy_states[len(ret)]
if coverities is not None:
memory = memory + tf.layers.dense(
coverities[len(ret)],
units=output_size,
activation=None,
use_bias=False)
memory = tf.nn.tanh(memory)
ret_y_ = tf.einsum("bim,bm->bi", memory, query)
ret.append(ret_y_)
if FLAGS.copy_x:
memory = memory_copy_states[len(ret)]
if coverities is not None:
memory = memory + tf.layers.dense(
coverities[len(ret)],
units=output_size,
activation=None,
use_bias=False)
memory = tf.nn.tanh(memory)
ret_x = tf.einsum("bim,bm->bi", memory, query)
ret.append(ret_x)
return ret
return get_copy_scores
cell = CopyNetWrapper(
cell=cell, vocab_size=vocab.size,
memory_ids_states_lengths=[
tuple(kwargs['{}_{}'.format(prefix, s)]
for s in ('ids', 'states', 'lengths'))
for prefix in memory_prefixes],
input_ids=\
kwargs['input_ids'] if tgt_ref_flag is not None else None,
get_get_copy_scores=get_get_copy_scores,
coverity_dim=config_model.coverity_dim if FLAGS.coverage else None,
coverity_rnn_cell_hparams=config_model.coverity_rnn_cell if FLAGS.coverage else None,
disabled_vocab_size=FLAGS.disabled_vocab_size,
eps=FLAGS.eps)
decoder = tx.modules.BasicRNNDecoder(
cell=cell, hparams=config_model.decoder,
**output_layer_params)
return decoder
def get_decoder_and_outputs(
cell, y__ref_flag, x_ref_flag, tgt_ref_flag, params,
beam_width=None):
decoder = get_decoder(
cell, y__ref_flag, x_ref_flag, tgt_ref_flag,
beam_width=beam_width)
if beam_width is None:
ret = decoder(**params)
else:
ret = tx.modules.beam_search_decode(
decoder_or_cell=decoder,
beam_width=beam_width,
**params)
return (decoder,) + ret
get_decoder_and_outputs = tf.make_template(
'get_decoder_and_outputs', get_decoder_and_outputs)
def teacher_forcing(cell, y__ref_flag, x_ref_flag, loss_name):
tgt_ref_flag = x_ref_flag
tgt_str = y_strs[tgt_ref_flag]
sequence_length = data_batch['{}_length'.format(tgt_str)] - 1
decoder, tf_outputs, final_state, _ = get_decoder_and_outputs(
cell, y__ref_flag, x_ref_flag, tgt_ref_flag,
{'decoding_strategy': 'train_greedy',
'inputs': y_embeds[tgt_ref_flag],
'sequence_length': sequence_length})
tgt_y_ids = data_batch['{}_text_ids'.format(tgt_str)][:, 1:]
loss = tx.losses.sequence_sparse_softmax_cross_entropy(
labels=tgt_y_ids,
logits=tf_outputs.logits,
sequence_length=sequence_length,
average_across_batch=False)
loss = tf.reduce_mean(loss, 0)
if copy_flag and FLAGS.exact_cover_w != 0:
sum_copy_probs = list(map(lambda t: tf.cast(t, tf.float32), final_state.sum_copy_probs))
memory_lengths = [lengths for _, _, lengths in decoder.cell.memory_ids_states_lengths]
exact_coverage_losses = [
tf.reduce_mean(tf.reduce_sum(
tx.utils.mask_sequences(
tf.square(sum_copy_prob - 1.), memory_length),
1))
for sum_copy_prob, memory_length in zip(sum_copy_probs, memory_lengths)]
print_xe_loss_op = tf.print(loss_name, 'xe loss:', loss)
with tf.control_dependencies([print_xe_loss_op]):
for i, exact_coverage_loss in enumerate(exact_coverage_losses):
print_op = tf.print(loss_name, 'exact coverage loss {:d}:'.format(i), exact_coverage_loss)
with tf.control_dependencies([print_op]):
#exact_cover_w = FLAGS.exact_cover_w + FLAGS.exact_cover_w * tf.cast(step, tf.float32)
loss += FLAGS.exact_cover_w * exact_coverage_loss
losses[loss_name] = loss
return decoder, tf_outputs, loss
def beam_searching(cell, y__ref_flag, x_ref_flag, beam_width):
start_tokens = tf.ones_like(data_batch['y_aux_length']) * \
vocab.bos_token_id
end_token = vocab.eos_token_id
# helper = tx.modules.TopKSampleEmbeddingHelper(
# embedding=embedders['y_aux'],
# start_tokens=start_tokens,
# end_token=end_token,
# top_k=FLAGS.top_k,
# softmax_temperature=FLAGS.temperature)
decoder, bs_outputs, _, _ = get_decoder_and_outputs(
cell, y__ref_flag, x_ref_flag, None,
{'embedding': embedders['y_aux'],
'start_tokens': start_tokens,
'end_token': end_token,
'max_decoding_length': config_train.infer_max_decoding_length,
'decoding_strategy': 'infer_sample'},
beam_width=None)#config_train.infer_beam_width)
return decoder, bs_outputs
decoder, tf_outputs, loss = teacher_forcing(rnn_cell, 1, 0, 'MLE')
rec_decoder, _, rec_loss = teacher_forcing(rnn_cell, 1, 1, 'REC')
rec_weight = FLAGS.rec_w
step_stage = tf.cast(step, tf.float32) / tf.constant(921.0)
rec_weight = tf.case([(tf.less_equal(step_stage, tf.constant(1.0)), lambda:tf.constant(1.0)),\
(tf.greater(step_stage, tf.constant(1.0)), lambda:FLAGS.rec_w)],\
default=lambda:tf.constant(1.0) - (step_stage - 1) * (1 - FLAGS.rec_w))
joint_loss = (1 - rec_weight) * loss + rec_weight * rec_loss
losses['joint'] = joint_loss
tiled_decoder, bs_outputs = beam_searching(
rnn_cell, 1, 0, config_train.infer_beam_width)
train_ops = {
name: get_train_op(losses[name], hparams=config_train.train[name])
for name in config_train.train}
return train_ops, bs_outputs
def main():
# data batch
datasets = {mode: tx.data.MultiAlignedData(hparams)
for mode, hparams in config_data.datas.items()}
data_iterator = tx.data.FeedableDataIterator(datasets)
data_batch = data_iterator.get_next()
global_step = tf.train.get_or_create_global_step()
train_ops, bs_outputs \
= build_model(data_batch, datasets['train'], global_step)
summary_ops = {
name: tf.summary.merge(
tf.get_collection(
tf.GraphKeys.SUMMARIES,
scope=get_scope_name_of_train_op(name)),
name=get_scope_name_of_summary_op(name))
for name in train_ops.keys()}
saver = tf.train.Saver(max_to_keep=None)
global best_ever_val_bleu
best_ever_val_bleu = 0.
def _save_to(directory, step):
print('saving to {} ...'.format(directory))
saved_path = saver.save(sess, directory, global_step=step)
print('saved to {}'.format(saved_path))
def _restore_from_path(ckpt_path):
print('restoring from {} ...'.format(ckpt_path))
try:
saver.restore(sess, ckpt_path)
except tf.errors.NotFoundError:
print('Some variables are missing. Try optimistically restoring.')
(get_optimistic_saver(ckpt_path)).restore(sess, ckpt_path)
print('done.')
def _restore_from(directory):
if os.path.exists(directory):
ckpt_path = tf.train.latest_checkpoint(directory)
_restore_from_path(ckpt_path)
else:
print('cannot find checkpoint directory {}'.format(directory))
def _train_epoch(sess, summary_writer, mode, train_op, summary_op):
print('in _train_epoch')
data_iterator.restart_dataset(sess, mode)
feed_dict = {
tx.global_mode(): tf.estimator.ModeKeys.TRAIN,
data_iterator.handle: data_iterator.get_handle(sess, mode),
}
while True:
try:
loss, summary = sess.run((train_op, summary_op), feed_dict)
step = tf.train.global_step(sess, global_step)
print('step {:d}: loss = {:.6f}'.format(step, loss))
summary_writer.add_summary(summary, step)
if step > 500:
if step % config_train.steps_per_eval == 0:
_eval_epoch(sess, summary_writer, 'val')
except tf.errors.OutOfRangeError:
break
print('end _train_epoch')
def _eval_epoch(sess, summary_writer, mode):
global best_ever_val_bleu
print('in _eval_epoch with mode {}'.format(mode))
data_iterator.restart_dataset(sess, mode)
feed_dict = {
tx.global_mode(): tf.estimator.ModeKeys.EVAL,
data_iterator.handle: data_iterator.get_handle(sess, mode)
}
step = tf.train.global_step(sess, global_step)
ref_hypo_pairs = []
fetches = [
[data_batch['y_aux_text'], data_batch['y_ref_text']],
# bs_outputs.predicted_ids, for beam_search
bs_outputs.sample_id, # for sample_id
] if not FLAGS.align else [
align_bs_outputs.predicted_ids,
]
if not os.path.exists(dir_model):
os.makedirs(dir_model)
hypo_file_name = os.path.join(
dir_model, "hypos.step{}.{}.txt".format(step, mode))
hypo_file = open(hypo_file_name, "w")
cnt = 0
while True:
try:
target_texts, output_ids = sess.run(fetches, feed_dict)
target_texts = [
tx.utils.strip_special_tokens(
texts[:, 1:].tolist(), is_token_list=True)
for texts in target_texts]
# output_ids = output_ids[:, :, 0] for beam_search
output_texts = tx.utils.map_ids_to_strs(
ids=output_ids.tolist(), vocab=datasets[mode].vocab('y_aux'),
join=False)
target_texts = list(zip(*target_texts))
for ref, hypo in zip(target_texts, output_texts):
if cnt < 10:
print('cnt = {}'.format(cnt))
for i, s in enumerate(ref):
print('ref{}: {}'.format(i, ' '.join(s)))
print('hypo: {}'.format(' '.join(hypo)))
print(' '.join(hypo), file=hypo_file)
cnt += 1
print('processed {} samples'.format(cnt))
ref_hypo_pairs.extend(zip(target_texts, output_texts))
except tf.errors.OutOfRangeError:
break
hypo_file.close()
if FLAGS.eval_ie:
gold_file_name = os.path.join(
config_data.dst_dir, "gold.{}.txt".format(
config_data.mode_to_filemode[mode]))
inter_file_name = "{}.h5".format(hypo_file_name[:-len(".txt")])
prec, rec = get_precrec(
gold_file_name, hypo_file_name, inter_file_name,
gpuid=FLAGS.eval_ie_gpuid)
#print('========entry_texts are: {}'.format(entry_texts[:10]))
refs, hypos = zip(*ref_hypo_pairs)
#refs_ = list(zip(*refs))
bleus = []
get_bleu_name = '{}_BLEU'.format
for i in range(1, 2):
refs_ = list(map(lambda ref: ref[i:i+1], refs))
# ents_ = list(map(lambda ent: ent[i:i+1], entrys))
# entrys = list(zip(*entrys))
bleu = corpus_bleu(refs_, hypos)
bleus.append(bleu)
summary = tf.Summary()
for i, bleu in enumerate(bleus):
summary.value.add(
tag='{}/{}'.format(mode, get_bleu_name(i)), simple_value=bleu)
if FLAGS.eval_ie:
for name, value in {'precision': prec, 'recall': rec}.items():
summary.value.add(tag='{}/{}'.format(mode, name),
simple_value=value)
summary_writer.add_summary(summary, step)
summary_writer.flush()
bleu = bleus[0]
if mode == 'val':
if bleu > best_ever_val_bleu:
best_ever_val_bleu = bleu
print('updated best val bleu: {}'.format(bleu))
_save_to(ckpt_best, step)
print('end _eval_epoch')
return bleu
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(tf.tables_initializer())
if restore_from:
_restore_from_path(restore_from)
else:
_restore_from(dir_model)
summary_writer = tf.summary.FileWriter(
dir_summary, sess.graph, flush_secs=30)
epoch = 0
while epoch < config_train.max_epochs:
name = 'align' if FLAGS.align else 'joint'
train_op = train_ops[name]
summary_op = summary_ops[name]
val_bleu = _eval_epoch(sess, summary_writer, 'val')
# test_bleu = _eval_epoch(sess, summary_writer, 'test')
step = tf.train.global_step(sess, global_step)
print('epoch: {} ({}), step: {}, '
'val BLEU: {:.2f}'.format(
epoch, name, step, val_bleu))
_train_epoch(sess, summary_writer, 'train', train_op, summary_op)
epoch += 1
step = tf.train.global_step(sess, global_step)
_save_to(ckpt_model, step)
test_bleu = _eval_epoch(sess, summary_writer, 'test')
print('epoch: {}, test BLEU: {}'.format(epoch, test_bleu))
if __name__ == '__main__':
main()