-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel_Functions.py
executable file
·108 lines (78 loc) · 2.65 KB
/
Model_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
import torch.optim as optim
from torch.utils.data import DataLoader
import torch
import numpy as np
def classification(prediction):
classified = torch.argmax(prediction, dim = 1)
return classified
def accuracy(y_pred, y_test):
class_pred = classification(y_pred)
accuracy = class_pred == y_test
accuracy_percent = torch.count_nonzero(accuracy)/accuracy.shape[0]
return accuracy_percent.item()
def train_model(data_train,
data_test,
net, loss,
nepoch ,
lr = 0.01,
batch_size = -1,
momentum = 0,
use_cuda = False,
print_output = True,
optimiser = 'SGD'):
# setting up arrays for recording
test_acc = []
avg_acc = []
test_loss = []
avg_loss = []
# appropriate data type for CPU or GPU
device = None
if use_cuda and torch.cuda.is_available():
dtype = torch.cuda.FloatTensor
device = torch.device("cuda")
net = net.to(device)
else:
dtype = torch.FloatTensor
if optimiser == 'SGD':
optimizer = optim.SGD(net.parameters(), lr = lr, momentum = momentum)
else:
optimizer = optim.Adam(net.parameters(), lr = lr, momentum = momentum)
data_train = data_train.change_type(dtype)
data_test = data_test.change_type(dtype)
X_test,y_test = data_test.get_data()
y_test = y_test.type(torch.LongTensor)
if device != None:
y_test = y_test.type(torch.cuda.LongTensor)
data_loader = DataLoader(data_train, batch_size = batch_size, shuffle = True)
for epoch in range(nepoch):
batch_acc = []
batch_loss = []
for X_batch, y_batch in data_loader:
y_batch = y_batch.type(torch.LongTensor)
if use_cuda and device != None:
X_batch = X_batch.to(device)
y_batch = y_batch.to(device)
y_batch = y_batch.type(torch.cuda.LongTensor)
optimizer.zero_grad()
pred = net(X_batch)
Rn = loss(pred, y_batch)
accur = accuracy(pred,y_batch)
batch_acc.append(accur)
batch_loss.append(Rn.to(torch.device('cpu')).detach().numpy())
Rn.backward()
optimizer.step()
avg_batch_loss = np.mean(batch_loss)
avg_batch_acc = np.mean(batch_acc)
avg_acc.append(avg_batch_acc)
avg_loss.append(avg_batch_loss)
pred = net(X_test)
Rn = loss(pred, y_test)
accur = accuracy(pred,y_test)
test_acc.append(accur)
test_loss.append(Rn.to(torch.device('cpu')).detach().numpy())
if print_output:
print('epoch:', epoch)
print('loss:',Rn.item())
print('------------')
print('final loss:', Rn.item())
return net, avg_loss, avg_acc, test_loss, test_acc