forked from thuiar/TEXTOIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
162 lines (113 loc) · 5.54 KB
/
manager.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
from importlib import import_module
import torch
import torch.nn.functional as F
import copy
import logging
from sklearn.metrics import confusion_matrix, accuracy_score, f1_score
from tqdm import trange, tqdm
from losses import loss_map
from utils.functions import restore_model, save_model
from utils.metrics import F_measure
class MSPManager:
def __init__(self, args, data, model, logger_name = 'Detection'):
self.logger = logging.getLogger(logger_name)
self.set_model_optimizer(args, data, model)
self.data = data
self.train_dataloader = data.dataloader.train_labeled_loader
self.eval_dataloader = data.dataloader.eval_loader
self.test_dataloader = data.dataloader.test_loader
self.loss_fct = loss_map[args.loss_fct]
if not args.train:
restore_model(self.model, args.model_output_dir)
def set_model_optimizer(self, args, data, model):
self.model = model.set_model(args, 'bert')
self.optimizer, self.scheduler = model.set_optimizer(self.model, data.dataloader.num_train_examples, args.train_batch_size, \
args.num_train_epochs, args.lr, args.warmup_proportion)
self.device = model.device
def train(self, args, data):
self.logger.info('Training Start...')
best_model = None
best_eval_score = 0
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(self.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):
loss = self.model(input_ids, segment_ids, input_mask, label_ids, mode='train', loss_fct=self.loss_fct)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.scheduler.step()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
loss = tr_loss / nb_tr_steps
y_true, y_pred = self.get_outputs(args, data, mode = 'eval')
eval_score = round(accuracy_score(y_true, y_pred) * 100, 2)
eval_results = {
'train_loss': loss,
'eval_score': eval_score,
'best_eval_score': best_eval_score,
}
self.logger.info("***** Epoch: %s: Eval results *****", str(epoch + 1))
for key in sorted(eval_results.keys()):
self.logger.info(" %s = %s", key, str(eval_results[key]))
if eval_score > best_eval_score:
best_model = copy.deepcopy(self.model)
wait = 0
best_eval_score = eval_score
elif eval_score > 0:
wait += 1
if wait >= args.wait_patient:
break
self.model = best_model
if args.save_model:
save_model(self.model, args.model_output_dir)
def get_outputs(self, args, data, mode = 'eval', get_feats = False):
if mode == 'eval':
dataloader = self.eval_dataloader
elif mode == 'test':
dataloader = self.test_dataloader
self.model.eval()
total_labels = torch.empty(0,dtype=torch.long).to(self.device)
total_logits = torch.empty((0, data.num_labels)).to(self.device)
total_features = torch.empty((0,args.feat_dim)).to(self.device)
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, logits = self.model(input_ids, segment_ids, input_mask)
total_labels = torch.cat((total_labels,label_ids))
total_logits = torch.cat((total_logits, logits))
total_features = torch.cat((total_features, pooled_output))
if get_feats:
feats = total_features.cpu().numpy()
return feats
else:
total_probs = F.softmax(total_logits.detach(), dim=1)
total_maxprobs, total_preds = total_probs.max(dim = 1)
y_prob = total_maxprobs.cpu().numpy()
y_true = total_labels.cpu().numpy()
y_pred = total_preds.cpu().numpy()
if mode == 'test':
y_pred[y_prob < args.threshold] = data.unseen_label_id
return y_true, y_pred
def test(self, args, data, show=False):
y_true, y_pred = self.get_outputs(args, data, mode = 'test')
cm = confusion_matrix(y_true, y_pred)
test_results = F_measure(cm)
acc = round(accuracy_score(y_true, y_pred) * 100, 2)
test_results['Acc'] = acc
self.logger.info
self.logger.info("***** Test: Confusion Matrix *****")
self.logger.info("%s", str(cm))
self.logger.info("***** Test results *****")
for key in sorted(test_results.keys()):
self.logger.info(" %s = %s", key, str(test_results[key]))
test_results['y_true'] = y_true
test_results['y_pred'] = y_pred
return test_results