-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
213 lines (183 loc) · 8.04 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
import os
import numpy
import torch
from torch import nn
from dl4nlp import tasks, options, utils, optim
from dl4nlp.data import iterators
from dl4nlp.eval.f1_measure import get_f1_score
from dl4nlp.models.checkpoint_utils import load_model_state, save_state
# This is required for registering the models
from dl4nlp.models.cnn import CNNTagger
from dl4nlp.models.lstm import LSTMTagger
from dl4nlp.models.transformer import Transformer2
from dl4nlp.models.transformer_att import Transformer
from dl4nlp.models.modelutils.utils import contextwin
from dl4nlp.options import add_dataset_args, get_parser, add_model_args, add_optimization_args, add_checkpoint_args, \
add_distributed_training_args
from dl4nlp.logger import LogManager
logger = LogManager().logger
def prepare_sequence(seq, to_ix, ctx=None, use_cuda=False):
idxs = [to_ix[w] for w in seq]
if ctx:
idxs = contextwin(idxs, ctx, pad_id=0)
if use_cuda:
return torch.tensor(idxs, dtype=torch.long).cuda()
return torch.tensor(idxs, dtype=torch.long)
def get_data_parser(default_task='translation'):
parser = get_parser('Trainer', default_task)
add_dataset_args(parser, train=True)
add_distributed_training_args(parser)
add_model_args(parser)
add_optimization_args(parser)
add_checkpoint_args(parser)
return parser
def get_train_iterator(task, args, epoch, combine=True):
"""Return an EpochBatchIterator over the training set for a given epoch."""
print('| loading train data for epoch {}'.format(epoch))
task.load_dataset(args.train_subset, epoch=epoch, combine=combine)
return task.get_batch_iterator(
dataset=task.dataset(args.train_subset),
max_tokens=args.max_tokens,
max_sentences=args.max_sentences,
epoch=epoch,
)
def get_validation_iterator(task, args, epoch, combine=False):
"""Return an EpochBatchIterator over the training set for a given epoch."""
task.load_dataset(args.valid_subset, epoch=epoch, combine=combine)
return task.get_batch_iterator(
dataset=task.dataset(args.valid_subset),
max_tokens=args.max_tokens,
max_sentences=args.max_sentences,
epoch=epoch,
)
def prepare_sample(sample, use_cuda=False):
if sample is None or len(sample) == 0:
return None
if use_cuda:
return torch.tensor(sample, dtype=torch.long).cuda()
return torch.tensor(sample, dtype=torch.long)
parser = get_data_parser()
args = options.parse_args_and_arch(parser)
task = tasks.setup_task(args)
train_iter = get_train_iterator(task, args, epoch=0, combine=True)
itr = train_iter.next_epoch_itr(
shuffle=(train_iter.epoch >= args.curriculum),
)
CONTEXT=5
model = task.build_model(args)
# loss_function = nn.NLLLoss()
# CNN Training
loss_function = nn.CrossEntropyLoss()
optimizer = optim.build_optimizer(args, model.parameters())
use_cuda = torch.cuda.is_available()
print(use_cuda)
# use_cuda=False
if use_cuda:
model.cuda()
# optimizer.cuda()
# See what the scores are before training
# Note that element i,j of the output is the score for tag j for word i.
# Here we don't need to train, so the code is wrapped in torch.no_grad()
# with torch.no_grad():
# modeldir="transformer-models"
# modeldir="lstm-models"
modeldir="gru-models"
# modeldir="cnn-models"
if not os.path.exists(modeldir):
os.mkdir(modeldir)
checkpoint_last = 'checkpoint_last.pt'
checkpoint_best = "checkpoint_best.pt"
# See what the scores are after training
def get_accuracy_scores(eval=False):
data_iterator = get_validation_iterator(task, args, epoch=0, combine=True).next_epoch_itr(shuffle=False)
data_iterator = iterators.GroupedIterator(data_iterator, 1)
if eval:
start_epoch = load_model_state(os.path.join(modeldir, checkpoint_best), model)
with torch.no_grad():
hypothesis = list()
reference = list()
for i, samples in enumerate(data_iterator):
for j, sample in enumerate(samples):
net_input = prepare_sample(contextwin(sample['net_input']['src_tokens'].tolist()[0], CONTEXT,
pad_id=task.tgt_dict.pad()),
use_cuda)
# print(**sample['net_input'])
tag_scores = model(net_input)
tag_scores = tag_scores.view(-1, tag_scores.size(-1))
if use_cuda:
target = sample['target'].view(-1).cuda()
else:
target = sample['target'].view(-1)
_, predicted = torch.max(tag_scores, dim=1)
hypothesis.extend(predicted.tolist())
reference.extend(target.tolist())
return get_f1_score(reference, hypothesis)
training = True
# train with early stopping on validation set
best_f1 = -numpy.inf
training_options = dict()
if training:
start_epoch = load_model_state(os.path.join(modeldir, checkpoint_last), model)
for epoch in range(100): # again, normally you would NOT do 300 epochs, it is toy data
training_options["ce"] = epoch
itr = train_iter.next_epoch_itr(shuffle=(train_iter.epoch >= args.curriculum))
itr = iterators.GroupedIterator(itr, 1)
model.zero_grad()
epoch_loss = 0
for i, samples in enumerate(itr):
for j, sample in enumerate(samples):
net_input = prepare_sample(
contextwin(l=sample['net_input']['src_tokens'].tolist()[0],
win=CONTEXT,
pad_id=task.tgt_dict.pad()
),
use_cuda=use_cuda
)
# tag_scores = model(**sample['net_input'])
tag_scores = model(net_input)
tag_scores = tag_scores.view(-1, tag_scores.size(-1))
if use_cuda:
target = sample['target'].view(-1).cuda()
else:
target = sample['target'].view(-1)
loss = loss_function(tag_scores, target)
epoch_loss += loss
loss.backward()
optimizer.step()
print("epoch {0} loss={1}".format(epoch, epoch_loss))
validation = get_accuracy_scores()
checkpoint = "checkpoint" + str(epoch) + ".pt"
save_state(os.path.join(modeldir, checkpoint), model, loss_function, optimizer, epoch)
save_state(os.path.join(modeldir, checkpoint_last), model, loss_function, optimizer, epoch)
if validation[1] > best_f1:
logger.info('NEW BEST: epoch' + str(epoch) + ', valid F1=' + str(validation[1]))
save_state(os.path.join(modeldir, checkpoint_best), model, loss_function, optimizer, epoch)
training_options["be"] = epoch
best_f1 = validation[1]
# Break if no improvement in 10 epochs
if abs(training_options['be'] - training_options['ce']) >= 10:
break
logger.info('BEST RESULT: epoch' + str(training_options['be']) + ', valid F1=' + str(best_f1) + ', final checkpoint-' + checkpoint_best)
print(get_accuracy_scores(eval=True))
# for epoch in range(300): # again, normally you would NOT do 300 epochs, it is toy data
# for sentence, tags in training_data:
# # Step 1. Remember that Pytorch accumulates gradients.
# # We need to clear them out before each instance
# model.zero_grad()
#
# # Step 2. Get our inputs ready for the network, that is, turn them into
# # Tensors of word indices.
# sentence_in = prepare_sequence(sentence, word_to_ix, CONTEXT, use_cuda=use_cuda)
# # sentence_in = prepare_sequence(sentence, word_to_ix, use_cuda=use_cuda)
# targets = prepare_sequence(tags, tag_to_ix, use_cuda=use_cuda)
#
# # Step 3. Run our forward pass.
# tag_scores = model(sentence_in)
# print(tag_scores)
#
# # Step 4. Compute the loss, gradients, and update the parameters by
# # calling optimizer.step()
# loss = loss_function(tag_scores, targets)
# print(loss)
# loss.backward()
# optimizer.step()