-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-csqa.py
61 lines (42 loc) · 1.43 KB
/
main-csqa.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
import numpy as np
import torch
from models import CSQAModel, CSQAInference
seed=514
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
def read(fname, ftype):
with open(fname) as fp:
lines = fp.read().strip().split('\n')
items = []
for line in lines:
evals = eval(line)
quest = evals['question']['stem']
choices = [f"{choice['text']}." for choice in evals['question']['choices']]
quests = [quest for choice in choices]
if ftype != 'pred':
label = ['A', 'B', 'C', 'D', 'E'].index(evals['answerKey'])
items.append({
'quests': quests,
'choices': choices,
'label': label,
})
else:
items.append({
'quests': quests,
'choices': choices,
})
return items
items_train = read('CSQA/train_rand_split.jsonl', 'train')
items_eval = read('CSQA/dev_rand_split.jsonl', 'eval')
items_pred = read('CSQA/test_rand_split.jsonl', 'pred')
inference = CSQAInference('microsoft/deberta-v3-large', 5)
n_epoch = 4
best = 0.0
for epoch in range(n_epoch):
inference.train(items_train)
score = inference.evaluate(items_eval)
if score > best:
torch.save(inference.state_dict(), f'./csqa_debertav3.pth')
best = score