-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_grid.py
345 lines (288 loc) · 11.8 KB
/
run_grid.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
#!/usr/bin/env python3 -u
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Translate pre-processed data with a trained model.
"""
from typing import Mapping, Iterable
from itertools import product
import torch
from fairseq import bleu, checkpoint_utils, options, progress_bar, tasks, utils
from fairseq.meters import StopwatchMeter, TimeMeter
import numpy as np
import copy
from rouge import rouge_n_sentence_level, rouge_l_summary_level
import re
import files2rouge as f2r
import tempfile
import os
import pyrouge
import logging
parameters = {'beam': [4], 'no_repeat_ngram_size': [3], 'lenpen': [0.5, 1.0, 2], 'min_len': [45, 50, 55], 'max_len_b': [125, 150, 200]}
def run_p2r(summ_path,
ref_path,
rouge_args=None,
verbose=False,
saveto=None,
eos=".",
ignore_empty=False,
stemming=False):
s = f2r.settings.Settings()
s._load()
dirpath = tempfile.mkdtemp()
sys_root, model_root = [os.path.join(dirpath, _)
for _ in ["system", "model"]]
print("Preparing documents...", end=" ")
f2r.utils.mkdirs([sys_root, model_root])
ignored = f2r.utils.split_files(model_file=ref_path,
system_file=summ_path,
model_dir=model_root,
system_dir=sys_root,
eos=eos,
ignore_empty=ignore_empty)
log_level = logging.ERROR if not verbose else None
r = pyrouge.Rouge155(rouge_dir=os.path.dirname(s.data['ROUGE_path']), log_level=log_level)
r.system_dir = sys_root
r.model_dir = model_root
r.system_filename_pattern = r's.(\d+).txt'
r.model_filename_pattern = 'm.[A-Z].#ID#.txt'
data_arg = "-e %s" % s.data['ROUGE_data']
if not rouge_args:
rouge_args = [
'-c', 95,
'-r', 1000,
'-n', 2,
'-a']
if stemming:
rouge_args.append("-m")
rouge_args_str = " ".join([str(_) for _ in rouge_args])
else:
rouge_args_str = rouge_args
rouge_args_str = "%s %s" % (data_arg, rouge_args_str)
output = r.convert_and_evaluate(rouge_args=rouge_args_str)
return output
def parse_rouge(text):
r1 = float(text.partition('ROUGE-1 Average_F: ')[2][:7])
r2 = float(text.partition('ROUGE-2 Average_F: ')[2][:7])
r3 = float(text.partition('ROUGE-L Average_F: ')[2][:7])
return {
'ROUGE-1-F (avg)': r1,
'ROUGE-2-F (avg)': r2,
'ROUGE-L-F (avg)': r3,
}
0.44341
class TrueRougeScorer:
def score(self, pairs):
with tempfile.NamedTemporaryFile(mode = "a+") as h, tempfile.NamedTemporaryFile(mode = "a+") as t:
for pair in pairs:
target, hypo = pair
print(' '.join(target), file=t, flush=True)
print(' '.join(hypo), file=h, flush=True)
output = run_p2r(t.name, h.name)
return parse_rouge(output)
class RougeScorer:
def score(self, pairs):
rouges_1 = []
rouges_2 = []
rouges_l = []
for pair in pairs:
target, hypo = pair
# Calculate ROUGE-2.
_, _, rouge_1 = rouge_n_sentence_level(hypo, target, 1)
_, _, rouge_2 = rouge_n_sentence_level(hypo, target, 2)
_, _, rouge_l = rouge_l_summary_level(hypo, target)
rouges_1.append(rouge_1)
rouges_2.append(rouge_2)
rouges_l.append(rouge_l)
return {
'ROUGE-1-F (avg)': np.average(rouges_1),
'ROUGE-2-F (avg)': np.average(rouges_2),
'ROUGE-L-F (avg)': np.average(rouges_l),
}
class ParameterGrid:
def __init__(self, param_grid):
if not isinstance(param_grid, (Mapping, Iterable)):
raise TypeError('Parameter grid is not a dict or '
'a list ({!r})'.format(param_grid))
if isinstance(param_grid, Mapping):
# wrap dictionary in a singleton list to support either dict
# or list of dicts
param_grid = [param_grid]
# check if all entries are dictionaries of lists
for grid in param_grid:
if not isinstance(grid, dict):
raise TypeError('Parameter grid is not a '
'dict ({!r})'.format(grid))
for key in grid:
if not isinstance(grid[key], Iterable):
raise TypeError('Parameter grid value is not iterable '
'(key={!r}, value={!r})'
.format(key, grid[key]))
self.param_grid = param_grid
def __iter__(self):
"""Iterate over the points in the grid.
Returns
-------
params : iterator over dict of string to any
Yields dictionaries mapping each estimator parameter to one of its
allowed values.
"""
for p in self.param_grid:
# Always sort the keys of a dictionary, for reproducibility
items = sorted(p.items())
if not items:
yield {}
else:
keys, values = zip(*items)
for v in product(*values):
params = dict(zip(keys, v))
yield params
class GridSearch:
def __init__(self, params, args, task, models, scorer):
self.param_grid = ParameterGrid(params)
self.task = task
self.args = args
self.models = models
self.scorer = scorer
# Load alignment dictionary for unknown word replacement
# (None if no unknown word replacement, empty if no path to align dictionary)
self.align_dict = utils.load_align_dict(self.args.replace_unk)
def make_fit_args(self, params):
fit_args = copy.deepcopy(self.args)
for k, v in params.items():
vars(fit_args)[k] = v
return fit_args
def get_batch_iterator(self):
return self.task.get_batch_iterator(
dataset=self.task.dataset(self.args.gen_subset),
max_tokens=self.args.max_tokens,
max_sentences=self.args.max_sentences,
max_positions=utils.resolve_max_positions(
self.task.max_positions(),
*[model.max_positions() for model in self.models]
),
ignore_invalid_inputs=self.args.skip_invalid_size_inputs_valid_test,
required_batch_size_multiple=self.args.required_batch_size_multiple,
num_shards=self.args.num_shards,
shard_id=self.args.shard_id,
num_workers=self.args.num_workers,
)
def fit(self, n = 8):
scores = []
samples = []
itr = self.get_batch_iterator().next_epoch_itr(shuffle=False)
for _ in range(n):
samples.append(next(itr))
for params in self.param_grid:
print(params)
print('init')
to_score = []
for sample in samples:
to_score.extend(self.gen_candidates(params, sample))
score = self.scorer.score(to_score)
scores.append((params, score))
print(score)
return scores
def gen_candidates(self, params, sample):
use_cuda = torch.cuda.is_available() and not self.args.cpu
sample = utils.move_to_cuda(sample) if use_cuda else sample
if 'net_input' not in sample:
return
src_dict = self.task.source_dictionary
tgt_dict = self.task.target_dictionary
regex = r" ##"
args = self.make_fit_args(params)
generator = self.task.build_generator(args)
prefix_tokens = None
if args.prefix_size > 0:
prefix_tokens = sample['target'][:, :args.prefix_size]
hypos = self.task.inference_step(generator, self.models, sample, prefix_tokens)
num_generated_tokens = sum(len(h[0]['tokens']) for h in hypos)
to_score = []
for i, sample_id in enumerate(sample['id'].tolist()):
# Remove padding
src_tokens = utils.strip_pad(sample['net_input']['src_tokens'][i, :], tgt_dict.pad())
target_tokens = utils.strip_pad(sample['target'][i, :], tgt_dict.pad()).int().cpu()
src_str = src_dict.string(src_tokens, args.remove_bpe)
target_str = tgt_dict.string(target_tokens, args.remove_bpe, escape_unk=True)
# Process top predictions
for j, hypo in enumerate(hypos[i][:args.nbest]):
hypo_tokens, hypo_str, alignment = utils.post_process_prediction(
hypo_tokens=hypo['tokens'].int().cpu(),
src_str=src_str,
alignment=hypo['alignment'],
align_dict=self.align_dict,
tgt_dict=tgt_dict,
remove_bpe=args.remove_bpe,
)
# Score only the top hypothesis
if j == 0:
#if align_dict is not None or args.remove_bpe is not None:
# # Convert back to tokens for evaluation with unk replacement and/or without BPE
# target_tokens = tgt_dict.encode_line(target_str, add_if_not_exist=True)
#if hasattr(scorer, 'add_string'):
# self.scorer.add_string(target_str, hypo_str)
#else:
#self.scorer.add(target_tokens, hypo_tokens)
# print(hypo_str)
# print(target_str)
hypo_str = re.sub(regex, "", hypo_str, 0, re.MULTILINE)
target_str = re.sub(regex, "", target_str, 0, re.MULTILINE)
to_score.append((target_str.split(), hypo_str.split()))
return to_score
def main(args):
assert args.path is not None, '--path required for generation!'
assert not args.sampling or args.nbest == args.beam, \
'--sampling requires --nbest to be equal to --beam'
assert args.replace_unk is None or args.raw_text, \
'--replace-unk requires a raw text dataset (--raw-text)'
utils.import_user_module(args)
if args.max_tokens is None and args.max_sentences is None:
args.max_tokens = 12000
print(args)
use_cuda = torch.cuda.is_available() and not args.cpu
# Load dataset splits
task = tasks.setup_task(args)
task.load_dataset(args.gen_subset)
# Set dictionaries
try:
src_dict = getattr(task, 'source_dictionary', None)
except NotImplementedError:
src_dict = None
tgt_dict = task.target_dictionary
# Load ensemble
print('| loading model(s) from {}'.format(args.path))
models, _model_args = checkpoint_utils.load_model_ensemble(
args.path.split(':'),
arg_overrides=eval(args.model_overrides),
task=task,
)
# Optimize ensemble for generation
for model in models:
model.make_generation_fast_(
need_attn=False,
)
if args.fp16:
model.half()
if use_cuda:
model.cuda()
# Initialize generator
scorer = TrueRougeScorer()
gridsearch = GridSearch(parameters, args, task, models, scorer)
scores = gridsearch.fit()
print('top scores')
asc_scores = sorted(scores, key=lambda x: (x[1]['ROUGE-L-F (avg)'], x[1]['ROUGE-2-F (avg)'], x[1]['ROUGE-1-F (avg)']))
for score in asc_scores:
print(score[0])
print(score[1])
print('------')
def cli_main():
parser = options.get_generation_parser()
args = options.parse_args_and_arch(parser)
main(args)
if __name__ == '__main__':
torch.set_printoptions(profile="short")
torch.set_printoptions(threshold=50)
cli_main()