-
Notifications
You must be signed in to change notification settings - Fork 2
/
SLMM.py
190 lines (141 loc) · 6.54 KB
/
SLMM.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
from model import *
from init_parameter import *
from dataloader import *
from pretrain import *
from util import *
import numpy as np
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.test_results = None
self.predictions = None
self.true_labels = None
self.optimizer = torch.optim.Adam(self.model.parameters(), lr = args.lr_boundary)
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):
_, preds = self.model(input_ids, segment_ids, input_mask)
preds = preds.max(1).indices
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()
print(y_pred,y_true)
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):
best_model = None
wait = 0
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")):
l = np.random.beta(2, 2)
batch = tuple(t.to(self.device) for t in batch)
input_ids, input_mask, segment_ids, label_ids = batch
idx = torch.randperm(input_ids.shape[0])
input_ids_1, input_mask_1, segment_ids_1, label_ids_1 = input_ids[idx], input_mask[idx], segment_ids[idx], label_ids[idx]
mask = (label_ids_1 != label_ids)
label_ids_1[:] = data.num_labels
with torch.set_grad_enabled(True):
loss = self.model(input_ids, segment_ids, input_mask, label_ids, False, "train",input_ids_1, input_mask_1, segment_ids_1, label_ids_1,mask,l,args)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
# 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 epoch >=5:
if eval_score >= self.best_eval_score:
best_model = copy.deepcopy(self.model)
wait = 0
self.best_eval_score = eval_score
else:
wait += 1
if wait >= args.wait_patient:
break
self.model = best_model
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 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())
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!')
#manager.save_results(args)
# debug(data, manager_p, manager, args)