-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainingutils.py
324 lines (259 loc) · 9.51 KB
/
trainingutils.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
import torch
from utils import pad
import random
import numpy as np
import time
def prepare_example(example, vocab, device="cpu"):
"""
Map tokens to their IDs for a single example
"""
# vocab returns 0 if the word is not there (i2w[0] = <unk>)
x = torch.tensor([[vocab.w2i.get(t, 0) for t in example.tokens]], dtype=torch.int64)
x = x.to(device)
y = torch.tensor([example.label], dtype=torch.int64)
y = y.to(device)
return x, y
def get_examples(data, shuffle=True, **kwargs):
"""Shuffle data set and return 1 example at a time (until nothing left)"""
if shuffle:
random.shuffle(data) # shuffle training data each epoch
for example in data:
yield example
def batch(states):
"""
Turns a list of states into a single tensor for fast processing.
This function also chunks (splits) each state into a (h, c) pair"""
return torch.cat(states, 0).chunk(2, 1)
def unbatch(state):
"""
Turns a tensor back into a list of states.
First, (h, c) are merged into a single state.
Then the result is split into a list of sentences.
"""
return torch.split(torch.cat(state, 1), 1, 0)
def get_minibatch(data, batch_size=32, shuffle=True):
"""Return minibatches, optional shuffling"""
if shuffle:
print("Shuffling training data")
random.shuffle(data) # shuffle training data each epoch
batch = []
# yield minibatches
for example in data:
batch.append(example)
if len(batch) == batch_size:
yield batch
batch = []
# in case there is something left
if len(batch) > 0:
yield batch
def prepare_minibatch(mb, vocab, device="cpu"):
"""
Minibatch is a list of examples.
This function converts words to IDs and returns
torch tensors to be used as input/targets.
"""
maxlen = max([len(ex.tokens) for ex in mb])
# vocab returns 0 if the word is not there
x = [pad([vocab.w2i.get(t, 0) for t in ex.tokens], maxlen) for ex in mb]
x = torch.tensor(x, dtype=torch.int64).to(device)
y = torch.tensor([ex.label for ex in mb], dtype=torch.int64).to(device)
return x, y
def prepare_treelstm_minibatch(mb, vocab, device):
"""
Returns sentences reversed (last word first)
Returns transitions together with the sentences.
"""
maxlen = max([len(ex.tokens) for ex in mb])
# vocab returns 0 if the word is not there
# NOTE: reversed sequence!
x = torch.tensor(
[pad([vocab.w2i.get(t, 0) for t in ex.tokens], maxlen)[::-1] for ex in mb],
dtype=torch.int64,
).to(device)
y = torch.tensor([ex.label for ex in mb], dtype=torch.int64).to(device)
maxlen_t = max([len(ex.transitions) for ex in mb])
transitions = [pad(ex.transitions, maxlen_t, pad_value=2) for ex in mb]
transitions = np.array(transitions)
transitions = transitions.T # time-major
return (x, transitions), y
def simple_evaluate(model, data, prep_fn=prepare_example, device="cpu", **kwargs):
"""Accuracy of a model on given data set."""
correct = 0
total = 0
model.eval() # disable dropout (explained later)
for example in data:
# convert the example input and label to PyTorch tensors
x, target = prep_fn(example, model.vocab)
# forward pass without backpropagation (no_grad)
# get the output from the neural network for input x
with torch.no_grad():
logits = model(x)
# get the prediction
prediction = logits.argmax(dim=-1)
# add the number of correct predictions to the total correct
correct += (prediction == target).sum().item()
total += 1
return correct, total, correct / float(total)
def evaluate_minibatch(
model,
data,
batch_fn=get_minibatch,
prep_fn=prepare_treelstm_minibatch,
batch_size=32,
**kwargs,
):
"""Accuracy of a model on given data set (using mini-batches)"""
correct = 0
total = 0
model.eval() # disable dropout
for mb in batch_fn(data, batch_size=batch_size, shuffle=False):
x, targets = prep_fn(mb, model.vocab)
with torch.no_grad():
logits = model(x)
predictions = logits.argmax(dim=-1).view(-1)
# add the number of correct predictions to the total correct
correct += (predictions == targets.view(-1)).sum().item()
total += targets.size(0)
return correct, total, correct / float(total)
def train_model(
model,
optimizer,
train_data,
test_data,
dev_data,
num_iterations=10000,
print_every=1000,
eval_every=1000,
batch_fn=get_examples,
prep_fn=prepare_example,
eval_fn=simple_evaluate,
batch_size=1,
eval_batch_size=None,
device="cpu",
model_dir="save/",
**kwargs,
):
"""Train a model."""
iter_i = 0
train_loss = 0.0
print_num = 0
start = time.time()
criterion = torch.nn.CrossEntropyLoss() # loss function
best_eval = 0.0
best_iter = 0
# store train loss and validation accuracy during training
# so we can plot them afterwards
losses = []
accuracies = []
model.to(device)
if eval_batch_size is None:
eval_batch_size = batch_size
while True: # when we run out of examples, shuffle and continue
for batch in batch_fn(train_data, batch_size=batch_size):
# forward pass
model.train()
x, targets = prep_fn(batch, model.vocab)
logits = model(x)
B = targets.size(0) # later we will use B examples per update
# compute cross-entropy loss (our criterion)
# note that the cross entropy loss function computes the softmax for us
loss = criterion(logits.view([B, -1]), targets.view(-1))
train_loss += loss.item()
# backward pass (tip: check the Introduction to PyTorch notebook)
# erase previous gradients
optimizer.zero_grad()
# compute gradients
loss.backward()
# update weights - take a small step in the opposite dir of the gradient
optimizer.step()
print_num += 1
iter_i += 1
# print info
if iter_i % print_every == 0:
print(
"Iter %r: loss=%.4f, time=%.2fs"
% (iter_i, train_loss, time.time() - start)
)
losses.append(train_loss)
print_num = 0
train_loss = 0.0
# evaluate
if iter_i % eval_every == 0:
_, _, accuracy = eval_fn(
model,
dev_data,
batch_size=eval_batch_size,
batch_fn=batch_fn,
prep_fn=prep_fn,
)
accuracies.append(accuracy)
print("iter %r: dev acc=%.4f" % (iter_i, accuracy))
# save best model parameters
if accuracy > best_eval:
print("new highscore")
best_eval = accuracy
best_iter = iter_i
path = f"{model_dir}/{model.__class__.__name__}.pt"
ckpt = {
"state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"best_eval": best_eval,
"best_iter": best_iter,
}
torch.save(ckpt, path)
# done training
if iter_i == num_iterations:
print("Done training")
# evaluate on train, dev, and test with best model
print("Loading best model")
path = "{}.pt".format(model.__class__.__name__)
ckpt = torch.load(model_dir + path)
model.load_state_dict(ckpt["state_dict"])
_, _, train_acc = eval_fn(
model,
train_data,
batch_size=eval_batch_size,
batch_fn=batch_fn,
prep_fn=prep_fn,
)
_, _, dev_acc = eval_fn(
model,
dev_data,
batch_size=eval_batch_size,
batch_fn=batch_fn,
prep_fn=prep_fn,
)
_, _, test_acc = eval_fn(
model,
test_data,
batch_size=eval_batch_size,
batch_fn=batch_fn,
prep_fn=prep_fn,
)
print(
"best model iter {:d}: "
"train acc={:.4f}, dev acc={:.4f}, test acc={:.4f}".format(
best_iter, train_acc, dev_acc, test_acc
)
)
return losses, accuracies
def evaluate(
model,
data,
batch_fn=get_minibatch,
prep_fn=prepare_treelstm_minibatch,
batch_size=16,
):
"""Accuracy of a model on given data set (using mini-batches)"""
correct = 0
total = 0
model.eval() # disable dropout
for mb in batch_fn(data, batch_size=batch_size, shuffle=False):
x, targets = prep_fn(mb, model.vocab)
with torch.no_grad():
logits = model(x)
predictions = logits.argmax(dim=-1).view(-1)
# add the number of correct predictions to the total correct
correct += (predictions == targets.view(-1)).sum().item()
total += targets.size(0)
return correct, total, correct / float(total)