-
Notifications
You must be signed in to change notification settings - Fork 7
/
trainer.py
150 lines (106 loc) · 5.24 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
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
import torch
from torch import nn
from torch.optim import Adam
from sklearn.utils.class_weight import compute_class_weight as classweight
from sklearn.metrics import accuracy_score
import numpy as np
from torch.nn import CrossEntropyLoss
import time
class trainer:
def __init__(self, Model, Train_set, Val_set, n_classes):
self.Model = Model
self.compiled = False
self.X_train, self.y_train = Train_set
self.X_val, self.y_val = Val_set
self.tracker = {'train_tracker':[],'val_tracker':[]}
weights = classweight(class_weight="balanced",classes=np.arange(n_classes),y=self.y_train.numpy())
if torch.cuda.is_available():
class_weights = torch.FloatTensor(weights).cuda()
else:
class_weights = torch.FloatTensor(weights)
self.loss_func = CrossEntropyLoss(weight=class_weights)
def compile(self,learning_rate):
self.optimizer = Adam(self.Model.parameters(), lr=learning_rate)
self.compiled = True
def train(self, epochs, batch_size=32, patience=10, directory='model.pt'):
wait = 0
best_model = self.Model
if not self.compiled:
raise Exception("You need to compile an optimizer first before training.")
train_loss_tracker = []
val_loss_tracker = []
trainset = [[self.X_train[i],self.y_train[i]] for i in range(self.X_train.size()[0])]
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True)
valset = [[self.X_val[i],self.y_val[i]] for i in range(self.X_val.size()[0])]
valloader = torch.utils.data.DataLoader(valset, batch_size=batch_size, shuffle=True)
if torch.cuda.is_available():
self.Model.cuda()
for e in range(epochs):
T0 = time.time()
batch_train_loss = []
for data, target in trainloader:
if torch.cuda.is_available():
self.Model.train()
pred = self.Model(data.float().cuda())
self.optimizer.zero_grad()
train_loss = self.loss_func(pred, target.cuda())
train_loss.backward()
self.optimizer.step()
else:
self.Model.train()
pred = self.Model(data.float())
self.optimizer.zero_grad()
train_loss = self.loss_func(pred, target)
train_loss.backward()
self.optimizer.step()
batch_train_loss.append(train_loss)
final_train_loss = torch.mean(torch.tensor(batch_train_loss))
Training_time = time.time()-T0
batch_val_loss = []
with torch.no_grad():
for data, target in valloader:
if torch.cuda.is_available():
pred = self.Model(data.float().cuda())
val_loss = self.loss_func(pred, target.cuda())
batch_val_loss.append(val_loss)
else:
pred = self.Model(data.float())
val_loss = self.loss_func(pred, target)
batch_val_loss.append(val_loss)
final_val_loss = torch.mean(torch.tensor(batch_val_loss))
print("Epoch Number \t: ",e)
print("Train Loss \t:","{:.5f}".format(final_train_loss))
print("Val Loss \t:","{:.5f}".format(final_val_loss))
print("Training Time \t:","{:.5f}".format(Training_time))
print("===================================================================================\n")
if e>patience:
if val_loss.item()<=np.min(val_loss_tracker):
best_model = self.Model
torch.save(self.Model.state_dict(), directory)
wait = 0
else:
wait += 1
else:
torch.save(self.Model.state_dict(), directory)
train_loss_tracker.append(final_train_loss)
val_loss_tracker.append(final_val_loss)
if wait >= patience:
break
self.tracker['train_tracker'] = train_loss_tracker
self.tracker['val_tracker'] = val_loss_tracker
self.Model = best_model
return self.tracker
def predict(X_test):
output = []
testloader = torch.utils.data.DataLoader(X_test, batch_size=32, shuffle=True)
with torch.no_grad():
for data in testloader:
if torch.cuda.is_available():
pred = self.Model.cuda()(data.float().cuda())
pred = list(np.argmax(list(pred.cpu().numpy()), axis=1))
output += pred
else:
pred = self.Model(data.float())
pred = list(np.argmax(list(pred.cpu().numpy()), axis=1))
output += pred
return output