-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainer.py
393 lines (356 loc) · 15.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import torch
import gc
import random
import numpy as np
from functools import partial
from pathlib import Path
from torch.utils.data import DataLoader
from torch.utils.data import Dataset, Subset
from typing import AnyStr, Union, List
from tqdm import tqdm
from transformers import AutoModelForSequenceClassification
from transformers import AutoConfig
from transformers import AutoTokenizer
from transformers import AdamW
from transformers import get_linear_schedule_with_warmup
import ipdb
from datareader import collate_batch_transformer
from datareader import collate_batch_transformer_with_weight
from metrics import ClassificationEvaluator
from metrics import distillation_loss
from model import AutoTransformerMultiTask
from model import AutoTransformerForSentenceSequenceModelingMultiTask
class AbstractTransformerTrainer:
"""
An abstract class which other trainers should implement
"""
def __init__(
self,
model = None,
device = None,
tokenizer = None
):
self.model = model
self.device = device
self.tokenizer = tokenizer
def create_optimizer(self, lr: float, weight_decay: float=0.0):
"""
Create a weighted adam optimizer with the given learning rate
:param lr:
:return:
"""
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay)],
'weight_decay': weight_decay},
{'params': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
return AdamW(optimizer_grouped_parameters, lr=lr)
def save(self, model_file: AnyStr):
"""
Saves the current model
:return:
"""
if not Path(model_file).parent.exists():
Path(model_file).parent.mkdir(parents=True, exist_ok=True)
if self.multi_gpu:
save_model = self.model.module
else:
save_model = self.model
torch.save(save_model.state_dict(), model_file)
def load(self, model_file: AnyStr, new_classifier: bool = False, add_missing_keys: bool = True):
"""
Loads the model given by model_file
:param model_file:
:return:
"""
if self.multi_gpu:
model_dict = self.model.module.state_dict()
load_model = self.model.module
else:
model_dict = self.model.state_dict()
load_model = self.model
if new_classifier:
weights = {k: v for k, v in torch.load(model_file, map_location=lambda storage, loc: storage).items() if "classifier" not in k and "pooler" not in k}
model_dict.update([(k, weights[k]) for k in weights if k in model_dict or add_missing_keys])
load_model.load_state_dict(model_dict)
else:
weights = torch.load(model_file, map_location=lambda storage, loc: storage)
model_dict.update([(k, weights[k]) for k in weights if k in model_dict or add_missing_keys])
load_model.load_state_dict(model_dict)
def freeze(self, exclude_params: List=[]):
"""
Freeze the model weights
:return:
"""
for n,p in self.model.named_parameters():
if n not in exclude_params:
p.requires_grad = False
class TransformerClassificationTrainer(AbstractTransformerTrainer):
"""
A class to encapsulate all of the training and evaluation of a
transformer model for classification
"""
def __init__(
self,
transformer_model: Union[AnyStr, torch.nn.Module],
device: torch.device,
num_labels: Union[int, List],
multi_task: bool = False,
tokenizer=None,
multi_gpu: bool = False,
sequence_modeling = False
):
if type(num_labels) != list:
num_labels = [num_labels]
if type(transformer_model) == str:
self.model_name = transformer_model
# Create the model
if multi_task:
if sequence_modeling:
self.model = AutoTransformerForSentenceSequenceModelingMultiTask(transformer_model, num_labels).to(device)
else:
self.model = AutoTransformerMultiTask(transformer_model, num_labels).to(device)
else:
config = AutoConfig.from_pretrained(transformer_model, num_labels=num_labels[0])
self.model = AutoModelForSequenceClassification.from_pretrained(transformer_model, config=config).to(device)
self.tokenizer = AutoTokenizer.from_pretrained(transformer_model)
else:
self.model_name = 'custom'
self.model = transformer_model
self.tokenizer = tokenizer
if tokenizer == None:
print("WARNING: No tokenizer passed to trainer, incorrect padding token may be used.")
if multi_gpu:
self.model = torch.nn.DataParallel(self.model)
self.device = device
self.num_labels = num_labels
self.multi_task = multi_task
self.multi_gpu = multi_gpu
def evaluate(
self,
validation_dset: Dataset,
num_labels: int,
eval_averaging: AnyStr = 'micro',
return_labels_logits: bool = False,
task_idx: int = None,
temperature: int = 1.0
):
"""
Runs a round of evaluation on the given dataset
:param validation_dset:
:return:
"""
if self.tokenizer is not None:
pad_token_id = self.tokenizer.pad_token_id
else:
pad_token_id = 0
# Create the validation evaluator
validation_evaluator = ClassificationEvaluator(
validation_dset,
self.device,
num_labels=num_labels,
averaging=eval_averaging,
pad_token_id=pad_token_id,
multi_gpu=self.multi_gpu,
multi_task=self.multi_task,
task_idx=task_idx,
temperature=temperature
)
return validation_evaluator.evaluate(self.model, return_labels_logits=return_labels_logits)
def train(
self,
train_dset: Union[List, Dataset],
validation_dset: Union[List, Dataset] = [],
logger = None,
lr: float = 3e-5,
n_epochs: int = 2,
batch_size: int = 8,
weight_decay: float = 0.0,
warmup_steps: int = 200,
log_interval: int = 1,
metric_name: AnyStr = 'accuracy',
patience: int = 10,
model_file: AnyStr = "model.pth",
class_weights=None,
use_scheduler: bool = True,
eval_averaging: AnyStr = ['binary'],
lams: List = None,
clip_grad: float = None,
num_dataset_workers: int = 10,
eval_task: int = 0,
temperature: int = 1.0,
sequential_multitask: bool = False,
task: int = None
):
if type(train_dset) != list:
train_dset = [train_dset]
if type(validation_dset) != list:
validation_dset = [validation_dset]
if self.tokenizer is not None:
pad_token_id = self.tokenizer.pad_token_id
else:
pad_token_id = 0
if lams is None:
lams = [1.0] * len(train_dset)
collate_fn = partial(collate_batch_transformer, pad_token_id)
distill_loss = partial(distillation_loss, temperature)
# Create the loss function
if class_weights is None:
loss_fn = torch.nn.CrossEntropyLoss()
elif (isinstance(class_weights, str) and class_weights == 'sample_based_weight'):
loss_fn = torch.nn.CrossEntropyLoss(reduction='none')
collate_fn = partial(collate_batch_transformer_with_weight, pad_token_id)
elif isinstance(class_weights, str) and class_weights == 'balanced':
# Calculate the weights
if self.multi_task:
raise Exception('Not Implemented!')
if isinstance(train_dset[0], Subset):
labels = train_dset[0].dataset.getLabels(train_dset[0].indices).astype(np.int64)
else:
labels = train_dset[0].getLabels().astype(np.int64)
weight = torch.tensor(len(labels) / (self.num_labels[0] * np.bincount(labels)))
weight = weight.type(torch.FloatTensor).to(self.device)
loss_fn = torch.nn.CrossEntropyLoss(weight=weight)
elif isinstance(class_weights, List) and self.multi_task:
loss_fn = [torch.nn.CrossEntropyLoss(weight=torch.tensor(w).type(torch.FloatTensor).to(self.device)) for w in class_weights]
else:
loss_fn = torch.nn.CrossEntropyLoss(weight=torch.tensor(class_weights).type(torch.FloatTensor).to(self.device))
# Create the training dataloader(s)
train_dls = [DataLoader(
ds,
batch_size=batch_size,
shuffle=True,
collate_fn=collate_fn,
num_workers=num_dataset_workers
) for ds in train_dset]
# Create the optimizer
optimizer = self.create_optimizer(lr, weight_decay)
total = sum(len(dl) for dl in train_dls) if task is None else len(train_dls[task])
if use_scheduler:
# Create the scheduler
scheduler = get_linear_schedule_with_warmup(
optimizer,
warmup_steps,
n_epochs * len(train_dls[0])#total
)
# Set up metric tracking
best_metric = 0.0 if metric_name != 'loss' else -float('inf')
patience_counter = 0
# Save before training
self.save(model_file)
# Main training loop
for ep in range(n_epochs):
# Training loop
dl_iters = [iter(dl) for dl in train_dls]
dl_idx = list(reversed(range(len(dl_iters)))) if task is None else [task]
finished = [0] * len(dl_iters)
if task is not None:
finished = [1] * len(dl_iters)
finished[task] = 0
i = 0
with tqdm(total=total, desc="Training") as pbar:
while sum(finished) < len(dl_iters):
if not sequential_multitask:
random.shuffle(dl_idx)
for d in dl_idx:
run_sequential = True
while finished[d] != 1 and run_sequential:
run_sequential = sequential_multitask
task_dl = dl_iters[d]
try:
batch = next(task_dl)
except StopIteration:
finished[d] = 1
continue
self.model.train()
optimizer.zero_grad()
batch = tuple(t.to(self.device) if isinstance(t, torch.Tensor) else t for t in batch)
input_ids = batch[0]
masks = batch[1]
labels = batch[2]
if not self.multi_task:
outputs = self.model(input_ids, attention_mask=masks)
logits = outputs.logits
else:
logits_mask = batch[3] if len(batch) > 3 else None
outputs = self.model(input_ids, attention_mask=masks, logits_mask=logits_mask, task_num=d)
logits = outputs[0]
# Calculate what the weight of the loss should be
loss_weight = lams[d]
labels = labels.view(logits.shape[0], -1)
if labels.shape[1] > 1:
# Soft target loss
loss = loss_weight * distill_loss(logits.view(-1, self.num_labels[d]), labels.view(-1, self.num_labels[d]))
elif (isinstance(class_weights, str) and class_weights == 'sample_based_weight'):
sample_weight = batch[-1]
loss_weight *= sample_weight
loss = (loss_weight * loss_fn(logits.view(-1, self.num_labels[d]), labels.view(-1))).mean()
elif isinstance(loss_fn, List):
loss = loss_weight * loss_fn[d](logits.view(-1, self.num_labels[d]), labels.view(-1))
else:
loss = loss_weight * loss_fn(logits.view(-1, self.num_labels[d]), labels.view(-1))
if self.multi_gpu:
loss = loss.mean()
if i % log_interval == 0 and logger is not None:
logger.log({"Loss": loss.item()})
loss.backward()
# Clip gradients
if clip_grad:
torch.nn.utils.clip_grad_norm_(self.model.parameters(), clip_grad)
optimizer.step()
i += 1
pbar.update(1)
if use_scheduler:
scheduler.step()
gc.collect()
if len(validation_dset) == 0:
self.save(model_file)
# Inline evaluation
for i in range(len(validation_dset)):
(val_loss, acc, P, R, F1) = self.evaluate(validation_dset[i], self.num_labels[i], eval_averaging[i], task_idx=i, temperature=temperature)
if metric_name == 'accuracy':
metric = acc
elif metric_name == 'loss':
metric = -val_loss # negative since we are always maximizing
else:
metric = F1
if eval_averaging is None:
# Macro average if averaging is None
metric = sum(F1) / len(F1)
print(f"{metric_name}: {abs(metric)}")
if logger is not None:
# Log
logger.log({
'Validation accuracy - Task {}'.format(i): acc,
'Validation Precision - Task {}'.format(i): P,
'Validation Recall - Task {}'.format(i): R,
'Validation F1 - Task {}'.format(i): F1,
'Validation loss - Task{}'.format(i): val_loss}
)
else:
print({
'Validation accuracy': acc,
'Validation Precision': P,
'Validation Recall': R,
'Validation F1': F1,
'Validation loss': val_loss}
)
# Saving the best model and early stopping
# if val_loss < best_loss:
if i == eval_task:
if metric > best_metric:
best_metric = metric
if logger != None:
logger.log({f"best_{metric_name}": abs(metric)})
else:
print(f"Best {metric_name}: {abs(metric)}")
self.save(model_file)
patience_counter = 0
else:
patience_counter += 1
# Stop training once we have lost patience
if patience_counter == patience:
break
gc.collect()
self.load(model_file)