-
Notifications
You must be signed in to change notification settings - Fork 12
/
ADB.py
227 lines (166 loc) · 8.01 KB
/
ADB.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
from model import *
from init_parameter import *
from dataloader import *
from pretrain import *
from util import *
from loss import *
class ModelManager:
def __init__(self, args, data, pretrained_model=None):
self.model = pretrained_model
if self.model is None:
self.model = BertForModel.from_pretrained(args.bert_model, cache_dir = "", num_labels = data.num_labels)
self.restore_model(args)
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model.to(self.device)
self.best_eval_score = 0
self.delta = None
self.delta_points = []
self.centroids = None
self.test_results = None
self.predictions = None
self.true_labels = None
def open_classify(self, features):
logits = euclidean_metric(features, self.centroids)
probs, preds = F.softmax(logits.detach(), dim = 1).max(dim = 1)
euc_dis = torch.norm(features - self.centroids[preds], 2, 1).view(-1)
preds[euc_dis >= self.delta[preds]] = data.unseen_token_id
return preds
def evaluation(self, args, data, mode="eval"):
self.model.eval()
total_labels = torch.empty(0,dtype=torch.long).to(self.device)
total_preds = torch.empty(0,dtype=torch.long).to(self.device)
if mode == 'eval':
dataloader = data.eval_dataloader
elif mode == 'test':
dataloader = data.test_dataloader
for batch in tqdm(dataloader, desc="Iteration"):
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
with torch.set_grad_enabled(False):
pooled_output, _ = self.model(input_ids, segment_ids, input_mask)
preds = self.open_classify(pooled_output)
total_labels = torch.cat((total_labels,label_ids))
total_preds = torch.cat((total_preds, preds))
y_pred = total_preds.cpu().numpy()
y_true = total_labels.cpu().numpy()
self.predictions = list([data.label_list[idx] for idx in y_pred])
self.true_labels = list([data.label_list[idx] for idx in y_true])
if mode == 'eval':
cm = confusion_matrix(y_true, y_pred)
eval_score = F_measure(cm)['F1-score']
return eval_score
elif mode == 'test':
cm = confusion_matrix(y_true,y_pred)
results = F_measure(cm)
acc = round(accuracy_score(y_true, y_pred) * 100, 2)
results['Accuracy'] = acc
self.test_results = results
self.save_results(args)
def train(self, args, data):
criterion_boundary = BoundaryLoss(num_labels = data.num_labels, feat_dim = args.feat_dim)
self.delta = F.softplus(criterion_boundary.delta)
optimizer = torch.optim.Adam(criterion_boundary.parameters(), lr = args.lr_boundary)
self.centroids = self.centroids_cal(args, data)
wait = 0
best_delta, best_centroids = None, None
for epoch in trange(int(args.num_train_epochs), desc="Epoch"):
self.model.train()
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
for step, batch in enumerate(tqdm(data.train_dataloader, desc="Iteration")):
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
with torch.set_grad_enabled(True):
features = self.model(input_ids, segment_ids, input_mask, feature_ext=True)
loss, self.delta = criterion_boundary(features, self.centroids, label_ids)
optimizer.zero_grad()
loss.backward()
optimizer.step()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
self.delta_points.append(self.delta)
# if epoch <= 20:
# plot_curve(self.delta_points)
loss = tr_loss / nb_tr_steps
print('train_loss',loss)
eval_score = self.evaluation(args, data, mode="eval")
print('eval_score',eval_score)
if eval_score >= self.best_eval_score:
wait = 0
self.best_eval_score = eval_score
best_delta = self.delta
best_centroids = self.centroids
else:
wait += 1
if wait >= args.wait_patient:
break
self.delta = best_delta
self.centroids = best_centroids
def class_count(self, labels):
class_data_num = []
for l in np.unique(labels):
num = len(labels[labels == l])
class_data_num.append(num)
return class_data_num
def centroids_cal(self, args, data):
centroids = torch.zeros(data.num_labels, args.feat_dim).cuda()
total_labels = torch.empty(0, dtype=torch.long).to(self.device)
with torch.set_grad_enabled(False):
for batch in data.train_dataloader:
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
features = self.model(input_ids, segment_ids, input_mask, feature_ext=True)
total_labels = torch.cat((total_labels, label_ids))
for i in range(len(label_ids)):
label = label_ids[i]
centroids[label] += features[i]
total_labels = total_labels.cpu().numpy()
centroids /= torch.tensor(self.class_count(total_labels)).float().unsqueeze(1).cuda()
return centroids
def restore_model(self, args):
output_model_file = os.path.join(args.pretrain_dir, WEIGHTS_NAME)
self.model.load_state_dict(torch.load(output_model_file))
def save_results(self, args):
if not os.path.exists(args.save_results_path):
os.makedirs(args.save_results_path)
var = [args.dataset, args.known_cls_ratio, args.labeled_ratio, args.seed]
names = ['dataset', 'known_cls_ratio', 'labeled_ratio', 'seed']
vars_dict = {k:v for k,v in zip(names, var) }
results = dict(self.test_results,**vars_dict)
keys = list(results.keys())
values = list(results.values())
np.save(os.path.join(args.save_results_path, 'centroids.npy'), self.centroids.detach().cpu().numpy())
np.save(os.path.join(args.save_results_path, 'deltas.npy'), self.delta.detach().cpu().numpy())
file_name = 'results.csv'
results_path = os.path.join(args.save_results_path, file_name)
if not os.path.exists(results_path):
ori = []
ori.append(values)
df1 = pd.DataFrame(ori,columns = keys)
df1.to_csv(results_path,index=False)
else:
df1 = pd.read_csv(results_path)
new = pd.DataFrame(results,index=[1])
df1 = df1.append(new,ignore_index=True)
df1.to_csv(results_path,index=False)
data_diagram = pd.read_csv(results_path)
print('test_results', data_diagram)
if __name__ == '__main__':
print('Data and Parameters Initialization...')
parser = init_model()
args = parser.parse_args()
data = Data(args)
print('Pre-training begin...')
manager_p = PretrainModelManager(args, data)
manager_p.train(args, data)
print('Pre-training finished!')
manager = ModelManager(args, data, manager_p.model)
print('Training begin...')
manager.train(args, data)
print('Training finished!')
print('Evaluation begin...')
manager.evaluation(args, data, mode="test")
print('Evaluation finished!')
# debug(data, manager_p, manager, args)