forked from endrikacupaj/LASAGNE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
229 lines (188 loc) · 8.16 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
import os
import sys
import time
import random
import logging
import torch
import numpy as np
import torch.optim
import torch.nn as nn
from pathlib import Path
from args import get_parser
from model import LASAGNE
from dataset import CSQADataset
from torchtext.data import BucketIterator
from utils import (NoamOpt, AverageMeter,
SingleTaskLoss, MultiTaskLoss,
save_checkpoint, init_weights)
# import constants
from constants import *
# set logger
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p',
level=logging.INFO,
handlers=[
logging.FileHandler(f'{args.path_results}/train_{args.task}.log', 'w'),
logging.StreamHandler()
])
logger = logging.getLogger(__name__)
# set a seed value
random.seed(args.seed)
np.random.seed(args.seed)
if torch.cuda.is_available():
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
def main():
# load data
dataset = CSQADataset()
vocabs = dataset.get_vocabs()
train_data, val_data, _ = dataset.get_data()
# load model
model = LASAGNE(vocabs).to(DEVICE)
# initialize model weights
init_weights(model)
logger.info(f'The model has {sum(p.numel() for p in model.parameters() if p.requires_grad):,} trainable parameters')
# define loss function (criterion)
criterion = {
LOGICAL_FORM: SingleTaskLoss,
NER: SingleTaskLoss,
COREF: SingleTaskLoss,
GRAPH: SingleTaskLoss,
MULTITASK: MultiTaskLoss
}[args.task](ignore_index=vocabs[LOGICAL_FORM].stoi[PAD_TOKEN])
single_task_loss = SingleTaskLoss(ignore_index=vocabs[LOGICAL_FORM].stoi[PAD_TOKEN])
# define optimizer
optimizer = NoamOpt(torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))
if args.resume:
if os.path.isfile(args.resume):
logger.info(f"=> loading checkpoint '{args.resume}''")
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint[EPOCH]
best_val = checkpoint[BEST_VAL]
model.load_state_dict(checkpoint[STATE_DICT])
optimizer.optimizer.load_state_dict(checkpoint[OPTIMIZER])
logger.info(f"=> loaded checkpoint '{args.resume}' (epoch {checkpoint[EPOCH]})")
else:
logger.info(f"=> no checkpoint found at '{args.resume}'")
best_val = float('inf')
else:
best_val = float('inf')
# prepare training and validation loader
train_loader, val_loader = BucketIterator.splits((train_data, val_data),
batch_size=args.batch_size,
sort_within_batch=False,
sort_key=lambda x: len(x.input),
device=DEVICE)
logger.info('Loaders prepared.')
logger.info(f"Training data: {len(train_data.examples)}")
logger.info(f"Validation data: {len(val_data.examples)}")
logger.info(f'Question example: {train_data.examples[0].input}')
logger.info(f'Logical form example: {train_data.examples[0].logical_form}')
logger.info(f"Unique tokens in input vocabulary: {len(vocabs[INPUT])}")
logger.info(f"Unique tokens in logical form vocabulary: {len(vocabs[LOGICAL_FORM])}")
logger.info(f"Unique tokens in ner vocabulary: {len(vocabs[NER])}")
logger.info(f"Unique tokens in coref vocabulary: {len(vocabs[COREF])}")
logger.info(f"Number of nodes in the graph: {len(vocabs[GRAPH])}")
logger.info(f'Batch: {args.batch_size}')
logger.info(f'Epochs: {args.epochs}')
# run epochs
for epoch in range(args.start_epoch, args.epochs):
# train for one epoch
train(train_loader, model, vocabs, criterion, optimizer, epoch)
# evaluate on validation set
if (epoch+1) % args.valfreq == 0:
val_loss = validate(val_loader, model, vocabs, criterion, single_task_loss)
# if val_loss < best_val:
best_val = min(val_loss, best_val) # log every validation step
save_checkpoint({
EPOCH: epoch + 1,
STATE_DICT: model.state_dict(),
BEST_VAL: best_val,
OPTIMIZER: optimizer.optimizer.state_dict(),
CURR_VAL: val_loss})
logger.info(f'* Val loss: {val_loss:.4f}')
def train(train_loader, model, vocabs, criterion, optimizer, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
# switch to train mode
model.train()
end = time.time()
batch_progress_old = -1
for i, batch in enumerate(train_loader):
# get inputs
input = batch.input
logical_form = batch.logical_form
ner = batch.ner
coref = batch.coref
graph = batch.graph
# compute output
output = model(input, logical_form[:, :-1])
# prepare targets
target = {
LOGICAL_FORM: logical_form[:, 1:].contiguous().view(-1), # (batch_size * trg_len)
NER: ner.contiguous().view(-1),
COREF: coref.contiguous().view(-1),
GRAPH: graph[:, 1:].contiguous().view(-1)
}
# compute loss
loss = criterion(output, target) if args.task == MULTITASK else criterion(output[args.task], target[args.task])
# record loss
losses.update(loss.data, input.size(0))
# compute gradient and do Adam step
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
batch_progress = int(((i + 1) / len(train_loader)) * 100) # percentage
if batch_progress > batch_progress_old:
logger.info(f'Epoch: {epoch+1} - Train loss: {losses.val:.4f} ({losses.avg:.4f}) - Batch: {batch_progress:02d}% - Time: {batch_time.sum:0.2f}s')
batch_progress_old = batch_progress
def validate(val_loader, model, vocabs, criterion, single_task_loss):
losses = AverageMeter()
# record individual losses
losses_lf = AverageMeter()
losses_ner = AverageMeter()
losses_coref = AverageMeter()
losses_graph = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
for _, batch in enumerate(val_loader):
# get inputs
input = batch.input
logical_form = batch.logical_form
ner = batch.ner
coref = batch.coref
graph = batch.graph
# compute output
output = model(input, logical_form[:, :-1])
# prepare targets
target = {
LOGICAL_FORM: logical_form[:, 1:].contiguous().view(-1), # (batch_size * trg_len)
NER: ner.contiguous().view(-1),
COREF: coref.contiguous().view(-1),
GRAPH: graph[:, 1:].contiguous().view(-1)
}
# compute loss
loss = criterion(output, target) if args.task == MULTITASK else criterion(output[args.task], target[args.task])
# compute individual losses
loss_lf = single_task_loss(output[LOGICAL_FORM], target[LOGICAL_FORM])
loss_ner = single_task_loss(output[NER], target[NER])
loss_coref = single_task_loss(output[COREF], target[COREF])
loss_graph = single_task_loss(output[GRAPH], target[GRAPH])
# record loss
losses.update(loss.detach(), input.size(0))
# record individual losses
losses_lf.update(loss_lf.detach(), input.size(0))
losses_ner.update(loss_ner.detach(), input.size(0))
losses_coref.update(loss_coref.detach(), input.size(0))
losses_graph.update(loss_graph.detach(), input.size(0))
logger.info(f"Val losses:: LF: {losses_lf.avg} | NER: {losses_ner.avg} | COREF: {losses_coref.avg} | "
f"GRAPH: {losses_graph.avg}")
return losses.avg
if __name__ == '__main__':
main()