forked from megagonlabs/ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.py
336 lines (277 loc) · 10.6 KB
/
matcher.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
import torch
import torch.nn as nn
import os
import numpy as np
import random
import json
import jsonlines
import csv
import re
import time
import argparse
import sys
import sklearn
import traceback
from torch.utils import data
from tqdm import tqdm
from apex import amp
from scipy.special import softmax
from ditto_light.ditto import evaluate, DittoModel
from ditto_light.exceptions import ModelNotFoundError
from ditto_light.dataset import DittoDataset
from ditto_light.summarize import Summarizer
from ditto_light.knowledge import *
def set_seed(seed: int):
"""
Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch``
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def to_str(ent1, ent2, summarizer=None, max_len=256, dk_injector=None):
"""Serialize a pair of data entries
Args:
ent1 (Dictionary): the 1st data entry
ent2 (Dictionary): the 2nd data entry
summarizer (Summarizer, optional): the summarization module
max_len (int, optional): the max sequence length
dk_injector (DKInjector, optional): the domain-knowledge injector
Returns:
string: the serialized version
"""
content = ''
for ent in [ent1, ent2]:
if isinstance(ent, str):
content += ent
else:
for attr in ent.keys():
content += 'COL %s VAL %s ' % (attr, ent[attr])
content += '\t'
content += '0'
if summarizer is not None:
content = summarizer.transform(content, max_len=max_len)
new_ent1, new_ent2, _ = content.split('\t')
if dk_injector is not None:
new_ent1 = dk_injector.transform(new_ent1)
new_ent2 = dk_injector.transform(new_ent2)
return new_ent1 + '\t' + new_ent2 + '\t0'
def classify(sentence_pairs, model,
lm='distilbert',
max_len=256,
threshold=None):
"""Apply the MRPC model.
Args:
sentence_pairs (list of str): the sequence pairs
model (MultiTaskNet): the model in pytorch
max_len (int, optional): the max sequence length
threshold (float, optional): the threshold of the 0's class
Returns:
list of float: the scores of the pairs
"""
inputs = sentence_pairs
# print('max_len =', max_len)
dataset = DittoDataset(inputs,
max_len=max_len,
lm=lm)
# print(dataset[0])
iterator = data.DataLoader(dataset=dataset,
batch_size=len(dataset),
shuffle=False,
num_workers=0,
collate_fn=DittoDataset.pad)
# prediction
all_probs = []
all_logits = []
with torch.no_grad():
# print('Classification')
for i, batch in enumerate(iterator):
x, _ = batch
logits = model(x)
probs = logits.softmax(dim=1)[:, 1]
all_probs += probs.cpu().numpy().tolist()
all_logits += logits.cpu().numpy().tolist()
if threshold is None:
threshold = 0.5
pred = [1 if p > threshold else 0 for p in all_probs]
return pred, all_logits
def predict(input_path, output_path, config,
model,
batch_size=1024,
summarizer=None,
lm='distilbert',
max_len=256,
dk_injector=None,
threshold=None):
"""Run the model over the input file containing the candidate entry pairs
Args:
input_path (str): the input file path
output_path (str): the output file path
config (Dictionary): task configuration
model (DittoModel): the model for prediction
batch_size (int): the batch size
summarizer (Summarizer, optional): the summarization module
max_len (int, optional): the max sequence length
dk_injector (DKInjector, optional): the domain-knowledge injector
threshold (float, optional): the threshold of the 0's class
Returns:
None
"""
pairs = []
def process_batch(rows, pairs, writer):
predictions, logits = classify(pairs, model, lm=lm,
max_len=max_len,
threshold=threshold)
# try:
# predictions, logits = classify(pairs, model, lm=lm,
# max_len=max_len,
# threshold=threshold)
# except:
# # ignore the whole batch
# return
scores = softmax(logits, axis=1)
for row, pred, score in zip(rows, predictions, scores):
output = {'left': row[0], 'right': row[1],
'match': pred,
'match_confidence': score[int(pred)]}
writer.write(output)
# input_path can also be train/valid/test.txt
# convert to jsonlines
if '.txt' in input_path:
with jsonlines.open(input_path + '.jsonl', mode='w') as writer:
for line in open(input_path):
writer.write(line.split('\t')[:2])
input_path += '.jsonl'
# batch processing
start_time = time.time()
with jsonlines.open(input_path) as reader,\
jsonlines.open(output_path, mode='w') as writer:
pairs = []
rows = []
for idx, row in tqdm(enumerate(reader)):
pairs.append(to_str(row[0], row[1], summarizer, max_len, dk_injector))
rows.append(row)
if len(pairs) == batch_size:
process_batch(rows, pairs, writer)
pairs.clear()
rows.clear()
if len(pairs) > 0:
process_batch(rows, pairs, writer)
run_time = time.time() - start_time
run_tag = '%s_lm=%s_dk=%s_su=%s' % (config['name'], lm, str(dk_injector != None), str(summarizer != None))
os.system('echo %s %f >> log.txt' % (run_tag, run_time))
def tune_threshold(config, model, hp):
"""Tune the prediction threshold for a given model on a validation set"""
validset = config['validset']
task = hp.task
# summarize the sequences up to the max sequence length
set_seed(123)
summarizer = injector = None
if hp.summarize:
summarizer = Summarizer(config, lm=hp.lm)
validset = summarizer.transform_file(validset, max_len=hp.max_len, overwrite=True)
if hp.dk is not None:
if hp.dk == 'product':
injector = ProductDKInjector(config, hp.dk)
else:
injector = GeneralDKInjector(config, hp.dk)
validset = injector.transform_file(validset)
# load dev sets
valid_dataset = DittoDataset(validset,
max_len=hp.max_len,
lm=hp.lm)
# print(valid_dataset[0])
valid_iter = data.DataLoader(dataset=valid_dataset,
batch_size=64,
shuffle=False,
num_workers=0,
collate_fn=DittoDataset.pad)
# acc, prec, recall, f1, v_loss, th = eval_classifier(model, valid_iter,
# get_threshold=True)
f1, th = evaluate(model, valid_iter, threshold=None)
# verify F1
set_seed(123)
predict(validset, "tmp.jsonl", config, model,
summarizer=summarizer,
max_len=hp.max_len,
lm=hp.lm,
dk_injector=injector,
threshold=th)
predicts = []
with jsonlines.open("tmp.jsonl", mode="r") as reader:
for line in reader:
predicts.append(int(line['match']))
os.system("rm tmp.jsonl")
labels = []
with open(validset) as fin:
for line in fin:
labels.append(int(line.split('\t')[-1]))
real_f1 = sklearn.metrics.f1_score(labels, predicts)
print("load_f1 =", f1)
print("real_f1 =", real_f1)
return th
def load_model(task, path, lm, use_gpu, fp16=True):
"""Load a model for a specific task.
Args:
task (str): the task name
path (str): the path of the checkpoint directory
lm (str): the language model
use_gpu (boolean): whether to use gpu
fp16 (boolean, optional): whether to use fp16
Returns:
Dictionary: the task config
MultiTaskNet: the model
"""
# load models
checkpoint = os.path.join(path, task, 'model.pt')
if not os.path.exists(checkpoint):
raise ModelNotFoundError(checkpoint)
configs = json.load(open('configs.json'))
configs = {conf['name'] : conf for conf in configs}
config = configs[task]
config_list = [config]
if use_gpu:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
else:
device = 'cpu'
model = DittoModel(device=device, lm=lm)
saved_state = torch.load(checkpoint, map_location=lambda storage, loc: storage)
model.load_state_dict(saved_state['model'])
model = model.to(device)
if fp16 and 'cuda' in device:
model = amp.initialize(model, opt_level='O2')
return config, model
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default='Structured/Beer')
parser.add_argument("--input_path", type=str, default='input/candidates_small.jsonl')
parser.add_argument("--output_path", type=str, default='output/matched_small.jsonl')
parser.add_argument("--lm", type=str, default='distilbert')
parser.add_argument("--use_gpu", dest="use_gpu", action="store_true")
parser.add_argument("--fp16", dest="fp16", action="store_true")
parser.add_argument("--checkpoint_path", type=str, default='checkpoints/')
parser.add_argument("--dk", type=str, default=None)
parser.add_argument("--summarize", dest="summarize", action="store_true")
parser.add_argument("--max_len", type=int, default=256)
hp = parser.parse_args()
# load the models
set_seed(123)
config, model = load_model(hp.task, hp.checkpoint_path,
hp.lm, hp.use_gpu, hp.fp16)
summarizer = dk_injector = None
if hp.summarize:
summarizer = Summarizer(config, hp.lm)
if hp.dk is not None:
if 'product' in hp.dk:
dk_injector = ProductDKInjector(config, hp.dk)
else:
dk_injector = GeneralDKInjector(config, hp.dk)
# tune threshold
threshold = tune_threshold(config, model, hp)
# run prediction
predict(hp.input_path, hp.output_path, config, model,
summarizer=summarizer,
max_len=hp.max_len,
lm=hp.lm,
dk_injector=dk_injector,
threshold=threshold)