-
Notifications
You must be signed in to change notification settings - Fork 1
/
training_functions.py
116 lines (92 loc) · 3.53 KB
/
training_functions.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
import torch
import numpy as np
class Network:
"""
Network class that takes pytorch model, learning rate and device as input.
Parameters
----------
loss -> loss function
opt -> optimizer
lr_scheduler -> learning rate scheduler which reduces lr when validation plateu
Functions
---------
- train_step -> takes train_loader and train model for one epoch, and update train accuracy and loss
- validation_step -> takes val_loader and update validation accuracy and loss
- test_step -> takes test_loader and return averaged accuracy
- save_all -> saves traning, validation accuracy and loss to an '.npy' file
"""
def __init__(
self,
model : torch.nn.Module,
learning_rate : float,
device : str,
):
self.model = model
self.lr = learning_rate
self.loss = torch.nn.CrossEntropyLoss()
self.opt = torch.optim.Adam(self.model.parameters(), lr = self.lr)
self.train_loss = []
self.val_loss = []
self.train_acc = []
self.val_acc = []
self.device = device
self.lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
self.opt,
mode='min',
patience=3,
factor=0.5,
min_lr=1e-6,
verbose=True
)
def train_step(self, dataset):
self.model.train()
batch_loss = []
batch_acc = []
for batch in dataset:
inputs = batch[0].to(self.device)
targets = batch[1].to(self.device)
self.opt.zero_grad()
outputs = self.model(inputs)
loss = self.loss(outputs, targets)
loss.backward()
self.opt.step()
batch_loss.append(loss.item())
batch_acc.append(self.batch_accuracy(outputs,targets))
self.train_loss.append(np.mean(batch_loss))
self.train_acc.append(np.mean(batch_acc))
def validation_step(self, dataset):
self.model.eval()
batch_loss = []
batch_acc = []
with torch.no_grad():
for batch in dataset:
inputs = batch[0].to(self.device)
targets = batch[1].to(self.device)
outputs = self.model(inputs)
loss = self.loss(outputs, targets)
batch_loss.append(loss.item())
batch_acc.append(self.batch_accuracy(outputs,targets))
self.val_loss.append(np.mean(batch_loss))
self.val_acc.append(np.mean(batch_acc))
self.lr_scheduler.step(self.val_loss[-1])
def test_step(self, dataset):
self.model.eval()
batch_acc = []
with torch.no_grad():
for batch in dataset:
inputs = batch[0].to(self.device)
targets = batch[1].to(self.device)
outputs = self.model(inputs)
batch_acc.append(self.batch_accuracy(outputs,targets))
print("Accuracy : ", np.mean(batch_acc), "%")
def batch_accuracy(self, output, target):
# output shape: [batch, target]
output = nn.functional.softmax(output, dim=1)
output = output.argmax(1)
acc = torch.sum(output==target) / output.shape[0]
return acc.cpu()*100
def save_all(self, name):
np.save("val_acc{}.npy".format(name), self.val_acc)
np.save("train_acc{}.npy".format(name), self.train_acc)
np.save("val_loss{}.npy".format(name), self.val_loss)
np.save("train_loss{}.npy".format(name), self.train_loss)