-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
101 lines (92 loc) · 3.2 KB
/
utils.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
from typing import List, Tuple
import numpy as np
import matplotlib.pyplot as plt
import torch
from torch import nn
def plotLosses(trainLosses, testLosses):
xs = np.arange(0, len(trainLosses))
plt.plot(xs, trainLosses, label="Train Loss")
xs = np.arange(0, len(testLosses))
plt.plot(xs, testLosses, alpha=0.65, label="Test Loss")
plt.legend()
plt.title("Losses vs Epoch")
plt.show()
def getActualAndPredictedOutput(model, device, dataloader) -> Tuple[List, List]:
pred = []
act = []
model = model.to(device)
model.eval()
with torch.no_grad():
for x, y in dataloader:
x = x.to(device)
yhat = model(x)
_, ypred = yhat.max(dim=1)
ypred = ypred.detach().cpu().numpy()
ypred = list(ypred)
y = list(y.detach().cpu().numpy())
pred.extend(ypred)
act.extend(y)
return pred, act
def trainModel(device, model, criterion, optimizer, threshold, trainDataloader, trainDataset, testDataloader, testDataset, verbose=True,):
rnn = model.to(device)
epoch = 0
trainLoss = 0
prevTrainLoss = 0
trainLosses = []
testLosses = []
while True:
epoch += 1
trainLoss = 0
rnn.train()
if verbose:
print()
print(f"Epoch #{epoch} {'-'*30}")
for x, y in trainDataloader:
optimizer.zero_grad()
x = x.to(device)
y = y.to(device)
pred = rnn(x)
loss = criterion(pred, y)
loss.backward()
optimizer.step()
if type(x) == torch.nn.utils.rnn.PackedSequence:
nInput = x.batch_sizes.max().item()
else:
nInput = x.shape[0]
trainLoss += nInput*loss.item()
trainLoss = trainLoss/len(trainDataset)
trainLosses.append(trainLoss)
if verbose:
print(f"Train Loss: {trainLoss}")
print(f"Previous Training Loss: {prevTrainLoss}")
rnn.eval()
with torch.no_grad():
testLoss = 0
for x, y in testDataloader:
x = x.to(device)
y = y.to(device)
pred = rnn(x)
loss = criterion(pred, y)
if type(x) == torch.nn.utils.rnn.PackedSequence:
nInput = x.batch_sizes.max().item()
else:
nInput = x.shape[0]
testLoss += nInput*loss.item()
testLoss = testLoss/len(testDataset)
testLosses.append(testLoss)
if verbose:
print(f"Test Loss: {testLoss}")
if ((abs(trainLoss-prevTrainLoss) < threshold) and prevTrainLoss != 0) or epoch > 1000:
break
else:
prevTrainLoss = trainLoss
if verbose:
print(
f"Training continued till {epoch} number of epochs.\n The final training loss being {trainLoss} and test loss being {testLoss}.")
return rnn, trainLosses, testLosses
def collate_fn(listOfData):
x = list(map(lambda x: x[0], listOfData))
y = list(map(lambda x: x[1], listOfData))
xxs = nn.utils.rnn.pack_sequence(x, enforce_sorted=False)
yys = torch.stack(y)
return xxs, yys