-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.py
349 lines (291 loc) · 11.2 KB
/
prototype.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
import argparse, time, torch, os, logging, warnings, sys
import random
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import StepLR
from transformers import AdamW
from models.sc_model import MultiTaskModel
from samplers.pt_sampler import TaskSampler
from learners.pt_learner import PtLearner
from losses import CPELoss
from utils.data import CorpusNLI
from utils.datapath import loc, get_loc
from utils.seed import seed_everything
from utils.logger import Logger
logging.getLogger("transformers.tokenization_utils").setLevel(logging.ERROR)
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
parser.add_argument("--meta_lr", type=float, default=2e-5, help="Meta learning rate")
parser.add_argument("--dropout", type=float, default=0.1, help="Dropout probability")
parser.add_argument("--hidden_dims", type=int, default=768, help="")
parser.add_argument(
"--lambda_1", type=float, default=1.0, help="DCE Coefficient in loss function"
)
parser.add_argument(
"--lambda_2", type=float, default=0.5, help="CE Coefficient in loss function"
)
parser.add_argument(
"--temp_scale",
type=float,
default=0.2,
help="Temperature scale for DCE in loss function",
)
# bert-base-multilingual-cased
# xlm-roberta-base
parser.add_argument(
"--model_name",
type=str,
default="xlm-roberta-base",
help="name of the pretrained model",
)
parser.add_argument(
"--local_model", action="store_true", help="use local pretrained model"
)
parser.add_argument("--grad_clip", type=float, default=5.0)
parser.add_argument("--sc_labels", type=int, default=3, help="NLI labels count")
parser.add_argument("--sc_batch_size", type=int, default=32, help="NLI batch size")
# ---------------
parser.add_argument("--epochs", type=int, default=5, help="iterations")
parser.add_argument("--start_epoch", type=int, default=0, help="start iterations from")
parser.add_argument("--ways", type=int, default=3, help="number of ways")
parser.add_argument("--shot", type=int, default=4, help="number of shots")
parser.add_argument("--query_num", type=int, default=4, help="number of queries")
parser.add_argument(
"--target_shot", type=int, default=0, help="number of target queries"
)
parser.add_argument("--meta_iteration", type=int, default=3000, help="")
# ---------------
parser.add_argument("--seed", type=int, default=42, help="seed for numpy and pytorch")
parser.add_argument(
"--log_interval",
type=int,
default=200,
help="Print after every log_interval batches",
)
parser.add_argument("--data_dir", type=str, default="data/", help="directory of data")
parser.add_argument("--save", type=str, default="saved/", help="")
parser.add_argument("--load", type=str, default="", help="")
parser.add_argument("--log_file", type=str, default="main_output.txt", help="")
parser.add_argument("--meta_tasks", type=str, default="sc_en")
parser.add_argument("--target_task", type=str, default="")
parser.add_argument("--queue_len", default=8, type=int)
parser.add_argument("--num_workers", type=int, default=0, help="")
parser.add_argument("--pin_memory", action="store_true", help="")
# Optimizer
parser.add_argument(
"--weight_decay", default=0.0, type=float, help="Weight decay if we apply some."
)
parser.add_argument(
"--adam_epsilon", default=1e-8, type=float, help="Epsilon for Adam optimizer."
)
# Scheduler options
parser.add_argument("--scheduler", action="store_true", help="Use scheduler")
parser.add_argument(
"--step_size", default=3000, type=int, help="Step size for scheduler"
)
parser.add_argument(
"--last_step", default=0, type=int, help="Last step of the scheduler"
)
parser.add_argument(
"--gamma", default=0.1, type=float, help="Multiplicative factor of the scheduler"
)
args = parser.parse_args()
if not os.path.exists(args.save):
os.makedirs(args.save)
sys.stdout = Logger(os.path.join(args.save, args.log_file))
print(args)
print("target tasks: ", args.target_task)
task_types = args.meta_tasks.split(",")
list_of_tasks = []
for tt in loc["train"].keys():
if tt[:2] in task_types:
list_of_tasks.append(tt)
for tt in task_types:
if "_" in tt:
list_of_tasks.append(tt)
list_of_tasks = list(set(list_of_tasks))
print("support tasks: ", list_of_tasks)
def evaluate(model, data, device):
with torch.no_grad():
total_loss = 0.0
for batch in data:
output = model.forward(batch)
data_labels = batch["label"].to(device)
loss = F.cross_entropy(output[2], data_labels, reduction="none")
loss = loss.detach().mean().item()
total_loss += loss
total_loss /= len(data)
return total_loss
def evaluateMeta(model, dev_loaders, device):
loss_dict = {}
total_loss = 0
tasks = [args.target_task] if args.target_task != "" else list_of_tasks
model.eval()
for i, task in enumerate(tasks):
loss = evaluate(model, dev_loaders[i], device)
loss_dict[task] = loss
total_loss += loss
return loss_dict, total_loss
def main():
seed_everything(args.seed)
# Prepare train and validation dataloaders
train_loaders = []
dev_loaders = []
for task in list_of_tasks:
train_corpus = CorpusNLI(
get_loc("train", task, args.data_dir),
model_name=args.model_name,
local_files_only=args.local_model,
)
train_sampler = TaskSampler(
train_corpus,
n_way=args.ways,
n_shot=args.shot,
n_query=args.query_num,
n_tasks=args.meta_iteration,
)
train_loader = DataLoader(
train_corpus,
batch_sampler=train_sampler,
num_workers=args.num_workers,
pin_memory=args.pin_memory,
collate_fn=train_sampler.episodic_collate_fn,
shuffle=False,
)
train_loaders.append(train_loader)
if args.target_task == "":
dev_corpus = CorpusNLI(
get_loc("dev", task, args.data_dir),
model_name=args.model_name,
local_files_only=args.local_model,
)
dev_loader = DataLoader(
dev_corpus, batch_size=args.sc_batch_size, pin_memory=args.pin_memory
)
dev_loaders.append(dev_loader)
if args.target_task != "":
### == target dataset ==============
trg_train_corpus = CorpusNLI(
get_loc("train", args.target_task, args.data_dir),
model_name=args.model_name,
local_files_only=args.local_model,
)
trg_dev_corpus = CorpusNLI(
get_loc("dev", args.target_task, args.data_dir),
model_name=args.model_name,
local_files_only=args.local_model,
)
trg_train_sampler = TaskSampler(
trg_train_corpus,
n_way=args.ways,
n_shot=0,
n_query=args.target_shot,
n_tasks=args.meta_iteration,
)
trg_train_loader = DataLoader(
trg_train_corpus,
batch_sampler=trg_train_sampler,
num_workers=args.num_workers,
pin_memory=args.pin_memory,
collate_fn=trg_train_sampler.episodic_collate_fn,
)
trg_dev_loader = DataLoader(
trg_dev_corpus, batch_size=args.sc_batch_size, pin_memory=args.pin_memory
)
dev_loaders = [trg_dev_loader]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Model
if args.load != "":
print(f"loading model {args.load}...")
model = torch.load(args.load)
else:
model = MultiTaskModel(args).to(device)
# Optimizer
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.weight_decay,
"lr": args.meta_lr,
},
{
"params": [
p
for n, p in model.named_parameters()
if any(nd in n for nd in no_decay)
],
"weight_decay": 0.0,
"lr": args.meta_lr,
},
]
optim = AdamW(optimizer_grouped_parameters, lr=args.meta_lr, eps=args.adam_epsilon)
# Scheduler
scheduler = StepLR(
optim,
step_size=args.step_size,
gamma=args.gamma,
last_epoch=args.last_step - 1,
)
criterion = CPELoss(device, args)
## == training ======
log_interval_time = time.time()
min_task_losses = {
"sc": float("inf"),
"qa": float("inf"),
}
pt_learner = PtLearner(criterion, device)
for epoch in range(args.start_epoch, args.epochs):
print(f"======================= Epoch {epoch} =======================")
train_loss = 0.0
train_loader_iterations = [iter(train_loader) for train_loader in train_loaders]
if args.target_task != "":
trg_train_loader_iteration = iter(trg_train_loader)
for iteration in range(args.meta_iteration):
# == Data preparation ===========
queue = [next(trainloader) for trainloader in train_loader_iterations]
if args.queue_len < len(train_loader_iterations):
queue = random.sample(queue, args.queue_len)
trg_queue = []
if args.target_task != "":
trg_queue = [next(trg_train_loader_iteration)]
## == train ===================
loss = pt_learner.train(model, queue, trg_queue, optim, iteration, args)
train_loss += loss
## == validation ==============
if (iteration + 1) % args.log_interval == 0:
total_loss = train_loss / args.log_interval
train_loss = 0.0
# evalute on val_dataset
val_loss_dict, val_loss_total = evaluateMeta(
model, dev_loaders, device=device
)
loss_per_task = {}
for task in val_loss_dict.keys():
if task[:2] in loss_per_task.keys():
loss_per_task[task[:2]] = (
loss_per_task[task[:2]] + val_loss_dict[task]
)
else:
loss_per_task[task[:2]] = val_loss_dict[task]
for task in loss_per_task.keys():
if loss_per_task[task] < min_task_losses[task]:
print("Saving " + task + " Model")
torch.save(
model, os.path.join(args.save, "model_" + task + ".pt"),
)
min_task_losses[task] = loss_per_task[task]
print(
f"Time: {time.time() - log_interval_time:.4f}, Step: {iteration + 1}, Train Loss: {total_loss:.4f}, Val Loss: {val_loss_total:.4f}"
)
log_interval_time = time.time()
total_loss = 0
if args.scheduler:
scheduler.step()
print("Saving new last model...")
torch.save(model, os.path.join(args.save, "model_last.pt"))
if __name__ == "__main__":
main()