-
Notifications
You must be signed in to change notification settings - Fork 52
/
trainer.py
308 lines (259 loc) · 11.4 KB
/
trainer.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import logging
import os
import random
import numpy as np
import torch
import torch.nn.functional as F
from sklearn.metrics import f1_score, matthews_corrcoef
from tensorboardX import SummaryWriter
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from tqdm import tqdm, trange
from datasets import my_collate, my_collate_elmo, my_collate_pure_bert, my_collate_bert
from transformers import AdamW
from transformers import BertTokenizer
logger = logging.getLogger(__name__)
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
def get_input_from_batch(args, batch):
embedding_type = args.embedding_type
if embedding_type == 'glove' or embedding_type == 'elmo':
# sentence_ids, aspect_ids, dep_tag_ids, pos_class, text_len, aspect_len, sentiment, dep_rel_ids, dep_heads, aspect_positions
inputs = { 'sentence': batch[0],
'aspect': batch[1], # aspect token
'dep_tags': batch[2], # reshaped
'pos_class': batch[3],
'text_len': batch[4],
'aspect_len': batch[5],
'dep_rels': batch[7], # adj no-reshape
'dep_heads': batch[8],
'aspect_position': batch[9],
'dep_dirs': batch[10]
}
labels = batch[6]
else: # bert
if args.pure_bert:
# input_cat_ids, segment_ids, dep_tag_ids, pos_class, text_len, aspect_len, sentiment, dep_rel_ids, dep_heads, aspect_positions
inputs = { 'input_ids': batch[0],
'token_type_ids': batch[1]}
labels = batch[6]
else:
# input_ids, word_indexer, input_aspect_ids, aspect_indexer, dep_tag_ids, pos_class, text_len, aspect_len, sentiment, dep_rel_ids, dep_heads, aspect_positions
inputs = { 'input_ids': batch[0],
'input_aspect_ids': batch[2],
'word_indexer': batch[1],
'aspect_indexer': batch[3],
'input_cat_ids': batch[4],
'segment_ids': batch[5],
'dep_tags': batch[6],
'pos_class': batch[7],
'text_len': batch[8],
'aspect_len': batch[9],
'dep_rels': batch[11],
'dep_heads': batch[12],
'aspect_position': batch[13],
'dep_dirs': batch[14]}
labels = batch[10]
return inputs, labels
def get_collate_fn(args):
embedding_type = args.embedding_type
if embedding_type == 'glove':
return my_collate
elif embedding_type == 'elmo':
return my_collate_elmo
else:
if args.pure_bert:
return my_collate_pure_bert
else:
return my_collate_bert
def get_bert_optimizer(args, model):
# Prepare optimizer and schedule (linear warmup and decay)
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
'weight_decay': args.weight_decay},
{'params': [p for n, p in model.named_parameters() if any(
nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
optimizer = AdamW(optimizer_grouped_parameters,
lr=args.learning_rate, eps=args.adam_epsilon)
# scheduler = WarmupLinearSchedule(
# optimizer, warmup_steps=args.warmup_steps, t_total=t_total)
return optimizer
def train(args, train_dataset, model, test_dataset):
'''Train the model'''
tb_writer = SummaryWriter()
args.train_batch_size = args.per_gpu_train_batch_size
train_sampler = RandomSampler(train_dataset)
collate_fn = get_collate_fn(args)
train_dataloader = DataLoader(train_dataset, sampler=train_sampler,
batch_size=args.train_batch_size,
collate_fn=collate_fn)
if args.max_steps > 0:
t_total = args.max_steps
args.num_train_epochs = args.max_steps // (
len(train_dataloader) // args.gradient_accumulation_steps) + 1
else:
t_total = len(
train_dataloader) // args.gradient_accumulation_steps * args.num_train_epochs
if args.embedding_type == 'bert':
optimizer = get_bert_optimizer(args, model)
else:
parameters = filter(lambda param: param.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters, lr=args.learning_rate)
# Train
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_dataset))
logger.info(" Num Epochs = %d", args.num_train_epochs)
logger.info(" Instantaneous batch size per GPU = %d",
args.per_gpu_train_batch_size)
logger.info(" Gradient Accumulation steps = %d",
args.gradient_accumulation_steps)
logger.info(" Total optimization steps = %d", t_total)
global_step = 0
tr_loss, logging_loss = 0.0, 0.0
all_eval_results = []
model.zero_grad()
train_iterator = trange(int(args.num_train_epochs), desc="Epoch")
set_seed(args)
for _ in train_iterator:
# epoch_iterator = tqdm(train_dataloader, desc='Iteration')
for step, batch in enumerate(train_dataloader):
model.train()
batch = tuple(t.to(args.device) for t in batch)
inputs, labels = get_input_from_batch(args, batch)
logit = model(**inputs)
loss = F.cross_entropy(logit, labels)
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
loss.backward()
torch.nn.utils.clip_grad_norm_(
model.parameters(), args.max_grad_norm)
tr_loss += loss.item()
if (step + 1) % args.gradient_accumulation_steps == 0:
# scheduler.step() # Update learning rate schedule
optimizer.step()
model.zero_grad()
global_step += 1
# Log metrics
if args.logging_steps > 0 and global_step % args.logging_steps == 0:
results, eval_loss = evaluate(args, test_dataset, model)
all_eval_results.append(results)
for key, value in results.items():
tb_writer.add_scalar(
'eval_{}'.format(key), value, global_step)
tb_writer.add_scalar('eval_loss', eval_loss, global_step)
# tb_writer.add_scalar('lr', scheduler.get_lr()[0], global_step)
tb_writer.add_scalar(
'train_loss', (tr_loss - logging_loss) / args.logging_steps, global_step)
logging_loss = tr_loss
# Save model checkpoint
if args.max_steps > 0 and global_step > args.max_steps:
epoch_iterator.close()
break
if args.max_steps > 0 and global_step > args.max_steps:
epoch_iterator.close()
break
tb_writer.close()
return global_step, tr_loss/global_step, all_eval_results
def evaluate(args, eval_dataset, model):
results = {}
args.eval_batch_size = args.per_gpu_eval_batch_size
eval_sampler = SequentialSampler(eval_dataset)
collate_fn = get_collate_fn(args)
eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler,
batch_size=args.eval_batch_size,
collate_fn=collate_fn)
# Eval
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_dataset))
logger.info(" Batch size = %d", args.eval_batch_size)
eval_loss = 0.0
nb_eval_steps = 0
preds = None
out_label_ids = None
for batch in eval_dataloader:
# for batch in tqdm(eval_dataloader, desc='Evaluating'):
model.eval()
batch = tuple(t.to(args.device) for t in batch)
with torch.no_grad():
inputs, labels = get_input_from_batch(args, batch)
logits = model(**inputs)
tmp_eval_loss = F.cross_entropy(logits, labels)
eval_loss += tmp_eval_loss.mean().item()
nb_eval_steps += 1
if preds is None:
preds = logits.detach().cpu().numpy()
out_label_ids = labels.detach().cpu().numpy()
else:
preds = np.append(preds, logits.detach().cpu().numpy(), axis=0)
out_label_ids = np.append(
out_label_ids, labels.detach().cpu().numpy(), axis=0)
eval_loss = eval_loss / nb_eval_steps
preds = np.argmax(preds, axis=1)
# print(preds)
result = compute_metrics(preds, out_label_ids)
results.update(result)
output_eval_file = os.path.join(args.output_dir, 'eval_results.txt')
with open(output_eval_file, 'a+') as writer:
logger.info('***** Eval results *****')
logger.info(" eval loss: %s", str(eval_loss))
for key in sorted(result.keys()):
logger.info(" %s = %s", key, str(result[key]))
writer.write(" %s = %s\n" % (key, str(result[key])))
writer.write('\n')
writer.write('\n')
return results, eval_loss
def evaluate_badcase(args, eval_dataset, model, word_vocab):
eval_sampler = SequentialSampler(eval_dataset)
collate_fn = get_collate_fn(args)
eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler,
batch_size=1,
collate_fn=collate_fn)
# Eval
badcases = []
eval_loss = 0.0
nb_eval_steps = 0
preds = None
out_label_ids = None
for batch in eval_dataloader:
# for batch in tqdm(eval_dataloader, desc='Evaluating'):
model.eval()
batch = tuple(t.to(args.device) for t in batch)
with torch.no_grad():
inputs, labels = get_input_from_batch(args, batch)
logits = model(**inputs)
pred = int(np.argmax(logits.detach().cpu().numpy(), axis=1)[0])
label = int(labels.detach().cpu().numpy()[0])
if pred != label:
if args.embedding_type == 'bert':
sent_ids = inputs['input_ids'][0].detach().cpu().numpy()
aspect_ids = inputs['input_aspect_ids'][0].detach().cpu().numpy()
case = {}
case['sentence'] = args.tokenizer.decode(sent_ids)
case['aspect'] = args.tokenizer.decode(aspect_ids)
case['pred'] = pred
case['label'] = label
badcases.append(case)
else:
sent_ids = inputs['sentence'][0].detach().cpu().numpy()
aspect_ids = inputs['aspect'][0].detach().cpu().numpy()
case = {}
case['sentence'] = ' '.join([word_vocab['itos'][i] for i in sent_ids])
case['aspect'] = ' '.join([word_vocab['itos'][i] for i in aspect_ids])
case['pred'] = pred
case['label'] = label
badcases.append(case)
return badcases
def simple_accuracy(preds, labels):
return (preds == labels).mean()
def acc_and_f1(preds, labels):
acc = simple_accuracy(preds, labels)
f1 = f1_score(y_true=labels, y_pred=preds, average='macro')
return {
"acc": acc,
"f1": f1
}
def compute_metrics(preds, labels):
return acc_and_f1(preds, labels)