-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
402 lines (342 loc) · 21.4 KB
/
utils.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
# coding: UTF-8
import math
import numpy as np
import pandas as pd
import torch
from torch.utils import data
from tqdm import tqdm
import time
from datetime import timedelta
import torch.nn.functional as F
def fixed_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def build_zh_dataset(config):
def load_dataset(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
# 去掉(打一物)这样的提示
riddle = riddle.split(' ')[0]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
train_text, train_label = load_dataset(config.train_path, config.pad_size)
dev_text, dev_label = load_dataset(config.dev_path, config.pad_size)
test_text, test_label = load_dataset(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label, test_text, test_label
def build_en_dataset(config):
def load_BiRdQA_dataset_bert(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
# choice_text = choice0 + '[SEP]' + choice1 + '[SEP]' + choice2 + '[SEP]' + choice3 + '[SEP]' + choice4
# token = config.tokenizer.encode_plus(text=riddle, text_pair=choice_text, add_special_tokens=True,
# max_length=pad_size, padding='max_length', truncation=True,
# return_attention_mask=True, return_tensors='pt')
# group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
def load_BiRdQA_dataset_roberta(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
def load_riddlesense_dataset(path, pad_size=256, ):
text, target, corr = [], [], []
dict = {"A": "0", "B": "1", "C": "2", "D": "3", "E": "4", "hidden": "5"}
data = pd.read_json(path, lines=True)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line.question['stem'], \
line.question['choices'][0]['text'], \
line.question['choices'][1]['text'], \
line.question['choices'][2]['text'], \
line.question['choices'][3]['text'], \
line.question['choices'][4]['text'], \
line.answerKey
for key, value in dict.items():
label = label.replace(key, value)
label = int(label)
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
if config.dataset == 'BiRdQA':
if config.model_name != 'roberta-large':
train_text, train_label = load_BiRdQA_dataset_bert(config.train_path, config.pad_size)
dev_text, dev_label = load_BiRdQA_dataset_bert(config.dev_path, config.pad_size)
test_text, test_label = load_BiRdQA_dataset_bert(config.test_path, config.pad_size)
else:
train_text, train_label = load_BiRdQA_dataset_roberta(config.train_path, config.pad_size)
dev_text, dev_label = load_BiRdQA_dataset_roberta(config.dev_path, config.pad_size)
test_text, test_label = load_BiRdQA_dataset_roberta(config.test_path, config.pad_size)
else:
train_text, train_label = load_riddlesense_dataset(config.train_path, config.pad_size)
dev_text, dev_label = load_riddlesense_dataset(config.dev_path, config.pad_size)
test_text, test_label = load_riddlesense_dataset(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label, test_text, test_label
def build_t5_dataset(config):
def load_dataset(path, pad_size=150, ):
ques, ans = [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
text = riddle + ' \n' + ' (A) ' + choice0 + ' (B) ' + choice1 + ' (C) ' + \
choice2 + ' (D) ' + choice3 + ' (E) ' + choice4
encoding = config.tokenizer(text, max_length=pad_size, padding="max_length",
truncation=True, return_tensors="pt")
input_ids, attention_mask = encoding.input_ids, encoding.attention_mask
target_encoding = config.tokenizer(choice[label], max_length=9, padding="max_length", return_tensors="pt")
decoder_attention_mask, label_ids = target_encoding.attention_mask, target_encoding.input_ids
# print(choice[label])
# print(label_ids[0][0])
# x = config.model.generate(input_ids)
# print(x[0][1])
# x = config.tokenizer.decode(x[0], skip_special_tokens=True)
# print(x)
# exit()
label_ids[label_ids == config.tokenizer.pad_token_id] = -100
ques.append([input_ids.tolist(), attention_mask.tolist()])
ans.append([decoder_attention_mask.tolist(), label_ids.tolist()])
ques = torch.tensor(ques)
ans = torch.tensor(ans)
ques = torch.squeeze(ques)
ans = torch.squeeze(ans)
return ques, ans
train_text, train_label = load_dataset(config.train_path, config.pad_size)
dev_text, dev_label = load_dataset(config.dev_path, config.pad_size)
test_text, test_label = load_dataset(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label, test_text, test_label
def build_rd_dataset(config):
data = pd.read_json(config, lines=True)
for index, line in tqdm(data.iterrows()):
print(line.id)
print(line.question['stem'])
print(line.question['choices'][0]['label'], line.question['choices'][0]['text'])
print(line.question['choices'][1]['label'], line.question['choices'][1]['text'])
print(line.question['choices'][2]['label'], line.question['choices'][2]['text'])
print(line.question['choices'][3]['label'], line.question['choices'][3]['text'])
print(line.question['choices'][4]['label'], line.question['choices'][4]['text'])
print(line.answerKey)
exit()
def build_iterator(data_arrays, batch_size, is_train=False):
"""Construct a PyTorch data iterator."""
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset, batch_size, num_workers=4, shuffle=is_train)
def get_time_dif(start_time):
"""获取已使用时间"""
end_time = time.time()
time_dif = end_time - start_time
return timedelta(seconds=int(round(time_dif)))
def attention(query, key, value, mask=None, dropout=None):
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
p_attn = F.softmax(scores, dim=-1)
if dropout is not None:
p_attn = dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
def build_my_en_dataset(config):
def load_BiRdQA_dataset_bert_attention(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
q_token = config.tokenizer.encode_plus(text=riddle, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([q_token.input_ids.tolist(), q_token.token_type_ids.tolist(), q_token.attention_mask.tolist()])
for i in choice:
token = config.tokenizer.encode_plus(text=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
def load_BiRdQA_dataset_bert_56(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
choice_text = choice0 + '[SEP]' + choice1 + '[SEP]' + choice2 + '[SEP]' + choice3 + '[SEP]' + choice4
token = config.tokenizer.encode_plus(text=riddle, text_pair=choice_text, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
train_text, train_label = load_BiRdQA_dataset_bert_56(config.train_path, config.pad_size)
dev_text, dev_label = load_BiRdQA_dataset_bert_56(config.dev_path, config.pad_size)
test_text, test_label = load_BiRdQA_dataset_bert_56(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label, test_text, test_label
def build_my_zh_dataset(config):
def load_dataset(path, pad_size=256, ):
text, target, corr = [], [], []
data = pd.read_csv(path)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line['riddle'], line['choice0'], \
line['choice1'], line['choice2'], \
line['choice3'], line['choice4'], \
line['label']
choice = [choice0, choice1, choice2, choice3, choice4]
# 去掉(打一物)这样的提示
riddle = riddle.split(' ')[0]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
choice_text = choice0 + '[SEP]' + choice1 + '[SEP]' + choice2 + '[SEP]' + choice3 + '[SEP]' + choice4
token = config.tokenizer.encode_plus(text=riddle, text_pair=choice_text, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
train_text, train_label = load_dataset(config.train_path, config.pad_size)
dev_text, dev_label = load_dataset(config.dev_path, config.pad_size)
test_text, test_label = load_dataset(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label, test_text, test_label
def build_CSQA_dataset(config):
def load_dataset(path, pad_size=256, ):
text, target, corr = [], [], []
dict = {"A": "0", "B": "1", "C": "2", "D": "3", "E": "4", "hidden": "5"}
data = pd.read_json(path, lines=True)
for index, line in tqdm(data.iterrows()):
riddle, choice0, choice1, choice2, choice3, choice4, label = line.question['stem'], \
line.question['choices'][0]['text'], \
line.question['choices'][1]['text'], \
line.question['choices'][2]['text'], \
line.question['choices'][3]['text'], \
line.question['choices'][4]['text'], \
line.answerKey
for key, value in dict.items():
label = label.replace(key, value)
label = int(label)
choice = [choice0, choice1, choice2, choice3, choice4]
group = []
for i in choice:
token = config.tokenizer.encode_plus(text=riddle, text_pair=i, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
choice_text = choice0 + '[SEP]' + choice1 + '[SEP]' + choice2 + '[SEP]' + choice3 + '[SEP]' + choice4
token = config.tokenizer.encode_plus(text=riddle, text_pair=choice_text, add_special_tokens=True,
max_length=pad_size, padding='max_length', truncation=True,
return_attention_mask=True, return_tensors='pt')
group.append([token.input_ids.tolist(), token.token_type_ids.tolist(), token.attention_mask.tolist()])
group = torch.tensor(group)
text.append(group)
target.append(label)
corr = torch.tensor(target)
text = [i.tolist() for i in text]
text = torch.tensor(text)
question = torch.squeeze(text)
return question, corr
train_text, train_label = load_dataset(config.train_path, config.pad_size)
dev_text, dev_label = load_dataset(config.dev_path, config.pad_size)
# test_text, test_label = load_dataset(config.test_path, config.pad_size)
return train_text, train_label, dev_text, dev_label