-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert_classifier.py
executable file
·377 lines (321 loc) · 13.2 KB
/
bert_classifier.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
# -*- coding: utf-8 -*-
import logging as log
from collections import OrderedDict
import numpy as np
import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import DataLoader, RandomSampler
from transformers import AutoModel, AutoTokenizer
import pytorch_lightning as pl
from bert_tokenizer import BERTTextEncoder
from dataloader import topic_finder_dataset
from test_tube import HyperOptArgumentParser
from torchnlp.encoders import LabelEncoder
from torchnlp.utils import collate_tensors, lengths_to_mask
from utils import mask_fill
class BERTClassifier(pl.LightningModule):
"""
Sample model to show how to use BERT to classify sentences.
:param hparams: ArgumentParser containing the hyperparameters.
"""
def __init__(self, hparams) -> None:
super(BERTClassifier, self).__init__()
self.hparams = hparams
self.batch_size = hparams.batch_size
# build model
self.__build_model()
# Loss criterion initialization.
self.__build_loss()
if hparams.nr_frozen_epochs > 0:
self._frozen = True
self.freeze_encoder()
else:
self._frozen = False
self.nr_frozen_epochs = hparams.nr_frozen_epochs
def __build_model(self) -> None:
""" Init BERT model + tokenizer + classification head."""
self.bert = AutoModel.from_pretrained(
"google/bert_uncased_L-2_H-128_A-2", output_hidden_states=True
)
# Tokenizer
self.tokenizer = BERTTextEncoder("google/bert_uncased_L-2_H-128_A-2")
# Label Encoder
self.label_encoder = LabelEncoder(
self.hparams.label_set.split(";"), reserved_labels=[]
)
self.label_encoder.unknown_index = None
# Classification head
self.classification_head = nn.Sequential(
nn.Linear(128, 256),
nn.ReLU(),
nn.Linear(512, 128),
nn.ReLU(),
nn.Linear(128, self.label_encoder.vocab_size),
)
def __build_loss(self):
""" Initializes the loss function/s. """
self._loss = nn.CrossEntropyLoss()
def unfreeze_encoder(self) -> None:
""" un-freezes the encoder layer. """
if self._frozen:
log.info(f"\n-- Encoder model fine-tuning")
for param in self.bert.parameters():
param.requires_grad = True
def freeze_encoder(self) -> None:
""" freezes the encoder layer. """
for param in self.bert.parameters():
param.requires_grad = False
def predict(self, sample: dict) -> dict:
""" Predict function.
:param sample: dictionary with the text we want to classify.
Returns:
Dictionary with the input text and the predicted label.
"""
if self.training:
self.eval()
with torch.no_grad():
model_input, _ = self.prepare_sample([sample], prepare_target=False)
model_out = self.forward(**model_input)
logits = model_out["logits"].numpy()
predicted_labels = [
self.label_encoder.index_to_token[prediction]
for prediction in np.argmax(logits, axis=1)
]
sample["predicted_label"] = predicted_labels[0]
return sample
def forward(self, tokens, lengths):
""" Usual pytorch forward function.
:param tokens: text sequences [batch_size x src_seq_len]
:param lengths: source lengths [batch_size]
Returns:
Dictionary with model outputs (e.g: logits)
"""
tokens = tokens[:, : lengths.max()]
# When using just one GPU this should not change behavior
# but when splitting batches across GPU the tokens have padding
# from the entire original batch
mask = lengths_to_mask(lengths, device=tokens.device)
# Run BERT model.
word_embeddings, _, _ = self.bert(tokens, mask)
# Average Pooling
word_embeddings = mask_fill(
0.0, tokens, word_embeddings, self.tokenizer.padding_index
)
sentemb = torch.sum(word_embeddings, 1)
sum_mask = mask.unsqueeze(-1).expand(word_embeddings.size()).float().sum(1)
sentemb = sentemb / sum_mask
return {"logits": self.classification_head(sentemb)}
def loss(self, predictions: dict, targets: dict) -> torch.tensor:
"""
Computes Loss value according to a loss function.
:param predictions: model specific output. Must contain a key 'logits' with
a tensor [batch_size x 1] with model predictions
:param labels: Label values [batch_size]
Returns:
torch.tensor with loss value.
"""
return self._loss(predictions["logits"], targets["labels"])
def prepare_sample(self, sample: list, prepare_target: bool = True) -> (dict, dict):
"""
Function that prepares a sample to input the model.
:param sample: list of dictionaries.
Returns:
- dictionary with the expected model inputs.
- dictionary with the expected target labels.
"""
sample = collate_tensors(sample)
tokens, lengths = self.tokenizer.batch_encode(sample["text"])
inputs = {"tokens": tokens, "lengths": lengths}
if not prepare_target:
return inputs, {}
#Prepare target:
try:
targets = {"labels": self.label_encoder.batch_encode(sample["topic"])}
return inputs, targets
except RuntimeError:
raise Exception("Label encoder found an unknown label.")
def training_step(self, batch: tuple, batch_nb: int, *args, **kwargs) -> dict:
"""
Runs one training step. This usually consists in the forward function followed
by the loss function.
:param batch: The output of your dataloader.
:param batch_nb: Integer displaying which batch this is
Returns:
- dictionary containing the loss and the metrics to be added to the lightning logger.
"""
inputs, targets = batch
model_out = self.forward(**inputs)
loss_val = self.loss(model_out, targets)
# in DP mode (default) make sure if result is scalar, there's another dim in the beginning
if self.trainer.use_dp or self.trainer.use_ddp2:
loss_val = loss_val.unsqueeze(0)
tqdm_dict = {"train_loss": loss_val}
output = OrderedDict(
{"loss": loss_val, "progress_bar": tqdm_dict, "log": tqdm_dict}
)
# can also return just a scalar instead of a dict (return loss_val)
return output
def validation_step(self, batch: tuple, batch_nb: int, *args, **kwargs) -> dict:
""" Similar to the training step but with the model in eval mode.
Returns:
- dictionary passed to the validation_end function.
"""
inputs, targets = batch
model_out = self.forward(**inputs)
loss_val = self.loss(model_out, targets)
y = targets["labels"]
y_hat = model_out["logits"]
# acc
labels_hat = torch.argmax(y_hat, dim=1)
val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)
val_acc = torch.tensor(val_acc)
if self.on_gpu:
val_acc = val_acc.cuda(loss_val.device.index)
# in DP mode (default) make sure if result is scalar, there's another dim in the beginning
if self.trainer.use_dp or self.trainer.use_ddp2:
loss_val = loss_val.unsqueeze(0)
val_acc = val_acc.unsqueeze(0)
pdb.set_trace()
output = OrderedDict({"val_loss": loss_val, "val_acc": val_acc,})
# can also return just a scalar instead of a dict (return loss_val)
return output
def validation_end(self, outputs: list) -> dict:
""" Function that takes as input a list of dictionaries returned by the validation_step
function and measures the model performance accross the entire validation set.
Returns:
- Dictionary with metrics to be added to the lightning logger.
"""
val_loss_mean = 0
val_acc_mean = 0
for output in outputs:
val_loss = output["val_loss"]
# reduce manually when using dp
if self.trainer.use_dp or self.trainer.use_ddp2:
val_loss = torch.mean(val_loss)
val_loss_mean += val_loss
# reduce manually when using dp
val_acc = output["val_acc"]
if self.trainer.use_dp or self.trainer.use_ddp2:
val_acc = torch.mean(val_acc)
val_acc_mean += val_acc
val_loss_mean /= len(outputs)
val_acc_mean /= len(outputs)
tqdm_dict = {"val_loss": val_loss_mean, "val_acc": val_acc_mean}
result = {
"progress_bar": tqdm_dict,
"log": tqdm_dict,
"val_loss": val_loss_mean,
}
return result
def configure_optimizers(self):
""" Sets different Learning rates for different parameter groups. """
parameters = [
{"params": self.classification_head.parameters()},
{
"params": self.bert.parameters(),
"lr": self.hparams.encoder_learning_rate,
},
]
optimizer = optim.Adam(parameters, lr=self.hparams.learning_rate)
return [optimizer], []
def on_epoch_end(self):
""" Pytorch lightning hook """
if self.current_epoch + 1 >= self.nr_frozen_epochs:
self.unfreeze_encoder()
def __retrieve_dataset(self, train=True, val=True, test=True):
""" Retrieves task specific dataset """
return topic_finder_dataset(self.hparams, train, val, test)
@pl.data_loader
def train_dataloader(self) -> DataLoader:
""" Function that loads the train set. """
self._train_dataset = self.__retrieve_dataset(val=False, test=False)[0]
return DataLoader(
dataset=self._train_dataset,
sampler=RandomSampler(self._train_dataset),
batch_size=self.hparams.batch_size,
collate_fn=self.prepare_sample,
num_workers=self.hparams.loader_workers,
)
@pl.data_loader
def val_dataloader(self) -> DataLoader:
""" Function that loads the validation set. """
self._dev_dataset = self.__retrieve_dataset(train=False, test=False)[0]
return DataLoader(
dataset=self._dev_dataset,
batch_size=self.hparams.batch_size,
collate_fn=self.prepare_sample,
num_workers=self.hparams.loader_workers,
)
@pl.data_loader
def test_dataloader(self) -> DataLoader:
""" Function that loads the validation set. """
self._test_dataset = self.__retrieve_dataset(train=False, val=False)[0]
return DataLoader(
dataset=self._test_dataset,
batch_size=self.hparams.batch_size,
collate_fn=self.prepare_sample,
num_workers=self.hparams.loader_workers,
)
@classmethod
def add_model_specific_args(
cls, parser: HyperOptArgumentParser
) -> HyperOptArgumentParser:
""" Parser for Estimator specific arguments/hyperparameters.
:param parser: HyperOptArgumentParser obj
Returns:
- updated parser
"""
parser.add_argument(
"--encoder_learning_rate",
default=1e-05,
type=float,
help="Encoder specific learning rate.",
)
parser.add_argument(
"--learning_rate",
default=3e-05,
type=float,
help="Classification head learning rate.",
)
parser.opt_list(
"--nr_frozen_epochs",
default=1,
type=int,
help="Number of epochs we want to keep the encoder model frozen.",
tunable=True,
options=[0, 1, 2, 3, 4, 5],
)
# Data Args:
parser.add_argument(
"--label_set",
default="Social sciences and society;Sports and recreation;Natural sciences;Language and literature;Geography and places;Music;Media and drama;Art and architecture;Warfare;Engineering and technology;Video games;Philosophy and religion;Agriculture, food and drink;History;Mathematics;Miscellaneous",
type=str,
help="Classification labels set.",
)
parser.add_argument(
"--train_csv",
default="data/train_data.csv",
type=str,
help="Path to the file containing the train data.",
)
parser.add_argument(
"--dev_csv",
default="data/valid_data.csv",
type=str,
help="Path to the file containing the dev data.",
)
parser.add_argument(
"--test_csv",
default="data/valid_data.csv",
type=str,
help="Path to the file containing the dev data.",
)
parser.add_argument(
"--loader_workers",
default=8,
type=int,
help="How many subprocesses to use for data loading. 0 means that \
the data will be loaded in the main process.",
)
return parser