-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
292 lines (238 loc) · 12.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
import os
import yaml
from argparse import ArgumentParser
from loguru import logger
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
import torchmetrics
import wandb
import lightning as L
import lightning.pytorch as pl
from pytorch_lightning.loggers import WandbLogger
from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint
from models.lang_models.maria import MariaRoberta
from models.lang_models.politibeto import PolitiBeto
from models.lang_models.xlmt import TwitterXLM
from models.clfs.simpleCLF import SimpleCLF
from dataloader import SpanishTweetsDataModule
from models.utils import concat_embeds
from loss import cross_entropy_loss, accuracy
from utils import DictAsMember
os.environ["TOKENIZERS_PARALLELISM"] = "false"
DEVICE = torch.device(
"cuda:0") if torch.cuda.is_available() else torch.device("cpu")
CLF_DICT = {"simple": [
'gender',
'profession',
'ideology_binary',
'ideology_multiclass'],
"mlcl": ["mlcl"]
}
pl.seed_everything(42, workers=True) # for reproducibility
class SpanishTweetsCLF(pl.LightningModule):
def __init__(self, freeze_lang_model=True, clf_type="simple", hp_path="./clf_hp", bias=False, lr=2.0e-5):
super().__init__()
self.MariaRoberta = MariaRoberta()
self.PolitiBeto = PolitiBeto()
self.TwitterXLM = TwitterXLM()
# classifiers
self.clf_type = clf_type
self.clf_attr = CLF_DICT[clf_type]
self.lr = lr
# TODO: finetune SimpleCLF classifier
if clf_type == "simple":
logger.info("running simple classifier")
for attr in self.clf_attr:
attr_hp_path = os.path.join(hp_path, f"{attr}.yaml")
with open(attr_hp_path) as f:
attr_hp = DictAsMember(yaml.safe_load(f))
logger.info(attr_hp)
setattr(self, f"{attr}_hp", attr_hp)
setattr(self, f"{attr}_precision", torchmetrics.Precision(
num_classes=attr_hp.output_size, average='macro', task="multiclass").to(DEVICE))
setattr(self, f"{attr}_recall", torchmetrics.Recall(
num_classes=attr_hp.output_size, average='macro', task="multiclass").to(DEVICE))
setattr(self, f"{attr}_f1", torchmetrics.classification.MulticlassF1Score(
num_classes=attr_hp.output_size, average='macro', task="multiclass").to(DEVICE))
setattr(self, f"clf_{attr}", SimpleCLF(
attr_name=attr,
output_size=attr_hp.output_size,
bias=bias,
dropout_rate=attr_hp.dropout,
hidden_size=attr_hp.hidden_size,
num_layers=attr_hp.num_layers))
# TODO: add cl classifier and config file for it
else:
raise NotImplementedError
if freeze_lang_model:
for param in self.MariaRoberta.parameters():
param.requires_grad = False
for param in self.PolitiBeto.parameters():
param.requires_grad = False
for param in self.TwitterXLM.parameters():
param.requires_grad = False
def forward(self, x):
ret = {"device": DEVICE, **x}
ret.update(self.MariaRoberta(**ret))
ret.update(self.PolitiBeto(**ret))
ret.update(self.TwitterXLM(**ret))
ret["concated_embeds"] = concat_embeds(**ret)
for attr in self.clf_attr:
ret.update(getattr(self, f'clf_{attr}')(**ret))
return [ret[f"pred_{attr}"] for attr in self.clf_attr]
def training_step(self, batch, batch_idx):
ret = {"device": DEVICE, **batch}
ret.update(self.MariaRoberta(**ret))
ret.update(self.PolitiBeto(**ret))
ret.update(self.TwitterXLM(**ret))
ret["concated_embeds"] = concat_embeds(**ret)
loss = 0
wandb_logger = {}
for attr in self.clf_attr:
ret.update(getattr(self, f'clf_{attr}')(**ret))
attr_loss = cross_entropy_loss(ret[f"pred_{attr}"], ret[attr])
loss += attr_loss
# Calculate and log precision, recall, and F1-score
precision = getattr(self, f'{attr}_precision')(
ret[f"pred_{attr}"], ret[attr])
recall = getattr(self, f'{attr}_recall')(
ret[f"pred_{attr}"], ret[attr])
f1 = getattr(self, f'{attr}_f1')(ret[f"pred_{attr}"], ret[attr])
final_metric = (f1 * precision * recall) / 3
wandb_logger[f"train/{attr}/loss"] = attr_loss
wandb_logger[f"train/{attr}/acc"] = accuracy(
ret[f"pred_{attr}"], ret[attr])
wandb_logger[f"train/{attr}/precision"] = precision
wandb_logger[f"train/{attr}/recall"] = recall
wandb_logger[f"train/{attr}/f1"] = f1
wandb_logger[f"train/{attr}/final_metric"] = final_metric
if self.global_step % 80 == 0:
cm = wandb.plot.confusion_matrix(
y_true=ret[attr].tolist(),
preds=ret[f"pred_{attr}"].argmax(dim=1).tolist(),
class_names=getattr(self, f'{attr}_hp').class_name)
wandb_logger[f"train/{attr}/conf_mat"] = cm
self.logger.experiment.log(wandb_logger)
return loss
def validation_step(self, batch, batch_idx):
ret = {"device": DEVICE, **batch}
ret.update(self.MariaRoberta(**ret))
ret.update(self.PolitiBeto(**ret))
ret.update(self.TwitterXLM(**ret))
ret["concated_embeds"] = concat_embeds(**ret)
loss = 0
total_f1 = 0
total_final_metric = 0
wandb_logger = {}
for attr in self.clf_attr:
ret.update(getattr(self, f'clf_{attr}')(**ret))
attr_loss = cross_entropy_loss(ret[f"pred_{attr}"], ret[attr])
loss += attr_loss
# Calculate and log precision, recall, and F1-score
precision = getattr(self, f'{attr}_precision')(
ret[f"pred_{attr}"], ret[attr])
recall = getattr(self, f'{attr}_recall')(
ret[f"pred_{attr}"], ret[attr])
f1 = getattr(self, f'{attr}_f1')(ret[f"pred_{attr}"], ret[attr])
final_metric = (f1 * precision * recall) / 3
total_final_metric += final_metric
total_f1 += f1
wandb_logger[f"valid/{attr}/loss"] = attr_loss
wandb_logger[f"valid/{attr}/acc"] = accuracy(
ret[f"pred_{attr}"], ret[attr])
wandb_logger[f"valid/{attr}/precision"] = precision
wandb_logger[f"valid/{attr}/recall"] = recall
wandb_logger[f"valid/{attr}/f1"] = f1
wandb_logger[f"valid/{attr}/final_metric"] = final_metric
self.log(f"valid/{attr}/loss", attr_loss)
self.log(f"valid/{attr}/acc", accuracy(
ret[f"pred_{attr}"], ret[attr]))
self.log(f"valid/{attr}/precision", precision)
self.log(f"valid/{attr}/recall", recall)
self.log(f"valid/{attr}/f1", f1)
self.log(f"valid/{attr}/final_metric", final_metric)
if self.global_step % 80 == 0:
cm = wandb.plot.confusion_matrix(
y_true=ret[attr].tolist(),
preds=ret[f"pred_{attr}"].argmax(dim=1).tolist(),
class_names=getattr(self, f'{attr}_hp').class_name)
wandb_logger[f"valid/{attr}/conf_mat"] = cm
average_f1 = total_f1 / len(self.clf_attr)
wandb_logger["valid/average_f1"] = average_f1
self.log("valid/average_f1", average_f1)
avg_final_metric = total_final_metric / len(self.clf_attr)
wandb_logger["valid/average_final_metric"] = avg_final_metric
self.log("valid/average_final_metric", avg_final_metric)
self.logger.experiment.log(wandb_logger)
return loss
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.lr)
return optimizer
def main(hparams):
if hparams.clf == "simple":
model = SpanishTweetsCLF(clf_type="simple",
freeze_lang_model=hparams.freeze,
lr=hparams.learning_rate,
bias=False)
else:
raise NotImplementedError
if hparams.tiny_train:
# path leads to *very* small subset of practise data
train_dataset_path = "data/tiny_data/tiny_cleaned_encoded_train.csv"
val_dataset_path = "data/tiny_data/tiny_cleaned_encoded_development.csv"
logger.info("Using tiny train")
elif hparams.practise_train:
# path leads to practise data
train_dataset_path = "data/practise_data/cleaned/cleaned_encoded_development_train.csv"
val_dataset_path = "data/practise_data/cleaned/cleaned_encoded_development_test.csv"
logger.info("Using practise train")
else:
train_dataset_path = "data/full_data/cleaned/train_clean_encoded.csv"
val_dataset_path = "data/full_data/cleaned/val_clean_encoded.csv"
logger.info("Using full train")
dm = SpanishTweetsDataModule(train_dataset_path,
val_dataset_path,
num_workers=hparams.num_workers,
batch_size=hparams.batch_size)
wandb_logger = WandbLogger(project="spanish-tweets", name=hparams.run_name)
trainer = L.Trainer(callbacks=[
EarlyStopping(monitor="valid/average_final_metric", mode="max", patience=3),
ModelCheckpoint(monitor="valid/average_final_metric", mode="max", save_top_k=3, save_last=False, verbose=True, filename="{epoch}-{valid/average_final_metric:.2f}"),
ModelCheckpoint(monitor="valid/profession/final_metric", mode="max", save_top_k=1, save_last=False, verbose=True, filename="{epoch}-{valid/profession/final_metric:.2f}"),
ModelCheckpoint(monitor="valid/gender/final_metric", mode="max", save_top_k=1, save_last=False, verbose=True, filename="{epoch}-{valid/gender/final_metric:.2f}"),
ModelCheckpoint(monitor="valid/ideology_multiclass/final_metric", mode="max", save_top_k=1, save_last=False, verbose=True, filename="{epoch}-{valid/ideology_multiclass/final_metric:.2f}"),
ModelCheckpoint(monitor="valid/ideology_binary/final_metric", mode="max", save_top_k=1, save_last=False, verbose=True, filename="{epoch}-{valid/ideology_binary/final_metric:.2f}"),],
accelerator=hparams.accelerator,
devices=1,
logger=wandb_logger,
max_epochs=hparams.epochs)
trainer.fit(model, dm, ckpt_path=hparams.path_to_checkpoint)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--accelerator", "-a", default="cpu",
help="Change to GPU if you have one (default: cpu)")
parser.add_argument("--batch-size", "-b", default=8, type=int,
help="Batch size for training (default: from yaml file)")
parser.add_argument("--num_workers", "-w", type=int,
help="Number of workers for dataloader (default: 2)")
parser.add_argument("--epochs", "-e", default=15, type=int,
help="Number of epochs to train (default: from yaml file)")
parser.add_argument("--learning-rate", "-lr",
default=2.0e-5, type=float, help="learning-rate")
parser.add_argument("--run-name", "-n", default=None,
type=str, help="learning-rate")
parser.add_argument("--clf", "-c", type=str, default="simple",
help="Classifier to use (default: simple)")
parser.add_argument("--hp_path", "-hp", type=str,
default="./clf_hp", help="hp files for classifiers")
parser.add_argument("--tiny_train", "-tiny", action="store_true",
help="Use tiny train dataset (default: False)")
parser.add_argument("--practise_train", "-practise", action="store_true",
help="Use tiny train dataset (default: False)")
parser.add_argument("--path_to_checkpoint", "-cp",
help="Path to checkpoint to load (default: None)")
parser.add_argument("--freeze", default=True, type=bool, help="Freeze language model (default: True)")
args = parser.parse_args()
main(args)