-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnnet.py
157 lines (127 loc) · 5.58 KB
/
nnet.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
class NNet(nn.Module):
def __init__(self, model):
super(NNet, self).__init__()
self.model = model
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.model.to(device)
self.history = {
'train_loss' : [],
'train_acc' : [],
'valid_loss' : [],
'valid_acc' : []
}
def forward(self, x):
if not isinstance(x, torch.Tensor):
x = torch.tensor(x, dtype=torch.float32)
output = self.model(x)
return output
def predict_proba(self, x):
with torch.no_grad():
logits = self.forward(x)
probs = F.softmax(logits, dim=-1)
probs = probs.detach().numpy()
return probs
def predict(self, x):
with torch.no_grad():
logits = self.forward(x).numpy()
return np.argmax(logits, axis=1)
def train_model(self, X, y, epochs, batch_size, lr, val_split, seed=None, updates=1):
import math
if seed is not None:
torch.manual_seed(seed)
np.random.seed(seed)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
X_tensor = torch.autograd.Variable(torch.FloatTensor(X))
y_tensor = torch.autograd.Variable(torch.LongTensor(y))
num_va_obs = int(val_split * len(y))
num_tr_obs = len(y) - num_va_obs
X_train = X_tensor[:num_tr_obs, :]
X_valid = X_tensor[num_tr_obs:, :]
y_train = y_tensor[:num_tr_obs]
y_valid = y_tensor[num_tr_obs:]
num_tr_batches = math.ceil(len(y_train) / batch_size)
num_va_batches = math.ceil(len(y_valid) / batch_size)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(self.model.parameters(), lr=lr)
for n in range(epochs):
#----------------------------
# Training Loop
#----------------------------
self.model.train()
train_loss = 0
train_correct = 0
for i in range(num_tr_batches):
# Get new batch
start = i * batch_size
end = start + batch_size
X_batch = X_train[start:end]
y_batch = y_train[start:end]
X_batch = X_batch.to(device)
y_batch = y_batch.to(device)
# Calculate output, then loss
optimizer.zero_grad()
output = self.model.forward(X_batch)
loss = loss_fn(output, y_batch)
# Perform Update
loss.backward()
optimizer.step()
# Add batch loss to epoch loss, weighting by batch size
train_loss += loss.item() * len(y_batch)
# Count correct predictions
values, labels = torch.max(output, 1)
train_correct += np.sum(labels.cpu().numpy() == y_batch.cpu().numpy())
#----------------------------
# Validation Loop
#----------------------------
self.model.eval()
valid_loss = 0
valid_correct = 0
for i in range(num_va_batches):
# Get new batch
start = i * batch_size
end = start + batch_size
X_batch = X_valid[start:end]
y_batch = y_valid[start:end]
X_batch = X_batch.to(device)
y_batch = y_batch.to(device)
# Calculate output, then loss
with torch.no_grad():
output = self.model(X_batch)
loss = loss_fn(output, y_batch)
# Add batch loss to epoch loss, weighting by batch size
valid_loss += loss.item() * len(y_batch)
# Count correct predictions
values, labels = torch.max(output, 1)
valid_correct += np.sum(labels.cpu().numpy() == y_batch.cpu().numpy())
train_loss /= len(y_train)
train_acc = train_correct / len(y_train)
valid_loss /= len(y_valid)
valid_acc = valid_correct / len(y_valid)
self.history['train_loss'].append(train_loss)
self.history['train_acc'].append(train_acc)
self.history['valid_loss'].append(valid_loss)
self.history['valid_acc'].append(valid_acc)
if (n+1) % updates == 0:
print(f'Epoch {n+1}: Training loss: {train_loss:.4f}, Training Acc: {train_acc:.4f}, Val Loss: {valid_loss:.4f}, Val Acc {valid_acc:.4f}')
def training_curves(self, start=1, figsize=[12,4]):
epoch_range = range(start, len(self.history['train_loss'])+1)
plt.figure(figsize=figsize)
plt.subplot(1,2,1)
plt.plot(epoch_range, self.history['train_loss'][start-1:], label='Training', zorder=2)
if len(self.history['train_loss']) > 0:
plt.plot(epoch_range, self.history['valid_loss'][start-1:], label='Validation', zorder=2)
plt.xlabel('Epoch'), plt.ylabel('Loss'), plt.title('Loss by Epoch')
plt.grid()
plt.legend()
plt.subplot(1,2,2)
plt.plot(epoch_range, self.history['train_acc'][start-1:], label='Training', zorder=2)
if len(self.history['train_acc']) > 0:
plt.plot(epoch_range, self.history['valid_acc'][start-1:], label='Validation', zorder=2)
plt.grid()
plt.legend()
plt.xlabel('Epoch'), plt.ylabel('Accuracy'), plt.title('Accuracy by Epoch')