-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetune-BERT.py
329 lines (285 loc) · 9.89 KB
/
finetune-BERT.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
import torch
import random
import argparse
import pandas as pd
from glob import glob
from pathlib import Path
import torch.nn as nn
from torch import tensor
from scipy.special import softmax
from transformers import Trainer, TrainingArguments
from transformers.integrations import TensorBoardCallback
from torch.utils.data import Dataset
from transformers.modeling_outputs import SequenceClassifierOutput
from transformers import (
AutoTokenizer,
BertPreTrainedModel,
BertModel,
)
from constants import (
DIALECTS,
DIALECTS_INDEX_INVERTED_MAP,
DIALECTS_INDEX_MAP,
COUNTRY_TO_REGION,
COUNTRIES_IN_SAME_REGION_int,
)
SEED = 42
random.seed(SEED)
torch.manual_seed(SEED)
def transform_input(tokenizer, filenames, alpha=0, beta=0):
"""Tokenize the input text and return the features in the HF dict format.
Args:
tokenizer: The model's tokenizer.
filenames: A list of tsv files of NADI.
Returns:
Features in the form of a HF dict format
"""
dfs = [pd.read_csv(filename, sep="\t") for filename in filenames]
df = pd.concat(dfs)
df["label_int"] = [DIALECTS_INDEX_MAP[dialect] for dialect in df["label"]]
df["countries_in_same_region_int"] = df["label_int"].apply(
lambda i: COUNTRIES_IN_SAME_REGION_int[i]
)
features_dict = tokenizer(
df["text"].tolist(),
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
)
n_dialects = len(DIALECTS)
# Allow the dataset not to have a label
if "label" in df.columns:
# A 2D vector of hard labels
if beta == 0 and alpha == 0:
features_dict["labels"] = tensor(
[
[i == DIALECTS_INDEX_MAP[dialect] for i in range(n_dialects)]
for dialect in df["label"].tolist()
],
dtype=torch.float,
)
else:
features_dict["labels"] = tensor(
[
[
(
1
- ((n_dialects - 1) * alpha / n_dialects)
- (
(len(row["countries_in_same_region_int"]) - 1)
* beta
/ len(row["countries_in_same_region_int"])
if len(row["countries_in_same_region_int"])
else 0
)
)
if i == row["label_int"]
else (
beta / len(row["countries_in_same_region_int"])
+ alpha / n_dialects
)
if i in row["countries_in_same_region_int"]
else (alpha / n_dialects)
for i in range(n_dialects)
]
for _, row in df.iterrows()
],
dtype=torch.float,
)
return features_dict
class NADIDataset(Dataset):
def __init__(self, tokenizer, dataset_file_path, alpha=0, beta=0):
super(NADIDataset).__init__()
dataset_filenames = [f for f in glob(dataset_file_path)]
self.alpha = 0
self.beta = 0
self.features_dict = transform_input(
tokenizer, dataset_filenames, alpha=alpha, beta=beta
)
self.input_keys = self.features_dict.keys()
def __len__(self):
return self.features_dict["labels"].shape[0]
def __getitem__(self, idx):
return {k: self.features_dict[k][idx, :] for k in self.input_keys}
class CustomBertForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.config = config
self.bert = BertModel(config)
classifier_dropout = (
config.classifier_dropout
if config.classifier_dropout is not None
else config.hidden_dropout_prob
)
self.dropout = nn.Dropout(classifier_dropout)
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
# Initialize weights and apply final processing
self.post_init()
def forward(
self,
input_ids=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss_function = nn.CrossEntropyLoss(reduction="mean")
loss = loss_function(input=logits, target=labels)
return SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
def model_init(model_name):
"""This function is required for ensuring the reproducibility."""
model = CustomBertForSequenceClassification.from_pretrained(
model_name, num_labels=len(DIALECTS)
)
return model
def main():
parser = argparse.ArgumentParser(
"Train a classification model for Dialect Identification (DI)."
)
subparsers = parser.add_subparsers()
training_subparser = subparsers.add_parser(
"train",
help="Train the DI model.",
)
training_subparser.set_defaults(mode="train")
training_subparser.add_argument(
"--train",
"-d",
required=True,
help="The filename of the training dataset (allows for glob).",
)
training_subparser.add_argument(
"--dev",
required=False,
help="The filename of the development dataset (allows for glob).",
)
training_subparser.add_argument(
"-model_name",
"-m",
default="UBC-NLP/MARBERT",
help="The model name.",
)
training_subparser.add_argument(
"-label_smoothing_factor",
"-alpha",
default=0,
help="The label smoothing factor in [0, 1].",
)
training_subparser.add_argument(
"-o",
required=True,
help="The output directory.",
)
prediction_subparser = subparsers.add_parser(
"predict",
help="Generate predictions using a fine-tuned model model.",
)
prediction_subparser.set_defaults(mode="predict")
prediction_subparser.add_argument(
"-d",
required=True,
help="The path of the dataset.",
)
prediction_subparser.add_argument(
"-model_name",
"-m",
default="UBC-NLP/MARBERT",
help="The model name.",
)
prediction_subparser.add_argument(
"-p",
required=True,
help="The trained model path.",
)
prediction_subparser.add_argument(
"-o",
required=True,
help="The output filename.",
)
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
if args.mode == "train":
# TODO: Update the training arguments
NO_STEPS = 1000
BATCH_SIZE = 32
ALPHA = 0.3
BETA = 0.3
training_args = TrainingArguments(
output_dir=args.o,
save_strategy="epoch",
evaluation_strategy="epoch",
eval_steps=NO_STEPS,
load_best_model_at_end=True,
# TODO: Avoid hard-coding this field
metric_for_best_model="eval_NADI2023_dev_loss",
greater_is_better=False,
per_device_train_batch_size=BATCH_SIZE,
seed=SEED,
data_seed=SEED,
label_smoothing_factor=float(args.label_smoothing_factor),
)
train_dataset = NADIDataset(tokenizer, args.train, alpha=ALPHA, beta=BETA)
eval_dataset_filenames = glob(args.dev)
eval_dataset = {
Path(filename).name.split(".")[0]: NADIDataset(
tokenizer, filename, alpha=ALPHA, beta=BETA
)
for filename in eval_dataset_filenames
}
trainer = Trainer(
model_init=lambda: model_init(args.model_name),
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
callbacks=[TensorBoardCallback],
)
trainer.train()
else:
# Load from checkpoint
model = CustomBertForSequenceClassification.from_pretrained(
args.p, num_labels=len(DIALECTS)
)
test_dataset = NADIDataset(tokenizer, args.d)
trainer = Trainer(model, args=None, train_dataset=None)
predictions = trainer.predict(test_dataset)
prediction_labels = predictions.predictions.argmax(1).tolist()
prediction_probs = softmax(predictions.predictions, axis=1)
prediction_probs = prediction_probs.max(axis=1).tolist()
prediction_labels = predictions.predictions.argmax(1).tolist()
prediction_strs = [DIALECTS_INDEX_INVERTED_MAP[l] for l in prediction_labels]
df = pd.read_csv(args.d, sep="\t")
df["prediction"] = prediction_strs
df["prediction_macro"] = [COUNTRY_TO_REGION[pred] for pred in prediction_strs]
df["prediction_prob"] = prediction_probs
df.to_csv(args.o, index=False, sep="\t")
if __name__ == "__main__":
main()