-
Notifications
You must be signed in to change notification settings - Fork 186
/
training.py
221 lines (191 loc) · 7.19 KB
/
training.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
""" module providing basic training utilities"""
import os
from os.path import join
from time import time
from datetime import timedelta
from itertools import starmap
from cytoolz import curry, reduce
import torch
from torch.nn.utils import clip_grad_norm_
from torch.optim.lr_scheduler import ReduceLROnPlateau
import tensorboardX
def get_basic_grad_fn(net, clip_grad, max_grad=1e2):
def f():
grad_norm = clip_grad_norm_(
[p for p in net.parameters() if p.requires_grad], clip_grad)
grad_norm = grad_norm.item()
if max_grad is not None and grad_norm >= max_grad:
print('WARNING: Exploding Gradients {:.2f}'.format(grad_norm))
grad_norm = max_grad
grad_log = {}
grad_log['grad_norm'] = grad_norm
return grad_log
return f
@curry
def compute_loss(net, criterion, fw_args, loss_args):
loss = criterion(*((net(*fw_args),) + loss_args))
return loss
@curry
def val_step(loss_step, fw_args, loss_args):
loss = loss_step(fw_args, loss_args)
return loss.size(0), loss.sum().item()
@curry
def basic_validate(net, criterion, val_batches):
print('running validation ... ', end='')
net.eval()
start = time()
with torch.no_grad():
validate_fn = val_step(compute_loss(net, criterion))
n_data, tot_loss = reduce(
lambda a, b: (a[0]+b[0], a[1]+b[1]),
starmap(validate_fn, val_batches),
(0, 0)
)
val_loss = tot_loss / n_data
print(
'validation finished in {} '.format(
timedelta(seconds=int(time()-start)))
)
print('validation loss: {:.4f} ... '.format(val_loss))
return {'loss': val_loss}
class BasicPipeline(object):
def __init__(self, name, net,
train_batcher, val_batcher, batch_size,
val_fn, criterion, optim, grad_fn=None):
self.name = name
self._net = net
self._train_batcher = train_batcher
self._val_batcher = val_batcher
self._criterion = criterion
self._opt = optim
# grad_fn is calleble without input args that modifyies gradient
# it should return a dictionary of logging values
self._grad_fn = grad_fn
self._val_fn = val_fn
self._n_epoch = 0 # epoch not very useful?
self._batch_size = batch_size
self._batches = self.batches()
def batches(self):
while True:
for fw_args, bw_args in self._train_batcher(self._batch_size):
yield fw_args, bw_args
self._n_epoch += 1
def get_loss_args(self, net_out, bw_args):
if isinstance(net_out, tuple):
loss_args = net_out + bw_args
else:
loss_args = (net_out, ) + bw_args
return loss_args
def train_step(self):
# forward pass of model
self._net.train()
fw_args, bw_args = next(self._batches)
net_out = self._net(*fw_args)
# get logs and output for logging, backward
log_dict = {}
loss_args = self.get_loss_args(net_out, bw_args)
# backward and update ( and optional gradient monitoring )
loss = self._criterion(*loss_args).mean()
loss.backward()
log_dict['loss'] = loss.item()
if self._grad_fn is not None:
log_dict.update(self._grad_fn())
self._opt.step()
self._net.zero_grad()
return log_dict
def validate(self):
return self._val_fn(self._val_batcher(self._batch_size))
def checkpoint(self, save_path, step, val_metric=None):
save_dict = {}
if val_metric is not None:
name = 'ckpt-{:6f}-{}'.format(val_metric, step)
save_dict['val_metric'] = val_metric
else:
name = 'ckpt-{}'.format(step)
save_dict['state_dict'] = self._net.state_dict()
save_dict['optimizer'] = self._opt.state_dict()
torch.save(save_dict, join(save_path, name))
def terminate(self):
self._train_batcher.terminate()
self._val_batcher.terminate()
class BasicTrainer(object):
""" Basic trainer with minimal function and early stopping"""
def __init__(self, pipeline, save_dir, ckpt_freq, patience,
scheduler=None, val_mode='loss'):
assert isinstance(pipeline, BasicPipeline)
assert val_mode in ['loss', 'score']
self._pipeline = pipeline
self._save_dir = save_dir
self._logger = tensorboardX.SummaryWriter(join(save_dir, 'log'))
os.makedirs(join(save_dir, 'ckpt'))
self._ckpt_freq = ckpt_freq
self._patience = patience
self._sched = scheduler
self._val_mode = val_mode
self._step = 0
self._running_loss = None
# state vars for early stopping
self._current_p = 0
self._best_val = None
def log(self, log_dict):
loss = log_dict['loss'] if 'loss' in log_dict else log_dict['reward']
if self._running_loss is not None:
self._running_loss = 0.99*self._running_loss + 0.01*loss
else:
self._running_loss = loss
print('train step: {}, {}: {:.4f}\r'.format(
self._step,
'loss' if 'loss' in log_dict else 'reward',
self._running_loss), end='')
for key, value in log_dict.items():
self._logger.add_scalar(
'{}_{}'.format(key, self._pipeline.name), value, self._step)
def validate(self):
print()
val_log = self._pipeline.validate()
for key, value in val_log.items():
self._logger.add_scalar(
'val_{}_{}'.format(key, self._pipeline.name),
value, self._step
)
if 'reward' in val_log:
val_metric = val_log['reward']
else:
val_metric = (val_log['loss'] if self._val_mode == 'loss'
else val_log['score'])
return val_metric
def checkpoint(self):
val_metric = self.validate()
self._pipeline.checkpoint(
join(self._save_dir, 'ckpt'), self._step, val_metric)
if isinstance(self._sched, ReduceLROnPlateau):
self._sched.step(val_metric)
else:
self._sched.step()
stop = self.check_stop(val_metric)
return stop
def check_stop(self, val_metric):
if self._best_val is None:
self._best_val = val_metric
elif ((val_metric < self._best_val and self._val_mode == 'loss')
or (val_metric > self._best_val and self._val_mode == 'score')):
self._current_p = 0
self._best_val = val_metric
else:
self._current_p += 1
return self._current_p >= self._patience
def train(self):
try:
start = time()
print('Start training')
while True:
log_dict = self._pipeline.train_step()
self._step += 1
self.log(log_dict)
if self._step % self._ckpt_freq == 0:
stop = self.checkpoint()
if stop:
break
print('Training finised in ', timedelta(seconds=time()-start))
finally:
self._pipeline.terminate()