-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcq_estimation.py
157 lines (134 loc) · 5.41 KB
/
mcq_estimation.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
import transformers
import torch
from transformers import pipeline
import torch.nn.functional as F
import json
import time
import numpy as np
import argparse
import random
import os
import datetime
from estimators import Estimator, DenseRetrieval
from metric import accuracy, precision, recall, f1_score
from data_loader import load_data
import tqdm
parser = argparse.ArgumentParser()
parser.add_argument('--cuda_id', type=int, default=0 )
parser.add_argument('--seed', type=int, default=0 )
parser.add_argument('--dataset', type=str, default='commonsense_qa', help='dataset name' )
parser.add_argument('--model_name', type=str, default='gpt2', help='pre-trained language model' )
parser.add_argument('--batch_size', type=int, default=1 )
parser.add_argument('--chat_template', type=int, default=0 )
parser.add_argument('--topk', type=int, default=1 )
args = parser.parse_args()
def manual_seed(seed: int):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
manual_seed(args.seed)
device = torch.device(f"cuda:{args.cuda_id}")
def main(args):
print(args)
prompt_lis, candidate_pool_lis, answer_lis, n_candidate_lis, batch_sizes = load_data(args)
print('loaded data')
true = []
pred_first = []
pred_last = []
pred_avg = []
pred_sample_avg = []
pred_sum = []
total_rt = 0
with torch.no_grad():
t1 = time.time()
for i in tqdm.tqdm(range(len(batch_sizes))):
i1 = int(np.sum(batch_sizes[:i]))
i2 = int(np.sum(batch_sizes[:i+1]))
choice_first, choice_last, choice_avg, choice_sample_avg, choice_sum = estimator(prompt_lis[i1 :i2], candidate_pool_lis[i1 :i2] )
true.append(torch.tensor(answer_lis[i1 :i2]))
pred_first.append(choice_first.detach().cpu())
pred_last.append(choice_last.detach().cpu())
pred_avg.append(choice_avg.detach().cpu())
pred_sample_avg.append(choice_sample_avg.detach().cpu())
pred_sum.append(choice_sum.detach().cpu())
t2 = time.time()
total_rt += t2 - t1
true = torch.cat(true)
pred_first = torch.cat(pred_first)
pred_last = torch.cat(pred_last)
pred_avg = torch.cat(pred_avg)
pred_sample_avg = torch.cat(pred_sample_avg)
pred_sum = torch.cat(pred_sum)
today =str(datetime.datetime.now()).split(' ')[0]
if not os.path.exists(f"results/{today}/{args.dataset}"):
os.makedirs(f"results/{today}/{args.dataset}")
if '/' in args.model_name:
model_name = args.model_name.split('/')[-1]
else:
model_name = args.model_name
method_lis = ['first', 'last', 'avg', 'sample_avg', 'sum']
pred_lis = [pred_first, pred_last, pred_avg, pred_sample_avg, pred_sum]
for i in range(len(method_lis)):
pred = pred_lis[i]
method = method_lis[i]
res = np.array([accuracy(true, pred),
precision(true, pred),
recall(true, pred),
f1_score(true, pred),
total_rt,
len(prompt_lis) ])
print(res)
np.savetxt(f"./results/{today}/{args.dataset}_{model_name}_{method}_seed{args.seed}.txt",
res.reshape(1,-1),
header='accuracy,precision,recall,f1,rt,n_sample',
delimiter=',',
comments='')
np.save(f"./results/{today}/{args.dataset}/{model_name}_{method}_true.npy", true.cpu().numpy())
np.save(f"./results/{today}/{args.dataset}/{model_name}_{method}_pred.npy", pred.cpu().numpy())
def main_dpr(args):
print(args)
prompt_lis, candidate_pool_lis, answer_lis, n_candidate_lis, batch_sizes = load_data(args)
print('loaded data')
true = []
pred = []
total_rt = 0
with torch.no_grad():
t1 = time.time()
for i in tqdm.tqdm(range(len(batch_sizes))):
i1 = int(np.sum(batch_sizes[:i]))
i2 = int(np.sum(batch_sizes[:i+1]))
choice = estimator(prompt_lis[i1 :i2], candidate_pool_lis[i1 :i2] )
true.append(torch.tensor(answer_lis[i1 :i2]))
pred.append(choice.detach().cpu())
t2 = time.time()
total_rt += t2 - t1
true = torch.cat(true)
pred = torch.cat(pred)
method = 'dpr'
today =str(datetime.datetime.now()).split(' ')[0]
if not os.path.exists(f"results/{today}/{args.dataset}"):
os.makedirs(f"results/{today}/{args.dataset}")
if '/' in args.model_name:
model_name = args.model_name.split('/')[-1]
else:
model_name = args.model_name
res = np.array([accuracy(true, pred),
precision(true, pred),
recall(true, pred),
f1_score(true, pred),
total_rt,
len(prompt_lis) ])
print(res)
np.savetxt(f"./results/{today}/{args.dataset}_{model_name}_{method}_seed{args.seed}.txt",
res.reshape(1,-1),
header='accuracy,precision,recall,f1,rt,n_sample',
delimiter=',',
comments='')
np.save(f"./results/{today}/{args.dataset}/{model_name}_{method}_true.npy", true.cpu().numpy())
np.save(f"./results/{today}/{args.dataset}/{model_name}_{method}_pred.npy", pred.cpu().numpy())
if args.model_name == 'dpr':
estimator = DenseRetrieval(args, device)
main_dpr(args)
else:
estimator = Estimator(args, device)
main(args)