-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.py
274 lines (226 loc) · 10.7 KB
/
trainer.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
import os
import time
from functools import partial
import torch
from typing import Union, Optional, Text, Tuple
from torch.utils.data import Dataset, DataLoader
from torch.utils.tensorboard import SummaryWriter
import abc
class Trainer(object):
def name(self) -> Text:
raise NotImplementedError
def save(self, model: torch.nn.Module = None, optimizer=None, epoch: Optional[int] = None):
"""save model state dict into model path"""
checkpoint = {"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"epoch": epoch}
path_checkpoint = f"models/{self.name()}-checkpoint_{epoch}_epoch.pkl"
torch.save(checkpoint, path_checkpoint)
class TextClassifizerTrainer(Trainer):
def name(self) -> Text:
return "text_classifizer"
def __init__(
self, model: torch.nn.Module = None,
args: Optional[Tuple] = None,
train_dataloader: DataLoader = None,
eval_dataloader: DataLoader = None,
epochs: Optional[int] = 30,
learning_rate: Optional[float] = 1e-5,
device: Optional[Text] = "cpu"
):
self.writer = SummaryWriter(
f'logs/text-classifier-B-{train_dataloader.batch_size}-E{epochs}-L{learning_rate}-{time.time()}')
self.writer.flush()
if model is None:
raise RuntimeError("`Trainer` requires a `model` ")
self.epochs = epochs
self.learning_rate = learning_rate
self.model = model
self.train_dataloader = train_dataloader
self.eval_dataloader = eval_dataloader
self.args = args
self.device = device
self.model.to(device)
def train(self):
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(self.model.parameters(), lr=self.learning_rate, weight_decay=0.1)
for epoch in range(1, self.epochs + 1):
epoch_start_time = time.time()
self.train_loop(epoch, loss_fn, optimizer)
accu_val, loss = self.eval_loop(epoch, loss_fn)
print('-' * 59)
print('| end of epoch {:3d} | time: {:5.2f}s | '
'valid accuracy {:8.3f} '
'valid loss {:8.3f} '
.format(epoch,
time.time() - epoch_start_time,
accu_val, loss))
print('-' * 59)
self.save(model=self.model, optimizer=optimizer, epoch=epoch)
def train_loop(self, epoch, loss_fn, optimizer):
self.model.train()
total_acc, total_count = 0, 0
for batch, data in enumerate(self.train_dataloader):
y = data["labels"].to(self.device)
token = data["token"]
input_ids = token["input_ids"].squeeze(1).to(self.device)
attention_mask = token["attention_mask"].squeeze(1).to(self.device)
token_type_ids = token["token_type_ids"].squeeze(1).to(self.device)
# Compute prediction and loss
pred = self.model(input_ids, attention_mask, token_type_ids)
loss = loss_fn(pred, y)
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
current_acc = (pred.argmax(1) == y).sum().item()
current_count = y.size(0)
loss, current = loss.item(), batch * len(token)
total_acc += current_acc
total_count += current_count
# ...log the running loss
self.writer.add_scalar('training loss',
loss,
(epoch - 1) * len(self.train_dataloader) + batch)
# ...log a Matplotlib Figure showing the model's predictions on a
# random mini-batch
# ...log the running loss
self.writer.add_scalar('training acc',
current_acc / current_count,
(epoch - 1) * len(self.train_dataloader) + batch)
print('| epoch {:3d} | {:5d}/{:5d} batches '
'| accuracy {:8.3f}'
'| loss {:8.3f}'
.format(epoch, batch, len(self.train_dataloader),
current_acc / current_count, loss))
def eval_loop(self, epoch, loss_fn):
self.model.eval()
total_acc, total_count = 0, 0
loss = 0
with torch.no_grad():
for batch, data in enumerate(self.eval_dataloader):
y = data["labels"].to(self.device)
token = data["token"]
input_ids = token["input_ids"].squeeze(1).to(self.device)
attention_mask = token["attention_mask"].squeeze(1).to(self.device)
token_type_ids = token["token_type_ids"].squeeze(1).to(self.device)
# Compute prediction and loss
pred = self.model(input_ids, attention_mask, token_type_ids)
loss = loss_fn(pred, y)
loss, current = loss.item(), batch * len(token)
current_acc = (pred.argmax(1) == y).sum().item()
current_count = y.size(0)
total_acc += current_acc
total_count += current_count
# ...log the running loss
self.writer.add_scalar('eval loss',
loss,
(epoch - 1) * len(self.eval_dataloader) + batch)
self.writer.add_scalar('eval acc',
current_acc / current_count,
(epoch - 1) * len(self.eval_dataloader) + batch)
return total_acc / total_count, loss
class SequenceLabelTrainer(Trainer):
def name(self) -> Text:
return "sequence-label"
def __init__(
self, model: torch.nn.Module = None,
args: Optional[Tuple] = None,
train_dataloader: DataLoader = None,
eval_dataloader: DataLoader = None,
epochs: Optional[int] = 30,
learning_rate: Optional[float] = 1e-5,
device: Optional[Text] = "cpu",
padding_tag:Optional[int]=0
):
self.writer = SummaryWriter(
f'logs/sequence-label-B-{train_dataloader.batch_size}-E{epochs}-L{learning_rate}-{time.time()}')
self.writer.flush()
if model is None:
raise RuntimeError("`Trainer` requires a `model` ")
self.epochs = epochs
self.learning_rate = learning_rate
self.model = model
self.train_dataloader = train_dataloader
self.eval_dataloader = eval_dataloader
self.args = args
self.device = device
self.model.to(self.device)
self.padding_tag = padding_tag
def train(self):
optimizer = torch.optim.Adam(self.model.parameters(), lr=self.learning_rate, weight_decay=0.1)
for epoch in range(1, self.epochs + 1):
epoch_start_time = time.time()
self.train_loop(epoch, optimizer)
accu_val, loss = self.eval_loop(epoch)
print('-' * 59)
print('| end of epoch {:3d} | time: {:5.2f}s | '
'valid accuracy {:8.3f} '
'valid loss {:8.3f} '
.format(epoch,
time.time() - epoch_start_time,
accu_val, loss))
self.save(model=self.model, optimizer=optimizer, epoch=epoch)
print('-' * 59)
def train_loop(self, epoch, optimizer):
self.model.train()
for batch, data in enumerate(self.train_dataloader):
y = data["labels"].to(self.device)
token = data["token"]
input_ids = token["input_ids"].squeeze(1).to(self.device)
attention_mask = token["attention_mask"].squeeze(1).to(self.device)
token_type_ids = token["token_type_ids"].squeeze(1).to(self.device)
# Compute prediction and loss
y_pred = self.model(input_ids, attention_mask, token_type_ids)
loss = self.model.loss(input_ids, attention_mask, token_type_ids, y)
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
padding_count = (y_pred == self.padding_tag).sum()
current_acc = (y_pred == y).sum().item() - padding_count
current = y.size(0) * y.size(1) - padding_count
loss = loss.item()
# ...log the running loss
self.writer.add_scalar('training loss',
loss,
(epoch - 1) * len(self.train_dataloader) + batch)
# ...log a Matplotlib Figure showing the model's predictions on a
# random mini-batch
# ...log the running loss
self.writer.add_scalar('training acc',
current_acc / current,
(epoch - 1) * len(self.train_dataloader) + batch)
print('| epoch {:3d} | {:5d}/{:5d} batches '
'| accuracy {:8.3f}'
'| loss {:8.3f}'
.format(epoch, batch, len(self.train_dataloader),
current_acc / current, loss))
def eval_loop(self, epoch):
self.model.eval()
total_acc, total_count = 0, 0
loss = 0
with torch.no_grad():
for batch, data in enumerate(self.eval_dataloader):
y = data["labels"].to(self.device)
token = data["token"]
input_ids = token["input_ids"].squeeze(1).to(self.device)
attention_mask = token["attention_mask"].squeeze(1).to(self.device)
token_type_ids = token["token_type_ids"].squeeze(1).to(self.device)
# Compute prediction and loss
y_pred = self.model(input_ids, attention_mask, token_type_ids)
loss = self.model.loss(input_ids, attention_mask, token_type_ids, y)
padding_count = (y_pred == self.padding_tag).sum()
current_acc = (y_pred == y).sum().item() - padding_count
current = y.size(0) * y.size(1) - padding_count
loss = loss.item()
total_acc += current_acc
total_count += current
# ...log the running loss
self.writer.add_scalar('eval loss',
loss,
(epoch - 1) * len(self.eval_dataloader) + batch)
self.writer.add_scalar('eval acc',
current_acc / current,
(epoch - 1) * len(self.eval_dataloader) + batch)
return total_acc / total_count, loss