-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trainer.py
161 lines (128 loc) · 5.76 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
151
152
153
154
155
156
157
158
159
160
161
import torch
import torch.nn as nn
from sklearn.metrics import confusion_matrix
from collections import defaultdict
from torch.nn.utils import clip_grad_norm
import torch.optim as optim
import matplotlib.pyplot as plt
from copy import deepcopy
class Trainer(nn.Module):
def __init__(self, model, args):
super(Trainer, self).__init__()
self.model = model(args)
self.optim = optim.Adam(
self.model.parameters(), lr=args.lr, weight_decay=args.L2)
if args.usingWeightRandomSampling:
pos_weight = None
else:
pos_weight = torch.tensor(
args.numberOfNonSpammer/args.numberOfSpammer)
self.threshold = args.threshold
self.log_path = args.log_path
self.model_path = args.model_path
self.model_name = args.model_name
self.Loss = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
self.hist = defaultdict(list)
self.cms = defaultdict(list)
self.confusion_matrics = []
def forward(self, input, label):
if type(input) is tuple:
input, lengths = input
else:
lengths = None
self.pred = self.model(input, lengths)
self.label = label
loss = self.Loss(self.pred.squeeze(1), label)
accuracy = torch.mean(
((torch.sigmoid(self.pred) > self.threshold).squeeze(-1) == label.byte()).float())
cm = confusion_matrix(label.cpu().numpy(),
(torch.sigmoid(self.pred) > self.threshold).cpu().numpy())
return loss, accuracy, cm
def train_step(self, input, label):
self.optim.zero_grad()
self.loss, self.accuracy, self.cm = self.forward(input, label)
self.hist["Temp_Train_Loss"].append(self.loss.item())
self.hist["Temp_Train_Accuracy"].append(self.accuracy.item())
self.hist["Train_Loss"].append(self.loss.item())
self.hist["Train_Accuracy"].append(self.accuracy.item())
self.cms["Train"].append(self.cm)
self.cms["Train"] = self.cms["Train"][-10:]
self.loss.backward()
# clip_grad_norm(self.model.parameters(), 0.25)
self.optim.step()
return self.loss, self.accuracy, self.cm
def test_step(self, input, label, validation=True):
# Not Updating the weight
self.loss, self.accuracy, self.cm = self.forward(input, label)
if validation:
self.hist["Temp_Val_Loss"].append(self.loss.item())
self.hist["Temp_Val_Accuracy"].append(self.accuracy.item())
self.hist["Val_Loss"].append(self.loss.item())
self.hist["Val_Accuracy"].append(self.accuracy.item())
self.cms["Val"].append(self.cm)
self.cms["Val"] = self.cms["Val"][-10:]
else:
self.hist["Temp_Test_Loss"].append(self.loss.item())
self.hist["Temp_Test_Accuracy"].append(self.accuracy.item())
self.hist["Test_Loss"].append(self.loss.item())
self.hist["Test_Accuracy"].append(self.accuracy.item())
self.cms["Test"].append(self.cm)
self.cms["Test"] = self.cms["Test"][-10:]
return self.loss, self.accuracy, self.cm
def calculateAverage(self,):
temp_keys = deepcopy(list(self.hist.keys()))
for name in temp_keys:
if 'Temp' in name:
self.hist["Average" + name[4:]
].append(sum(self.hist[name])/len(self.hist[name]))
self.hist[name] = []
def plot_train_hist(self, step):
fig = plt.figure(figsize=(20, 10))
num_loss = 2
i = 0
for name in self.hist.keys():
if 'Train' in name and not "Temp" in name and not "Average" in name:
i += 1
fig.add_subplot(num_loss, 1, i)
plt.plot(self.hist[name], label=name)
plt.xlabel('Number of Steps', fontsize=15)
plt.ylabel(name, fontsize=15)
plt.title(name, fontsize=30, fontweight="bold")
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()
fig.savefig(self.log_path+"Train_Loss&Acc_Hist_"+str(step)+".png")
def plot_all(self, step=None):
fig = plt.figure(figsize=(20, 10))
for name in self.hist.keys():
if "Average" in name:
if 'Loss' in name:
plt.subplot(211)
plt.plot(self.hist[name], marker='o', label=name)
plt.ylabel('Loss', fontsize=15)
plt.xlabel('Number of epochs', fontsize=15)
plt.title('Loss', fontsize=20, fontweight="bold")
plt.legend(loc='upper left')
if "Accuracy" in name:
plt.subplot(212)
plt.plot(self.hist[name], marker='o', label=name)
plt.ylabel('Accuracy', fontsize=15)
plt.xlabel('Number of epochs', fontsize=15)
plt.title('Accuracy', fontsize=20, fontweight="bold")
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()
if step is not None:
fig.savefig(self.log_path + "All_Hist_"+str(step)+".png")
def model_save(self, step):
path = self.model_path + self.model_name+'_Step_' + str(step) + '.pth'
torch.save({self.model_name: self.state_dict()}, path)
print('Model Saved')
def load_step_dict(self, step):
path = self.model_path + self.model_name + \
'_Step_' + str(step) + '.pth'
self.load_state_dict(torch.load(
path, map_location=lambda storage, loc: storage)[self.model_name])
print('Model Loaded')
def num_all_params(self,):
return sum([param.nelement() for param in self.parameters()])