-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_cicero2.py
234 lines (183 loc) · 8.04 KB
/
train_cicero2.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
import json
import time
import random
import pickle
import gc, os, sys
import numpy as np
import pandas as pd
from tqdm import tqdm
from pathlib import Path
from datetime import datetime
from argparse import ArgumentParser
import wandb
import torch
import torch.nn as nn
from torch.optim import AdamW
from torch.utils.data import Dataset, DataLoader
from models import Model
from transformers import get_linear_schedule_with_warmup
from transformers.trainer_pt_utils import get_parameter_names
from transformers.optimization import Adafactor, get_scheduler
from sklearn.metrics import accuracy_score, f1_score
class CICERO2Dataset(Dataset):
def __init__(self, f, shuffle):
content, labels = [], []
x = open(f).readlines()
if shuffle:
random.shuffle(x)
for line in x:
instance = json.loads(line)
context = instance["context"]
choices = [instance["choice" + str(k)] for k in range(4)]
l = instance["label"]
for k, c in enumerate(choices):
content.append("{} \\n choice: {}".format(context, c))
if k == l:
labels.append(1)
else:
labels.append(0)
self.content, self.labels = content, labels
def __len__(self):
return len(self.content)
def __getitem__(self, index):
s1, s2 = self.content[index], self.labels[index]
return s1, s2
def collate_fn(self, data):
dat = pd.DataFrame(data)
return [dat[i].tolist() for i in dat]
def configure_dataloaders(train_batch_size=16, eval_batch_size=16, shuffle=False):
"Prepare dataloaders"
train_dataset = CICERO2Dataset("data/cicero2/mcq_train.json", True)
train_loader = DataLoader(train_dataset, shuffle=shuffle, batch_size=train_batch_size, collate_fn=train_dataset.collate_fn)
val_dataset = CICERO2Dataset("data/cicero2/mcq_val.json", False)
val_loader = DataLoader(val_dataset, shuffle=False, batch_size=eval_batch_size, collate_fn=val_dataset.collate_fn)
test_dataset = CICERO2Dataset("data/cicero2/mcq_test.json", False)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=eval_batch_size, collate_fn=val_dataset.collate_fn)
return train_loader, val_loader, test_loader
def configure_optimizer(model, args):
"""Prepare optimizer and schedule (linear warmup and decay)"""
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": args.wd,
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.lr, eps=args.adam_epsilon)
return optimizer
def configure_scheduler(optimizer, num_training_steps, args):
"Prepare scheduler"
warmup_steps = (
args.warmup_steps
if args.warmup_steps > 0
else math.ceil(num_training_steps * args.warmup_ratio)
)
lr_scheduler = get_scheduler(
args.lr_scheduler_type,
optimizer,
num_warmup_steps=warmup_steps,
num_training_steps=num_training_steps,
)
return lr_scheduler
def train_or_eval_model(model, dataloader, optimizer=None, split="Train"):
losses, preds, preds_cls, labels_cls, = [], [], [], []
if split=="Train":
model.train()
else:
model.eval()
for batch in tqdm(dataloader, leave=False):
if split=="Train":
optimizer.zero_grad()
content, l_cls = batch
loss, p, p_cls = model(batch)
preds.append(p)
preds_cls.append(p_cls)
labels_cls.append(l_cls)
if split=="Train":
loss.backward()
optimizer.step()
losses.append(loss.item())
avg_loss = round(np.mean(losses), 4)
all_preds_cls = [item for sublist in preds_cls for item in sublist]
all_labels_cls = [item for sublist in labels_cls for item in sublist]
acc = round(accuracy_score(all_labels_cls, all_preds_cls), 4)
f1 = round(f1_score(all_labels_cls, all_preds_cls, average="macro"), 4)
instance_preds = [item for sublist in preds for item in sublist]
instance_labels = np.array(all_labels_cls).reshape(-1, args.num_choices).argmax(1)
instance_acc = round(accuracy_score(instance_labels, instance_preds), 4)
return avg_loss, acc, instance_acc, f1
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--lr", type=float, default=3e-6, help="Learning rate for transformers.")
parser.add_argument("--wd", default=0.0, type=float, help="Weight decay for transformers.")
parser.add_argument("--warm-up-steps", type=int, default=0, help="Warm up steps.")
parser.add_argument("--adam-epsilon", default=1e-8, type=float, help="Epsilon for AdamW optimizer.")
parser.add_argument("--bs", type=int, default=8, help="Batch size.")
parser.add_argument("--eval-bs", type=int, default=8, help="Batch size.")
parser.add_argument("--epochs", type=int, default=8, help="Number of epochs.")
parser.add_argument("--name", default="roberta-large", help="Which model.")
parser.add_argument('--shuffle', action='store_true', default=False, help="Shuffle train data such that positive and negative \
sequences of the same question are not necessarily in the same batch.")
global args
args = parser.parse_args()
print(args)
train_batch_size = args.bs
eval_batch_size = args.eval_bs
epochs = args.epochs
name = args.name
shuffle = args.shuffle
num_choices = 4
vars(args)["num_choices"] = num_choices
assert eval_batch_size%num_choices == 0, "Eval batch size should be a multiple of num choices, which is 4 for CICERO2"
model = Model(
name=name,
num_choices=num_choices
).cuda()
sep_token = model.tokenizer.sep_token
optimizer = configure_optimizer(model, args)
if "/" in name:
sp = name[name.index("/")+1:]
else:
sp = name
exp_id = str(int(time.time()))
vars(args)["exp_id"] = exp_id
rs = "Acc: {}"
path = "saved/cicero2/" + exp_id + "/" + name.replace("/", "-")
Path("saved/cicero2/" + exp_id + "/").mkdir(parents=True, exist_ok=True)
fname = "saved/cicero2/" + exp_id + "/" + "args.txt"
f = open(fname, "a")
f.write(str(args) + "\n\n")
f.close()
Path("results/cicero2/").mkdir(parents=True, exist_ok=True)
lf_name = "results/cicero2/" + name.replace("/", "-") + ".txt"
lf = open(lf_name, "a")
lf.write(str(args) + "\n\n")
lf.close()
for e in range(epochs):
train_loader, val_loader, test_loader = configure_dataloaders(
train_batch_size, eval_batch_size, shuffle
)
train_loss, train_acc, _, train_f1 = train_or_eval_model(model, train_loader, optimizer, "Train")
val_loss, val_acc, val_ins_acc, val_f1 = train_or_eval_model(model, val_loader, split="Val")
test_loss, test_acc, test_ins_acc, test_f1 = train_or_eval_model(model, test_loader, split="Test")
x = "Epoch {}: Loss: Train {}; Val {}; Test {}".format(e+1, train_loss, val_loss, test_loss)
y1 = "Classification Acc: Train {}; Val {}; Test {}".format(train_acc, val_acc, test_acc)
y2 = "Classification Macro F1: Train {}; Val {}; Test {}".format(train_f1, val_f1, test_f1)
z = "Instance Acc: Val {}; Test {}".format(val_ins_acc, test_ins_acc)
print (x)
print (y1)
print (y2)
print (z)
lf = open(lf_name, "a")
lf.write(x + "\n" + y1 + "\n" + y2 + "\n" + z + "\n\n")
lf.close()
f = open(fname, "a")
f.write(x + "\n" + y1 + "\n" + y2 + "\n" + z + "\n\n")
f.close()
lf = open(lf_name, "a")
lf.write("-"*100 + "\n")
lf.close()