forked from voiler/PopulationBasedTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
62 lines (53 loc) · 2.3 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
import torch
from torch.utils.data import DataLoader
import tqdm
class Trainer:
def __init__(self, model, optimizer, loss_fn=None, train_data=None,
test_data=None, batch_size=None, device=None):
"""Note: Trainer objects don't know about the database."""
self.model = model
self.optimizer = optimizer
self.loss_fn = loss_fn
self.train_data = train_data
self.test_data = test_data
self.batch_size = batch_size
self.task_id = None
self.device = device
def set_id(self, num):
self.task_id = num
def save_checkpoint(self, checkpoint_path):
checkpoint = dict(model_state_dict=self.model.state_dict(),
optim_state_dict=self.optimizer.state_dict(),
batch_size=self.batch_size)
torch.save(checkpoint, checkpoint_path)
def load_checkpoint(self, checkpoint_path):
checkpoint = torch.load(checkpoint_path)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.optimizer.load_state_dict(checkpoint['optim_state_dict'])
self.batch_size = checkpoint['batch_size']
def train(self):
self.model.train()
dataloader = tqdm.tqdm(DataLoader(self.train_data, self.batch_size, True),
desc='Train (task {})'.format(self.task_id),
ncols=80, leave=True)
for x, y in dataloader:
x, y = x.to(self.device), y.to(self.device)
output = self.model(x)
loss = self.loss_fn(output, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def eval(self):
"""Evaluate model on the provided validation or test set."""
self.model.eval()
dataloader = tqdm.tqdm(DataLoader(self.train_data, self.batch_size, True),
desc='Eval (task {})'.format(self.task_id),
ncols=80, leave=True)
correct = 0
for x, y in dataloader:
x, y = x.to(self.device), y.to(self.device)
output = self.model(x)
pred = output.argmax(1)
correct += pred.eq(y).sum().item()
accuracy = 100. * correct / (len(dataloader) * self.batch_size)
return accuracy