-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.py
executable file
·199 lines (181 loc) · 7.46 KB
/
methods.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
from fastai.text import *
import eval_w_dropout
import helpers
def random(data_size, args):
'''
input:
data_size: number of instances
'''
randperm = torch.randperm(data_size)
subset = list(randperm[:args.inc])
return subset
def entropy(model, args, df=None):
'''
input:
model: trained model
df: dataframe column of text documents
'''
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
preds = model.get_preds(DatasetType.Test)[0].cuda()
entropy = (-preds*torch.log(preds)).sum(dim=1).cuda()
sorted_e, sorted_ind = entropy.sort(0,True)
if args.cluster and (df is not None):
top_e = torch.empty(args.inc).cuda()
top_ind = torch.empty(args.inc).cuda()
for i in range(args.inc):
for j in range(len(df)):
if kmeans[sorted_ind[j]] == i:
top_e[i] = sorted_e[j]
top_ind[i] = sorted_ind[j]
break
else:
top_e = sorted_e[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_e.cpu().numpy().sum()
def margin(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
preds = model.get_preds(DatasetType.Test)[0].cuda()
sorted_ = preds.sort(descending=True)[0].cuda()
#print(len(sorted_), len(sorted_[0]), len(sorted_[1]))
margins = sorted_[:,0] - sorted_[:,1]
sorted_m, sorted_ind = margins.sort(0,descending=False)
#print(len(margins))
if args.cluster and (df is not None):
top_m = torch.empty(args.inc).cuda()
top_ind = torch.empty(args.inc).cuda()
for i in range(args.inc):
for j in range(len(df)):
if kmeans[sorted_ind[j]] == i:
top_m[i] = sorted_m[j]
top_ind[i] = sorted_ind[j]
break
else:
top_m = sorted_m[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_m.cpu().numpy().sum()
def variation_ratio(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
preds = model.get_preds(DatasetType.Test)[0].cuda()
var = 1 - preds.max(dim=1)[0].cuda()
sorted_var, sorted_ind = var.sort(0, descending=True)
if args.cluster and (df is not None):
top_var = args.empty(args.inc).cuda()
top_ind = args.empty(args.inc).cuda()
for i in range(args.inc):
for j in range(len(df)):
if kmeans[sorted_ind[j]] == i:
top_var[i] = sorted_var[j]
top_ind[i] = sorted_ind[j]
break
else:
top_e = sorted_var[:args.inc].cuda()
top_ind = sorted_var[:args.inc].cuda()
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_e.cpu().numpy().sum()
#-------------------------------------------------------------------------------------------------------
# Dropout methods
def dropout_variability(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
probs = []
for i in range(args.num_preds):
probs.append(eval_w_dropout.get_preds(model, DatasetType.Test)[0].cuda())
probs = torch.stack(probs).cuda()
mean = probs.mean(dim=0).cuda()
var = torch.abs(probs - mean).sum(dim=0).sum(dim=1).cuda()
# var = torch.pow(preds - mean, 2).sum(dim=0).sum(dim=1)
sorted_var, sorted_ind = var.sort(descending=True)
if args.cluster and (df is not None):
top_var = torch.empty(args.inc)
top_ind = torch.empty(args.inc)
for i in range(args.inc):
for j in range(len(sorted_var)):
if kmeans[sorted_ind[j]] == i:
top_var[i] = sorted_var[j]
top_ind[i] = sorted_ind[j]
break
else:
top_var = sorted_var[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_var.cpu().numpy().sum()
def dropout_entropy(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
probs = []
for i in range(args.num_preds):
probs.append(eval_w_dropout.get_preds(model, DatasetType.Test)[0].cuda())
probs = torch.stack(probs).cuda()
mean = probs.mean(dim=0).cuda()
entropies = -(mean*torch.log(mean)).sum(dim=1).cuda()
sorted_e, sorted_ind = entropies.sort(descending=True)
if args.cluster and (df is not None):
top_e = torch.empty(args.inc)
top_ind = torch.empty(args.inc)
for i in range(args.inc):
for j in range(len(sorted_e)):
if kmeans[sorted_ind[j]] == i:
top_e[i] = sorted_e[j]
top_ind[i] = sorted_ind[j]
break
else:
top_e = sorted_e[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_e.cpu().numpy().sum()
def dropout_margin(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
probs = []
for i in range(args.num_preds):
probs.append(eval_w_dropout.get_preds(model, DatasetType.Test)[0].cuda())
probs = torch.stack(probs).cuda()
sorted_mean = probs.mean(dim=0).sort(descending=True)[0]
margins = sorted_mean[:,0] - sorted_mean[:,1]
sorted_m, sorted_ind = margins.sort(descending=False)
if args.cluster and (df is not None):
top_m = torch.empty(args.inc)
top_ind = torch.empty(args.inc)
for i in range(args.inc):
for j in range(len(sorted_m)):
if kmeans[sorted_ind[j]] == i:
top_m[i] = sorted_m[j]
top_ind[i] = sorted_m[j]
break
else:
top_m = sorted_m[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_m.cpu().numpy().sum()
def dropout_variation(model, args, df=None):
if args.cluster and (df is not None): kmeans = helpers.clustering(df, args)
probs = []
for i in range(args.num_preds):
probs.append(eval_w_dropout.get_preds(model, DatasetType.Test)[0].cuda())
probs = torch.stack(probs).cuda()
means = probs.mean(dim=0).cuda()
var = 1 - means.max(dim=1).cuda()
sorted_var, sorted_ind = var.sort(descending=True)
if args.cluster and (df is not None):
top_var = torch.empty(args.inc)
top_ind = torch.empty(args.inc)
for i in range(args.inc):
for j in range(len(sorted_var)):
if kmeans[sorted_ind[j]] == i:
top_var[i] = sorted_var[j]
top_ind[i] = sorted_ind[j]
break
else:
top_var = sorted_var[:args.inc]
top_ind = sorted_ind[:args.inc]
subset = list(top_ind.cpu().numpy())
for i, idx in enumerate(subset): subset[i] = int(idx)
return subset, top_var.cpu().numpy().sum()