-
Notifications
You must be signed in to change notification settings - Fork 37
/
trainers.py
320 lines (273 loc) · 13.8 KB
/
trainers.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
# coding=utf-8
# Copyright 2021, Duong Nguyen
#
# Licensed under the CECILL-C License;
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.cecill.info
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Boilerplate for training a neural network.
References:
https://github.com/karpathy/minGPT
"""
import os
import math
import logging
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data.dataloader import DataLoader
from torch.nn import functional as F
import utils
from trAISformer import TB_LOG
logger = logging.getLogger(__name__)
@torch.no_grad()
def sample(model,
seqs,
steps,
temperature=1.0,
sample=False,
sample_mode="pos_vicinity",
r_vicinity=20,
top_k=None):
"""
Take a conditoning sequence of AIS observations seq and predict the next observation,
feed the predictions back into the model each time.
"""
max_seqlen = model.get_max_seqlen()
model.eval()
for k in range(steps):
seqs_cond = seqs if seqs.size(1) <= max_seqlen else seqs[:, -max_seqlen:] # crop context if needed
# logits.shape: (batch_size, seq_len, data_size)
logits, _ = model(seqs_cond)
d2inf_pred = torch.zeros((logits.shape[0], 4)).to(seqs.device) + 0.5
# pluck the logits at the final step and scale by temperature
logits = logits[:, -1, :] / temperature # (batch_size, data_size)
lat_logits, lon_logits, sog_logits, cog_logits = \
torch.split(logits, (model.lat_size, model.lon_size, model.sog_size, model.cog_size), dim=-1)
# optionally crop probabilities to only the top k options
if sample_mode in ("pos_vicinity",):
idxs, idxs_uniform = model.to_indexes(seqs_cond[:, -1:, :])
lat_idxs, lon_idxs = idxs_uniform[:, 0, 0:1], idxs_uniform[:, 0, 1:2]
lat_logits = utils.top_k_nearest_idx(lat_logits, lat_idxs, r_vicinity)
lon_logits = utils.top_k_nearest_idx(lon_logits, lon_idxs, r_vicinity)
if top_k is not None:
lat_logits = utils.top_k_logits(lat_logits, top_k)
lon_logits = utils.top_k_logits(lon_logits, top_k)
sog_logits = utils.top_k_logits(sog_logits, top_k)
cog_logits = utils.top_k_logits(cog_logits, top_k)
# apply softmax to convert to probabilities
lat_probs = F.softmax(lat_logits, dim=-1)
lon_probs = F.softmax(lon_logits, dim=-1)
sog_probs = F.softmax(sog_logits, dim=-1)
cog_probs = F.softmax(cog_logits, dim=-1)
# sample from the distribution or take the most likely
if sample:
lat_ix = torch.multinomial(lat_probs, num_samples=1) # (batch_size, 1)
lon_ix = torch.multinomial(lon_probs, num_samples=1)
sog_ix = torch.multinomial(sog_probs, num_samples=1)
cog_ix = torch.multinomial(cog_probs, num_samples=1)
else:
_, lat_ix = torch.topk(lat_probs, k=1, dim=-1)
_, lon_ix = torch.topk(lon_probs, k=1, dim=-1)
_, sog_ix = torch.topk(sog_probs, k=1, dim=-1)
_, cog_ix = torch.topk(cog_probs, k=1, dim=-1)
ix = torch.cat((lat_ix, lon_ix, sog_ix, cog_ix), dim=-1)
# convert to x (range: [0,1))
x_sample = (ix.float() + d2inf_pred) / model.att_sizes
# append to the sequence and continue
seqs = torch.cat((seqs, x_sample.unsqueeze(1)), dim=1)
return seqs
class TrainerConfig:
# optimization parameters
max_epochs = 10
batch_size = 64
learning_rate = 3e-4
betas = (0.9, 0.95)
grad_norm_clip = 1.0
weight_decay = 0.1 # only applied on matmul weights
# learning rate decay params: linear warmup followed by cosine decay to 10% of original
lr_decay = False
warmup_tokens = 375e6 # these two numbers come from the GPT-3 paper, but may not be good defaults elsewhere
final_tokens = 260e9 # (at what point we reach 10% of original LR)
# checkpoint settings
ckpt_path = None
num_workers = 0 # for DataLoader
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
class Trainer:
def __init__(self, model, train_dataset, test_dataset, config, savedir=None, device=torch.device("cpu"), aisdls={},
INIT_SEQLEN=0):
self.train_dataset = train_dataset
self.test_dataset = test_dataset
self.config = config
self.savedir = savedir
self.device = device
self.model = model.to(device)
self.aisdls = aisdls
self.INIT_SEQLEN = INIT_SEQLEN
def save_checkpoint(self, best_epoch):
# DataParallel wrappers keep raw model object in .module attribute
raw_model = self.model.module if hasattr(self.model, "module") else self.model
# logging.info("saving %s", self.config.ckpt_path)
logging.info(f"Best epoch: {best_epoch:03d}, saving model to {self.config.ckpt_path}")
torch.save(raw_model.state_dict(), self.config.ckpt_path)
def train(self):
model, config, aisdls, INIT_SEQLEN, = self.model, self.config, self.aisdls, self.INIT_SEQLEN
raw_model = model.module if hasattr(self.model, "module") else model
optimizer = raw_model.configure_optimizers(config)
if model.mode in ("gridcont_gridsin", "gridcont_gridsigmoid", "gridcont2_gridsigmoid",):
return_loss_tuple = True
else:
return_loss_tuple = False
def run_epoch(split, epoch=0):
is_train = split == 'Training'
model.train(is_train)
data = self.train_dataset if is_train else self.test_dataset
loader = DataLoader(data, shuffle=True, pin_memory=True,
batch_size=config.batch_size,
num_workers=config.num_workers)
losses = []
n_batches = len(loader)
pbar = tqdm(enumerate(loader), total=len(loader)) if is_train else enumerate(loader)
d_loss, d_reg_loss, d_n = 0, 0, 0
for it, (seqs, masks, seqlens, mmsis, time_starts) in pbar:
# place data on the correct device
seqs = seqs.to(self.device)
masks = masks[:, :-1].to(self.device)
# forward the model
with torch.set_grad_enabled(is_train):
if return_loss_tuple:
logits, loss, loss_tuple = model(seqs,
masks=masks,
with_targets=True,
return_loss_tuple=return_loss_tuple)
else:
logits, loss = model(seqs, masks=masks, with_targets=True)
loss = loss.mean() # collapse all losses if they are scattered on multiple gpus
losses.append(loss.item())
d_loss += loss.item() * seqs.shape[0]
if return_loss_tuple:
reg_loss = loss_tuple[-1]
reg_loss = reg_loss.mean()
d_reg_loss += reg_loss.item() * seqs.shape[0]
d_n += seqs.shape[0]
if is_train:
# backprop and update the parameters
model.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), config.grad_norm_clip)
optimizer.step()
# decay the learning rate based on our progress
if config.lr_decay:
self.tokens += (
seqs >= 0).sum() # number of tokens processed this step (i.e. label is not -100)
if self.tokens < config.warmup_tokens:
# linear warmup
lr_mult = float(self.tokens) / float(max(1, config.warmup_tokens))
else:
# cosine learning rate decay
progress = float(self.tokens - config.warmup_tokens) / float(
max(1, config.final_tokens - config.warmup_tokens))
lr_mult = max(0.1, 0.5 * (1.0 + math.cos(math.pi * progress)))
lr = config.learning_rate * lr_mult
for param_group in optimizer.param_groups:
param_group['lr'] = lr
else:
lr = config.learning_rate
# report progress
pbar.set_description(f"epoch {epoch + 1} iter {it}: loss {loss.item():.5f}. lr {lr:e}")
# tb logging
if TB_LOG:
tb.add_scalar("loss",
loss.item(),
epoch * n_batches + it)
tb.add_scalar("lr",
lr,
epoch * n_batches + it)
for name, params in model.head.named_parameters():
tb.add_histogram(f"head.{name}", params, epoch * n_batches + it)
tb.add_histogram(f"head.{name}.grad", params.grad, epoch * n_batches + it)
if model.mode in ("gridcont_real",):
for name, params in model.res_pred.named_parameters():
tb.add_histogram(f"res_pred.{name}", params, epoch * n_batches + it)
tb.add_histogram(f"res_pred.{name}.grad", params.grad, epoch * n_batches + it)
if is_train:
if return_loss_tuple:
logging.info(
f"{split}, epoch {epoch + 1}, loss {d_loss / d_n:.5f}, {d_reg_loss / d_n:.5f}, lr {lr:e}.")
else:
logging.info(f"{split}, epoch {epoch + 1}, loss {d_loss / d_n:.5f}, lr {lr:e}.")
else:
if return_loss_tuple:
logging.info(f"{split}, epoch {epoch + 1}, loss {d_loss / d_n:.5f}.")
else:
logging.info(f"{split}, epoch {epoch + 1}, loss {d_loss / d_n:.5f}.")
if not is_train:
test_loss = float(np.mean(losses))
# logging.info("test loss: %f", test_loss)
return test_loss
best_loss = float('inf')
self.tokens = 0 # counter used for learning rate decay
best_epoch = 0
for epoch in range(config.max_epochs):
run_epoch('Training', epoch=epoch)
if self.test_dataset is not None:
test_loss = run_epoch('Valid', epoch=epoch)
# supports early stopping based on the test loss, or just save always if no test set is provided
good_model = self.test_dataset is None or test_loss < best_loss
if self.config.ckpt_path is not None and good_model:
best_loss = test_loss
best_epoch = epoch
self.save_checkpoint(best_epoch + 1)
## SAMPLE AND PLOT
# ==========================================================================================
# ==========================================================================================
raw_model = model.module if hasattr(self.model, "module") else model
seqs, masks, seqlens, mmsis, time_starts = iter(aisdls["test"]).next()
n_plots = 7
init_seqlen = INIT_SEQLEN
seqs_init = seqs[:n_plots, :init_seqlen, :].to(self.device)
preds = sample(raw_model,
seqs_init,
96 - init_seqlen,
temperature=1.0,
sample=True,
sample_mode=self.config.sample_mode,
r_vicinity=self.config.r_vicinity,
top_k=self.config.top_k)
img_path = os.path.join(self.savedir, f'epoch_{epoch + 1:03d}.jpg')
plt.figure(figsize=(9, 6), dpi=150)
cmap = plt.cm.get_cmap("jet")
preds_np = preds.detach().cpu().numpy()
inputs_np = seqs.detach().cpu().numpy()
for idx in range(n_plots):
c = cmap(float(idx) / (n_plots))
try:
seqlen = seqlens[idx].item()
except:
continue
plt.plot(inputs_np[idx][:init_seqlen, 1], inputs_np[idx][:init_seqlen, 0], color=c)
plt.plot(inputs_np[idx][:init_seqlen, 1], inputs_np[idx][:init_seqlen, 0], "o", markersize=3, color=c)
plt.plot(inputs_np[idx][:seqlen, 1], inputs_np[idx][:seqlen, 0], linestyle="-.", color=c)
plt.plot(preds_np[idx][init_seqlen:, 1], preds_np[idx][init_seqlen:, 0], "x", markersize=4, color=c)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.savefig(img_path, dpi=150)
plt.close()
# Final state
raw_model = self.model.module if hasattr(self.model, "module") else self.model
# logging.info("saving %s", self.config.ckpt_path)
logging.info(f"Last epoch: {epoch:03d}, saving model to {self.config.ckpt_path}")
save_path = self.config.ckpt_path.replace("model.pt", f"model_{epoch + 1:03d}.pt")
torch.save(raw_model.state_dict(), save_path)