-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_bert_enn.py
245 lines (184 loc) · 10.5 KB
/
test_bert_enn.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
import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import numpy as np
import torch
from torch.utils.data import DataLoader, SequentialSampler
import pickle
import argparse
from utils import set_seed, cal_entropy, getDisn, get_performance, get_pr_roc
from models import BERT_ENN
import pandas as pd
pd.options.display.float_format = lambda x: '{:.0f}'.format(x) if round(x, 0) == x else '{:.3f}'.format(x)
pd.options.display.max_columns = 20
pd.options.display.width = 300
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--eval_batch_size", default=64, type=int, help="Batch size for training.")
parser.add_argument("--seed", default=0, type=int, help="Number of epochs for training.")
parser.add_argument("--dataset", default='sst', type=str, help="dataset", choices= ['20news','trec','sst'])
parser.add_argument('--path', type=str, default=None)
parser.add_argument('--save_result', type=str, default='n', choices= ['y','n'])
parser.add_argument('--evaluate_benchmark', type=str, default='y')
parser.add_argument('--MAX_LEN', type=int, default=150)
parser.add_argument("--base_rate", default=5, type=int, help="base rate N:1")
parser.add_argument('--recall_level', type=float, default=0.9)
args = parser.parse_args()
print('\n\n-------------------------------------------------\n')
filename = args.path.split('/')[-1]
folder = args.path.split(filename, 1)[0]
print('path:', args.path)
print('folder:', folder)
print('filename:', filename)
args.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
set_seed(args)
if args.dataset == '20news':
num_labels = 20
elif args.dataset == 'sst':
num_labels = 2
elif args.dataset == 'trec':
num_labels = 50
record = vars(args)
print(record)
print('Loading saved dataset checkpoints for testing...')
dataset_dir = 'dataset/test'
val_data = torch.load(dataset_dir + '/{}_val_in_domain.pt'.format(args.dataset))
test_data = torch.load(dataset_dir + '/{}_test_in_domain.pt'.format(args.dataset))
######## saved dataset
test_sampler = SequentialSampler(test_data)
prediction_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=args.eval_batch_size)
val_sampler = SequentialSampler(val_data)
validation_dataloader = DataLoader(val_data, sampler=val_sampler, batch_size=args.eval_batch_size)
model = BERT_ENN(num_labels=num_labels)
load_model = torch.load(args.path)
if type(load_model)== BERT_ENN:
model = load_model
else:
model.load_state_dict(load_model)
if torch.cuda.device_count() > 1:
print('Does not support multiple gpus')
# model = nn.DataParallel(model)
model.to(args.device)
df_test = pd.DataFrame(
columns=['epoch', 'idxs_mask', 'in_ent', 'in_vac', 'in_dis',
'succ_ent', 'fail_ent', 'succ_dis', 'fail_dis', 'succ_vac', 'fail_vac', 'bnd_ent_roc', 'bnd_dis_roc'])
df_test_avg = pd.DataFrame(
columns=['epoch', 'test_acc', 'in_ent', 'in_vac', 'in_dis',
'succ_ent', 'fail_ent', 'succ_dis', 'fail_dis', 'succ_vac', 'fail_vac', 'bnd_ent_auroc', 'bnd_dis_auroc'])
df_ood = pd.DataFrame(
columns=['epoch', 'ood_ent', 'ood_vac', 'ood_dis', 'ood_ent_roc', 'ood_vac_roc'])
df_ood_avg = pd.DataFrame(
columns=['epoch', 'ood_ent', 'ood_vac', 'ood_dis', 'ent_fpr', 'ent_auroc', 'ent_aupr', 'vac_fpr', 'vac_auroc', 'vac_aupr'])
# ##### test model on in-distribution test set
# Put model in evaluation mode
model.eval()
with torch.no_grad():
df_tmp = pd.DataFrame(
columns=['idxs_mask', 'in_ent', 'in_vac', 'in_dis', 'succ_ent', 'fail_ent',
'succ_dis', 'fail_dis', 'succ_vac', 'fail_vac'])
for batch in prediction_dataloader:
# Add batch to GPU
batch = tuple(t.to(args.device) for t in batch)
# Unpack the inputs from our dataloader
b_input_ids, b_input_mask, b_labels = batch
# Telling the model not to compute or store gradients, saving memory and speeding up prediction
alpha = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask)[0]
model.bert(b_input_ids,
attention_mask=b_input_mask,
token_type_ids=None)
p = alpha / alpha.sum(1, keepdim=True)
pred = p.argmax(dim=1, keepdim=True)
idxs_mask = pred.eq(b_labels.view_as(pred)).view(-1)
ent = cal_entropy(p)
disn = getDisn(alpha)
vac_in = (num_labels / torch.sum(alpha, dim=1))
succ_ent = ent[idxs_mask]
fail_ent = ent[~idxs_mask]
succ_dis = disn[idxs_mask]
fail_dis = disn[~idxs_mask]
succ_vac = vac_in[idxs_mask]
fail_vac = vac_in[~idxs_mask]
df_tmp.loc[len(df_tmp)] = [i.tolist() for i in
[idxs_mask, ent, vac_in, disn, succ_ent, fail_ent,
succ_dis, fail_dis, succ_vac, fail_vac]]
in_score = df_tmp.sum()
fpr, tpr, roc_auc = get_pr_roc(in_score['succ_ent'], in_score['fail_ent'])
bnd_dect_ent = {'auroc': round(roc_auc, 4), 'fpr': fpr, 'tpr': tpr}
fpr, tpr, roc_auc = get_pr_roc(in_score['succ_dis'], in_score['fail_dis'])
bnd_dect_dis = {'auroc': round(roc_auc, 4), 'fpr': fpr, 'tpr': tpr}
df_test.loc[len(df_test)] = [0, *in_score, bnd_dect_ent, bnd_dect_dis]
df_test_avg.loc[len(df_test_avg)] = [0, *in_score.apply(np.average), bnd_dect_ent['auroc'],
bnd_dect_dis['auroc']]
df = df_test_avg.tail(1)
test_log = 'Test in:\tacc: {:.3f},\t' \
'ent: {:.3f}({:.3f}/{:.3f}),\t' \
'vac: {:.3f}({:.3f}/{:.3f}),\t'\
'disn: {:.3f}({:.3f}/{:.3f}),\t' \
'bnd_auroc: [ent {:.3f}, disn {:.3f}]'.format(df['test_acc'].iloc[0],
df['in_ent'].iloc[0], df['succ_ent'].iloc[0], df['fail_ent'].iloc[0],
df['in_vac'].iloc[0], df['succ_vac'].iloc[0], df['fail_vac'].iloc[0],
df['in_dis'].iloc[0], df['succ_dis'].iloc[0], df['fail_dis'].iloc[0],
df['bnd_ent_auroc'].iloc[0],df['bnd_dis_auroc'].iloc[0])
print(test_log)
### test on out-of-distribution data ###################
report_result = []
in_num_examples = len(in_score['in_ent'])
ood_MAX_NUM = in_num_examples//args.base_rate
RECALL_LEVEL = args.recall_level
if args.evaluate_benchmark == 'y':
ood_list = ['snli','imdb', 'multi30k', 'wmt16', 'yelp' ]
else:
ood_list = [args.out_dataset]
for ood_dataset in ood_list:
nt_test_data = torch.load('dataset/test/{}_test_out_of_domain.pt'.format(ood_dataset))
nt_test_sampler = SequentialSampler(nt_test_data)
nt_test_dataloader = DataLoader(nt_test_data, sampler=nt_test_sampler, batch_size=args.eval_batch_size)
model.eval()
with torch.no_grad():
df_tmp = pd.DataFrame(columns=['ood_ent', 'ood_vac', 'ood_dis'])
for step, batch in enumerate(nt_test_dataloader):
batch = tuple(t.to(args.device) for t in batch)
if step * args.eval_batch_size > ood_MAX_NUM:
break
b_input_ids, b_input_mask, b_labels = batch
alpha_bar = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask)[0]
p_bar = alpha_bar / alpha_bar.sum(1, keepdim=True)
ent_bar = cal_entropy(p_bar)
disn_bar = getDisn(alpha_bar)
vac_bar = num_labels / torch.sum(alpha_bar, dim=1)
df_tmp.loc[len(df_tmp)] = [i.tolist() for i in [ent_bar, vac_bar, disn_bar]]
out_score = df_tmp.sum()
ood_num_examples, in_num_examples = len(out_score['ood_ent']), len(in_score['in_ent'])
expected_ap = ood_num_examples / (ood_num_examples + in_num_examples)
a = get_performance(out_score['ood_ent'], in_score['in_ent'], expected_ap, recall_level=RECALL_LEVEL)
b = get_performance(out_score['ood_vac'], in_score['in_vac'], expected_ap, recall_level=RECALL_LEVEL)
ent_fpr, ent_auroc, ent_aupr = a[0], a[1], a[2]
vac_fpr, vac_auroc, vac_aupr = b[0], b[1], b[2]
df_ood.loc[len(df_ood)] = [ood_dataset, *out_score, ent_auroc, vac_auroc]
df_ood_avg.loc[len(df_ood_avg)] = [ood_dataset, *out_score.apply(np.average), ent_fpr, ent_auroc, ent_aupr,
vac_fpr, vac_auroc, vac_aupr]
df = df_ood_avg.tail(1)
ood_log = 'Test out:\t{:10s}\tent: {:.3f},\t\t\t' \
'vac: {:.3f},\tdisn: {:.3f}\t\t\t' \
'ent: [fpr {:.3f}, auroc {:.3f}, aupr {:.3f}]\t'\
'vac: [fpr {:.3f}, auroc {:.3f}, aupr {:.3f}]'.format(ood_dataset,
df['ood_ent'].iloc[0],
df['ood_vac'].iloc[0],
df['ood_dis'].iloc[0],
df['ent_fpr'].iloc[0], df['ent_auroc'].iloc[0], df['ent_aupr'].iloc[0],
df['vac_fpr'].iloc[0], df['vac_auroc'].iloc[0], df['vac_aupr'].iloc[0])
print(ood_log)
report_result.append([df['vac_fpr'].iloc[0], df['vac_auroc'].iloc[0], df['vac_aupr'].iloc[0]])
if args.save_result == 'y':
result = {}
in_score_df = in_score.to_frame().T
for key in in_score_df:
result[key] = in_score_df[key][0]
out_score_df = out_score.to_frame().T
for key in out_score_df:
result[key] = out_score_df[key][0]
out_dir = '{}{}_{}_result.pt'.format(folder,args.dataset,ood_dataset)
print('save to %s'%out_dir)
with open(out_dir, "wb") as file:
pickle.dump(result, file)
if __name__ == "__main__":
main()