-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathraml_main.py
343 lines (276 loc) · 12.7 KB
/
raml_main.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
# Copyright 2018 The Texar Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Attentional Seq2seq with RAML algorithm.
Read a pre-processed file containing the augmented samples and
corresponding rewards for every target sentence.
RAML Algorithm is described in https://arxiv.org/pdf/1705.07136.pdf
"""
from io import open
import importlib
import tensorflow as tf
import texar.tf as tx
import numpy as np
import random
from rouge import Rouge
flags = tf.flags
flags.DEFINE_string("config_model", "configs.config_model", "The model config.")
flags.DEFINE_string("config_data", "configs.config_iwslt14",
"The dataset config.")
flags.DEFINE_string('raml_file', 'data/iwslt14/samples_iwslt14.txt',
'the samples and rewards described in RAML')
flags.DEFINE_integer('n_samples', 10,
'number of samples for every target sentence')
flags.DEFINE_float('tau', 0.4, 'the temperature in RAML algorithm')
flags.DEFINE_string('output_dir', '.', 'where to keep training logs')
FLAGS = flags.FLAGS
config_model = importlib.import_module(FLAGS.config_model)
config_data = importlib.import_module(FLAGS.config_data)
if not FLAGS.output_dir.endswith('/'):
FLAGS.output_dir += '/'
log_dir = FLAGS.output_dir + 'training_log_raml' +\
'_' + str(FLAGS.n_samples) + 'samples' +\
'_tau' + str(FLAGS.tau) + '/'
tx.utils.maybe_create_dir(log_dir)
def read_raml_sample_file():
raml_file = open(FLAGS.raml_file, encoding='utf-8')
train_data = []
sample_num = -1
for line in raml_file.readlines():
line = line[:-1]
if line.startswith('***'):
continue
elif line.endswith('samples'):
sample_num = eval(line.split()[0])
assert sample_num == 1 or sample_num == FLAGS.n_samples
elif line.startswith('source:'):
train_data.append({'source': line[7:], 'targets': []})
else:
train_data[-1]['targets'].append(line.split('|||'))
if sample_num == 1:
for i in range(FLAGS.n_samples - 1):
train_data[-1]['targets'].append(line.split('|||'))
return train_data
def raml_loss(batch, output, training_rewards):
mle_loss = tx.losses.sequence_sparse_softmax_cross_entropy(
labels=batch['target_text_ids'][:, 1:],
logits=output.logits,
sequence_length=batch['target_length'] - 1,
average_across_batch=False)
return tf.reduce_sum(mle_loss * training_rewards) /\
tf.reduce_sum(training_rewards)
def build_model(batch, train_data, rewards):
"""
Assembles the seq2seq model.
Code in this function is basically the same of build_model() in
baseline_seq2seq_attn_main.py except the normalization in loss_fn.
"""
source_embedder = tx.modules.WordEmbedder(
vocab_size=train_data.source_vocab.size, hparams=config_model.embedder)
encoder = tx.modules.BidirectionalRNNEncoder(
hparams=config_model.encoder)
enc_outputs, _ = encoder(source_embedder(batch['source_text_ids']))
target_embedder = tx.modules.WordEmbedder(
vocab_size=train_data.target_vocab.size, hparams=config_model.embedder)
decoder = tx.modules.AttentionRNNDecoder(
memory=tf.concat(enc_outputs, axis=2),
memory_sequence_length=batch['source_length'],
vocab_size=train_data.target_vocab.size,
hparams=config_model.decoder)
training_outputs, _, _ = decoder(
decoding_strategy='train_greedy',
inputs=target_embedder(batch['target_text_ids'][:, :-1]),
sequence_length=batch['target_length'] - 1)
train_op = tx.core.get_train_op(
raml_loss(batch, training_outputs, rewards),
hparams=config_model.opt)
start_tokens = tf.ones_like(batch['target_length']) *\
train_data.target_vocab.bos_token_id
beam_search_outputs, _, _ = \
tx.modules.beam_search_decode(
decoder_or_cell=decoder,
embedding=target_embedder,
start_tokens=start_tokens,
end_token=train_data.target_vocab.eos_token_id,
beam_width=config_model.beam_width,
max_decoding_length=60)
return train_op, beam_search_outputs
def print_stdout_and_file(content, file):
print(content)
print(content, file=file)
def main():
"""Entrypoint.
"""
config_data.train['batch_size'] *= FLAGS.n_samples
config_data.val['batch_size'] *= FLAGS.n_samples
config_data.test['batch_size'] *= FLAGS.n_samples
train_data = tx.data.PairedTextData(hparams=config_data.train)
val_data = tx.data.PairedTextData(hparams=config_data.val)
test_data = tx.data.PairedTextData(hparams=config_data.test)
data_iterator = tx.data.TrainTestDataIterator(
train=train_data, val=val_data, test=test_data)
batch = data_iterator.get_next()
rewards_ts = tf.placeholder(
dtype=tf.float32, shape=[None, ], name='training_rewards')
train_op, infer_outputs = build_model(batch, train_data, rewards_ts)
raml_train_data = read_raml_sample_file()
def _train_epoch(sess, epoch_no):
data_iterator.switch_to_train_data(sess)
training_log_file = \
open(log_dir + 'training_log' + str(epoch_no) + '.txt', 'w',
encoding='utf-8')
step = 0
source_buffer, target_buffer = [], []
random.shuffle(raml_train_data)
for training_pair in raml_train_data:
for target in training_pair['targets']:
source_buffer.append(training_pair['source'])
target_buffer.append(target)
if len(target_buffer) != train_data.batch_size:
continue
source_ids = []
source_length = []
target_ids = []
target_length = []
scores = []
trunc_len_src = train_data.hparams.source_dataset.max_seq_length
trunc_len_tgt = train_data.hparams.target_dataset.max_seq_length
for sentence in source_buffer:
ids = [train_data.source_vocab.token_to_id_map_py[token]
for token in sentence.split()][:trunc_len_src]
ids = ids + [train_data.source_vocab.eos_token_id]
source_ids.append(ids)
source_length.append(len(ids))
for sentence, score_str in target_buffer:
ids = [train_data.target_vocab.bos_token_id]
ids = ids + [train_data.target_vocab.token_to_id_map_py[token]
for token in sentence.split()][:trunc_len_tgt]
ids = ids + [train_data.target_vocab.eos_token_id]
target_ids.append(ids)
scores.append(eval(score_str))
target_length.append(len(ids))
rewards = []
for i in range(0, train_data.batch_size, FLAGS.n_samples):
tmp = np.array(scores[i:i + FLAGS.n_samples])
tmp = np.exp(tmp / FLAGS.tau) / np.sum(np.exp(tmp / FLAGS.tau))
for j in range(0, FLAGS.n_samples):
rewards.append(tmp[j])
for value in source_ids:
while len(value) < max(source_length):
value.append(0)
for value in target_ids:
while len(value) < max(target_length):
value.append(0)
feed_dict = {
batch['source_text_ids']: np.array(source_ids),
batch['target_text_ids']: np.array(target_ids),
batch['source_length']: np.array(source_length),
batch['target_length']: np.array(target_length),
rewards_ts: np.array(rewards)
}
source_buffer = []
target_buffer = []
loss = sess.run(train_op, feed_dict=feed_dict)
print("step={}, loss={:.4f}".format(step, loss),
file=training_log_file)
if step % config_data.observe_steps == 0:
print("step={}, loss={:.4f}".format(step, loss))
training_log_file.flush()
step += 1
# code below this line is exactly the same as baseline_seq2seq_attn_main.py
def _eval_epoch(sess, mode, epoch_no):
if mode == 'val':
data_iterator.switch_to_val_data(sess)
else:
data_iterator.switch_to_test_data(sess)
refs, hypos = [], []
while True:
try:
fetches = [
batch['target_text'][:, 1:],
infer_outputs.predicted_ids[:, :, 0]
]
feed_dict = {
tx.global_mode(): tf.estimator.ModeKeys.EVAL
}
target_texts_ori, output_ids = \
sess.run(fetches, feed_dict=feed_dict)
target_texts = tx.utils.strip_special_tokens(
target_texts_ori.tolist(), is_token_list=True)
target_texts = tx.utils.str_join(target_texts)
output_texts = tx.utils.map_ids_to_strs(
ids=output_ids, vocab=val_data.target_vocab)
tx.utils.write_paired_text(
target_texts, output_texts,
log_dir + mode + '_results' + str(epoch_no) + '.txt',
append=True, mode='h', sep=' ||| ')
for hypo, ref in zip(output_texts, target_texts):
if config_data.eval_metric == 'bleu':
hypos.append(hypo)
refs.append([ref])
elif config_data.eval_metric == 'rouge':
hypos.append(tx.utils.compat_as_text(hypo))
refs.append(tx.utils.compat_as_text(ref))
except tf.errors.OutOfRangeError:
break
if config_data.eval_metric == 'bleu':
return tx.evals.corpus_bleu_moses(
list_of_references=refs, hypotheses=hypos)
elif config_data.eval_metric == 'rouge':
rouge = Rouge()
return rouge.get_scores(hyps=hypos, refs=refs, avg=True)
def _calc_reward(score):
"""
Return the bleu score or the sum of (Rouge-1, Rouge-2, Rouge-L).
"""
if config_data.eval_metric == 'bleu':
return score
elif config_data.eval_metric == 'rouge':
return sum([value['f'] for key, value in score.items()])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(tf.tables_initializer())
best_val_score = -1.
scores_file = open(log_dir + 'scores.txt', 'w', encoding='utf-8')
for i in range(config_data.num_epochs):
_train_epoch(sess, i)
val_score = _eval_epoch(sess, 'val', i)
test_score = _eval_epoch(sess, 'test', i)
best_val_score = max(best_val_score, _calc_reward(val_score))
if config_data.eval_metric == 'bleu':
print_stdout_and_file(
'val epoch={}, BLEU={:.4f}; best-ever={:.4f}'.format(
i, val_score, best_val_score), file=scores_file)
print_stdout_and_file(
'test epoch={}, BLEU={:.4f}'.format(i, test_score),
file=scores_file)
print_stdout_and_file('=' * 50, file=scores_file)
elif config_data.eval_metric == 'rouge':
print_stdout_and_file(
'valid epoch {}:'.format(i), file=scores_file)
for key, value in val_score.items():
print_stdout_and_file(
'{}: {}'.format(key, value), file=scores_file)
print_stdout_and_file('fsum: {}; best_val_fsum: {}'.format(
_calc_reward(val_score), best_val_score), file=scores_file)
print_stdout_and_file(
'test epoch {}:'.format(i), file=scores_file)
for key, value in test_score.items():
print_stdout_and_file(
'{}: {}'.format(key, value), file=scores_file)
print_stdout_and_file('=' * 110, file=scores_file)
scores_file.flush()
if __name__ == '__main__':
main()