forked from elijahcole/sinr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
89 lines (75 loc) · 2.99 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
import os
import numpy as np
import torch
import setup
import losses
import models
import datasets
import utils
class Trainer():
def __init__(self, model, train_loader, params):
self.params = params
# define loaders:
self.train_loader = train_loader
# define model:
self.model = model
# define important objects:
self.compute_loss = losses.get_loss_function(params)
self.encode_location = self.train_loader.dataset.enc.encode
# define optimization objects:
self.optimizer = torch.optim.Adam(self.model.parameters(), params['lr'])
self.lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=params['lr_decay'])
def train_one_epoch(self):
self.model.train()
# initialise run stats
running_loss = 0.0
samples_processed = 0
steps_trained = 0
for _, batch in enumerate(self.train_loader):
# reset gradients:
self.optimizer.zero_grad()
# compute loss:
batch_loss = self.compute_loss(batch, self.model, self.params, self.encode_location)
# backwards pass:
batch_loss.backward()
# update parameters:
self.optimizer.step()
# track and report:
running_loss += float(batch_loss.item())
steps_trained += 1
samples_processed += batch[0].shape[0]
if steps_trained % self.params['log_frequency'] == 0:
print(f'[{samples_processed}/{len(self.train_loader.dataset)}] loss: {np.around(running_loss / self.params["log_frequency"], 4)}')
running_loss = 0.0
# update learning rate according to schedule:
self.lr_scheduler.step()
def save_model(self):
save_path = os.path.join(self.params['save_path'], 'model.pt')
op_state = {'state_dict': self.model.state_dict(), 'params' : self.params}
torch.save(op_state, save_path)
def launch_training_run(ovr):
# setup:
params = setup.get_default_params_train(ovr)
params['save_path'] = os.path.join(params['save_base'], params['experiment_name'])
if params['timestamp']:
params['save_path'] = params['save_path'] + '_' + utils.get_time_stamp()
os.makedirs(params['save_path'], exist_ok=False)
# data:
train_dataset = datasets.get_train_data(params)
params['input_dim'] = train_dataset.input_dim
# params['num_classes'] = train_dataset.num_classes # unconditional
# params['class_to_taxa'] = train_dataset.class_to_taxa # unconditional
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=params['batch_size'],
shuffle=True,
num_workers=4)
# model:
model = models.get_model(params)
model = model.to(params['device'])
# train:
trainer = Trainer(model, train_loader, params)
for epoch in range(0, params['num_epochs']):
print(f'epoch {epoch+1}')
trainer.train_one_epoch()
trainer.save_model()