-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDeepAligned.py
279 lines (204 loc) · 10.2 KB
/
DeepAligned.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from model import *
from init_parameter import *
from dataloader import *
from pretrain import *
from util import *
class ModelManager:
def __init__(self, args, data, pretrained_model=None):
if pretrained_model is None:
pretrained_model = BertForModel.from_pretrained(args.bert_model, cache_dir = "", num_labels = data.n_known_cls)
if os.path.exists(args.pretrain_dir):
pretrained_model = self.restore_model(args.pretrained_model)
self.pretrained_model = pretrained_model
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if args.cluster_num_factor > 1:
self.num_labels = self.predict_k(args, data)
else:
self.num_labels = data.num_labels
self.model = BertForModel.from_pretrained(args.bert_model, cache_dir = "", num_labels = self.num_labels)
if args.pretrain:
self.load_pretrained_model(args)
if args.freeze_bert_parameters:
self.freeze_parameters(self.model)
self.model.to(self.device)
num_train_examples = len(data.train_labeled_examples) + len(data.train_unlabeled_examples)
self.num_train_optimization_steps = int(num_train_examples / args.train_batch_size) * args.num_train_epochs
self.optimizer = self.get_optimizer(args)
self.best_eval_score = 0
self.centroids = None
self.test_results = None
self.predictions = None
self.true_labels = None
def get_features_labels(self, dataloader, model, args):
model.eval()
total_features = torch.empty((0,args.feat_dim)).to(self.device)
total_labels = torch.empty(0,dtype=torch.long).to(self.device)
for batch in tqdm(dataloader, desc="Extracting representation"):
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
with torch.no_grad():
feature = model(input_ids, segment_ids, input_mask, feature_ext = True)
total_features = torch.cat((total_features, feature))
total_labels = torch.cat((total_labels, label_ids))
return total_features, total_labels
def predict_k(self, args, data):
feats, _ = self.get_features_labels(data.train_semi_dataloader, self.pretrained_model, args)
feats = feats.cpu().numpy()
km = KMeans(n_clusters = data.num_labels).fit(feats)
y_pred = km.labels_
pred_label_list = np.unique(y_pred)
drop_out = len(feats) / data.num_labels
print('drop',drop_out)
cnt = 0
for label in pred_label_list:
num = len(y_pred[y_pred == label])
if num < drop_out:
cnt += 1
num_labels = len(pred_label_list) - cnt
print('pred_num',num_labels)
return num_labels
def get_optimizer(self, args):
param_optimizer = list(self.model.named_parameters())
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = BertAdam(optimizer_grouped_parameters,
lr = args.lr,
warmup = args.warmup_proportion,
t_total = self.num_train_optimization_steps)
return optimizer
def evaluation(self, args, data):
feats, labels = self.get_features_labels(data.test_dataloader, self.model, args)
feats = feats.cpu().numpy()
km = KMeans(n_clusters = self.num_labels).fit(feats)
y_pred = km.labels_
y_true = labels.cpu().numpy()
results = clustering_score(y_true, y_pred)
print('results',results)
ind, _ = hungray_aligment(y_true, y_pred)
map_ = {i[0]:i[1] for i in ind}
y_pred = np.array([map_[idx] for idx in y_pred])
cm = confusion_matrix(y_true,y_pred)
print('confusion matrix',cm)
self.test_results = results
self.save_results(args)
def alignment(self, km, args):
if self.centroids is not None:
old_centroids = self.centroids.cpu().numpy()
new_centroids = km.cluster_centers_
DistanceMatrix = np.linalg.norm(old_centroids[:,np.newaxis,:]-new_centroids[np.newaxis,:,:],axis=2)
row_ind, col_ind = linear_sum_assignment(DistanceMatrix)
new_centroids = torch.tensor(new_centroids).to(self.device)
self.centroids = torch.empty(self.num_labels ,args.feat_dim).to(self.device)
alignment_labels = list(col_ind)
for i in range(self.num_labels):
label = alignment_labels[i]
self.centroids[i] = new_centroids[label]
pseudo2label = {label:i for i,label in enumerate(alignment_labels)}
pseudo_labels = np.array([pseudo2label[label] for label in km.labels_])
else:
self.centroids = torch.tensor(km.cluster_centers_).to(self.device)
pseudo_labels = km.labels_
pseudo_labels = torch.tensor(pseudo_labels, dtype=torch.long).to(self.device)
return pseudo_labels
def update_pseudo_labels(self, pseudo_labels, args, data):
train_data = TensorDataset(data.semi_input_ids, data.semi_input_mask, data.semi_segment_ids, pseudo_labels)
train_sampler = SequentialSampler(train_data)
train_dataloader = DataLoader(train_data, sampler = train_sampler, batch_size = args.train_batch_size)
return train_dataloader
def train(self, args, data):
best_score = 0
best_model = None
wait = 0
for epoch in trange(int(args.num_train_epochs), desc="Epoch"):
feats, _ = self.get_features_labels(data.train_semi_dataloader, self.model, args)
feats = feats.cpu().numpy()
km = KMeans(n_clusters = self.num_labels).fit(feats)
score = metrics.silhouette_score(feats, km.labels_)
print('score',score)
if score > best_score:
best_model = copy.deepcopy(self.model)
wait = 0
best_score = score
else:
wait += 1
if wait >= args.wait_patient:
self.model = best_model
break
pseudo_labels = self.alignment(km, args)
train_dataloader = self.update_pseudo_labels(pseudo_labels, args, data)
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
self.model.train()
for batch in tqdm(train_dataloader, desc="Pseudo-Training"):
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
loss = self.model(input_ids, segment_ids, input_mask, label_ids, mode='train')
loss.backward()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
self.optimizer.step()
self.optimizer.zero_grad()
tr_loss = tr_loss / nb_tr_steps
print('train_loss',tr_loss)
def load_pretrained_model(self, args):
pretrained_dict = self.pretrained_model.state_dict()
classifier_params = ['classifier.weight','classifier.bias']
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k not in classifier_params}
self.model.load_state_dict(pretrained_dict, strict=False)
def restore_model(self, args, model):
output_model_file = os.path.join(args.pretrain_dir, WEIGHTS_NAME)
model.load_state_dict(torch.load(output_model_file))
return model
def freeze_parameters(self,model):
for name, param in model.bert.named_parameters():
param.requires_grad = False
if "encoder.layer.11" in name or "pooler" in name:
param.requires_grad = True
def save_results(self, args):
if not os.path.exists(args.save_results_path):
os.makedirs(args.save_results_path)
var = [args.dataset, args.method, args.known_cls_ratio, args.labeled_ratio, args.cluster_num_factor, args.seed, self.num_labels]
names = ['dataset', 'method', 'known_cls_ratio', 'labeled_ratio', 'cluster_num_factor','seed', 'K']
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())
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)
if args.pretrain:
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)
else:
manager = ModelManager(args, data)
print('Training begin...')
manager.train(args,data)
print('Training finished!')
print('Evaluation begin...')
manager.evaluation(args, data)
print('Evaluation finished!')
manager.save_results(args)