forked from abhishekkrthakur/long-text-token-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
264 lines (220 loc) · 9.87 KB
/
train.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
import argparse
import os
import random
import warnings
import numpy as np
import pandas as pd
import tez
import torch
import torch.nn as nn
from sklearn import metrics
from torch.nn import functional as F
from transformers import AdamW, AutoConfig, AutoModel, AutoTokenizer, get_cosine_schedule_with_warmup
from utils import EarlyStopping, prepare_training_data, target_id_map
warnings.filterwarnings("ignore")
def seed_everything(seed: int):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--fold", type=int, required=True)
parser.add_argument("--model", type=str, required=True)
parser.add_argument("--lr", type=float, required=True)
parser.add_argument("--output", type=str, default="../model", required=False)
parser.add_argument("--input", type=str, default="../input", required=False)
parser.add_argument("--max_len", type=int, default=1024, required=False)
parser.add_argument("--batch_size", type=int, default=8, required=False)
parser.add_argument("--valid_batch_size", type=int, default=8, required=False)
parser.add_argument("--epochs", type=int, default=20, required=False)
parser.add_argument("--accumulation_steps", type=int, default=1, required=False)
return parser.parse_args()
class FeedbackDataset:
def __init__(self, samples, max_len, tokenizer):
self.samples = samples
self.max_len = max_len
self.tokenizer = tokenizer
self.length = len(samples)
def __len__(self):
return self.length
def __getitem__(self, idx):
input_ids = self.samples[idx]["input_ids"]
input_labels = self.samples[idx]["input_labels"]
input_labels = [target_id_map[x] for x in input_labels]
other_label_id = target_id_map["O"]
padding_label_id = target_id_map["PAD"]
# print(input_ids)
# print(input_labels)
# add start token id to the input_ids
input_ids = [self.tokenizer.cls_token_id] + input_ids
input_labels = [other_label_id] + input_labels
if len(input_ids) > self.max_len - 1:
input_ids = input_ids[: self.max_len - 1]
input_labels = input_labels[: self.max_len - 1]
# add end token id to the input_ids
input_ids = input_ids + [self.tokenizer.sep_token_id]
input_labels = input_labels + [other_label_id]
attention_mask = [1] * len(input_ids)
padding_length = self.max_len - len(input_ids)
if padding_length > 0:
if self.tokenizer.padding_side == "right":
input_ids = input_ids + [self.tokenizer.pad_token_id] * padding_length
input_labels = input_labels + [padding_label_id] * padding_length
attention_mask = attention_mask + [0] * padding_length
else:
input_ids = [self.tokenizer.pad_token_id] * padding_length + input_ids
input_labels = [padding_label_id] * padding_length + input_labels
attention_mask = [0] * padding_length + attention_mask
return {
"ids": torch.tensor(input_ids, dtype=torch.long),
"mask": torch.tensor(attention_mask, dtype=torch.long),
"targets": torch.tensor(input_labels, dtype=torch.long),
}
class FeedbackModel(tez.Model):
def __init__(self, model_name, num_train_steps, learning_rate, num_labels, steps_per_epoch):
super().__init__()
self.learning_rate = learning_rate
self.model_name = model_name
self.num_train_steps = num_train_steps
self.num_labels = num_labels
self.steps_per_epoch = steps_per_epoch
self.step_scheduler_after = "batch"
hidden_dropout_prob: float = 0.1
layer_norm_eps: float = 1e-7
config = AutoConfig.from_pretrained(model_name)
config.update(
{
"output_hidden_states": True,
"hidden_dropout_prob": hidden_dropout_prob,
"layer_norm_eps": layer_norm_eps,
"add_pooling_layer": False,
"num_labels": self.num_labels,
}
)
self.transformer = AutoModel.from_pretrained(model_name, config=config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.dropout1 = nn.Dropout(0.1)
self.dropout2 = nn.Dropout(0.2)
self.dropout3 = nn.Dropout(0.3)
self.dropout4 = nn.Dropout(0.4)
self.dropout5 = nn.Dropout(0.5)
self.output = nn.Linear(config.hidden_size, self.num_labels)
def fetch_optimizer(self):
param_optimizer = list(self.named_parameters())
no_decay = ["bias", "LayerNorm.bias"]
optimizer_parameters = [
{
"params": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
"weight_decay": 0.01,
},
{
"params": [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
opt = AdamW(optimizer_parameters, lr=self.learning_rate)
return opt
def fetch_scheduler(self):
sch = get_cosine_schedule_with_warmup(
self.optimizer,
num_warmup_steps=int(0.1 * self.num_train_steps),
num_training_steps=self.num_train_steps,
num_cycles=1,
last_epoch=-1,
)
return sch
def loss(self, outputs, targets, attention_mask):
loss_fct = nn.CrossEntropyLoss()
active_loss = attention_mask.view(-1) == 1
active_logits = outputs.view(-1, self.num_labels)
true_labels = targets.view(-1)
outputs = active_logits.argmax(dim=-1)
idxs = np.where(active_loss.cpu().numpy() == 1)[0]
active_logits = active_logits[idxs]
true_labels = true_labels[idxs].to(torch.long)
loss = loss_fct(active_logits, true_labels)
return loss
def monitor_metrics(self, outputs, targets, attention_mask):
active_loss = (attention_mask.view(-1) == 1).cpu().numpy()
active_logits = outputs.view(-1, self.num_labels)
true_labels = targets.view(-1).cpu().numpy()
outputs = active_logits.argmax(dim=-1).cpu().numpy()
idxs = np.where(active_loss == 1)[0]
f1_score = metrics.f1_score(true_labels[idxs], outputs[idxs], average="macro")
return {"f1": f1_score}
def forward(self, ids, mask, token_type_ids=None, targets=None):
if token_type_ids:
transformer_out = self.transformer(ids, mask, token_type_ids)
else:
transformer_out = self.transformer(ids, mask)
sequence_output = transformer_out.last_hidden_state
sequence_output = self.dropout(sequence_output)
logits1 = self.output(self.dropout1(sequence_output))
logits2 = self.output(self.dropout2(sequence_output))
logits3 = self.output(self.dropout3(sequence_output))
logits4 = self.output(self.dropout4(sequence_output))
logits5 = self.output(self.dropout5(sequence_output))
logits = (logits1 + logits2 + logits3 + logits4 + logits5) / 5
logits = torch.softmax(logits, dim=-1)
loss = 0
if targets is not None:
loss1 = self.loss(logits1, targets, attention_mask=mask)
loss2 = self.loss(logits2, targets, attention_mask=mask)
loss3 = self.loss(logits3, targets, attention_mask=mask)
loss4 = self.loss(logits4, targets, attention_mask=mask)
loss5 = self.loss(logits5, targets, attention_mask=mask)
loss = (loss1 + loss2 + loss3 + loss4 + loss5) / 5
f1_1 = self.monitor_metrics(logits1, targets, attention_mask=mask)["f1"]
f1_2 = self.monitor_metrics(logits2, targets, attention_mask=mask)["f1"]
f1_3 = self.monitor_metrics(logits3, targets, attention_mask=mask)["f1"]
f1_4 = self.monitor_metrics(logits4, targets, attention_mask=mask)["f1"]
f1_5 = self.monitor_metrics(logits5, targets, attention_mask=mask)["f1"]
f1 = (f1_1 + f1_2 + f1_3 + f1_4 + f1_5) / 5
metric = {"f1": f1}
return logits, loss, metric
return logits, loss, {}
if __name__ == "__main__":
NUM_JOBS = 12
args = parse_args()
seed_everything(42)
os.makedirs(args.output, exist_ok=True)
df = pd.read_csv(os.path.join(args.input, "train_folds.csv"))
train_df = df[df["kfold"] != args.fold].reset_index(drop=True)
valid_df = df[df["kfold"] == args.fold].reset_index(drop=True)
tokenizer = AutoTokenizer.from_pretrained(args.model)
training_samples = prepare_training_data(train_df, tokenizer, args, num_jobs=NUM_JOBS)
valid_samples = prepare_training_data(valid_df, tokenizer, args, num_jobs=NUM_JOBS)
train_dataset = FeedbackDataset(training_samples, args.max_len, tokenizer)
num_train_steps = int(len(train_dataset) / args.batch_size / args.accumulation_steps * args.epochs)
print(num_train_steps)
model = FeedbackModel(
model_name=args.model,
num_train_steps=num_train_steps,
learning_rate=args.lr,
num_labels=len(target_id_map) - 1,
steps_per_epoch=len(train_dataset) / args.batch_size,
)
es = EarlyStopping(
model_path=os.path.join(args.output, f"model_{args.fold}.bin"),
valid_df=valid_df,
valid_samples=valid_samples,
batch_size=args.valid_batch_size,
patience=5,
mode="max",
delta=0.001,
save_weights_only=True,
tokenizer=tokenizer,
)
model.fit(
train_dataset,
train_bs=args.batch_size,
device="cuda",
epochs=args.epochs,
callbacks=[es],
fp16=True,
accumulation_steps=args.accumulation_steps,
)