-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_utils.py
More file actions
96 lines (76 loc) · 3.02 KB
/
train_utils.py
File metadata and controls
96 lines (76 loc) · 3.02 KB
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
"""Training utilities."""
from torch._C import device
from tqdm import tqdm
import numpy as np
import torch
import torch.nn.functional as F
import torch.nn as nn
import sys
sys.path.append('..')
class Flatten(nn.Module):
"""A custom layer that views an input as 1D."""
def forward(self, input):
# flatten each image in a batch of 32
return torch.flatten(input, start_dim=1)
# Helpers
def batchify_data(x_data, y_data, batch_size):
"""Takes a set of data points and labels and groups them into batches."""
# Only take batch_size chunks (i.e. drop the remainder)
N = int(len(x_data) / batch_size) * batch_size
batches = []
for i in range(0, N, batch_size):
batches.append({
'x': torch.tensor(x_data[i:i+batch_size], dtype=torch.float32),
'y': torch.tensor(y_data[i:i+batch_size], dtype=torch.long
)})
return batches
def compute_accuracy(predictions, y):
"""Computes the accuracy of predictions against the gold labels, y."""
predictions, y = predictions.to('cpu'), y.to('cpu')
return np.mean(np.equal(predictions.numpy(), y.numpy()))
# Training Procedure
def train_model(train_data, dev_data, model, lr=0.01, momentum=0.9, nesterov=False, n_epochs=30):
"""Train a model for N epochs given data and hyper-params."""
# We optimize with SGD
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum, nesterov=nesterov)
for epoch in range(1, 11):
print(f"-------------\nEpoch {epoch}:\n")
# Run **training***
loss, acc = run_epoch(train_data, model.train(), optimizer)
print('Train loss: {:.6f} | Train accuracy: {:.6f}'.format(loss, acc))
# Run **validation**
val_loss, val_acc = run_epoch(dev_data, model.eval(), optimizer)
print('Val loss: {:.6f} | Val accuracy: {:.6f}'.format(val_loss, val_acc))
# Save model
torch.save(model, f'/models/CNN{epoch}.pt')
return val_acc
def run_epoch(data, model, optimizer):
"""Train model for one pass of train data, and return loss, acccuracy"""
# Gather losses
losses = []
batch_accuracies = []
# If model is in train mode, use optimizer.
is_training = model.training
# Iterate through batches
for batch in tqdm(data):
# Grab x and y
x, y = batch['x'], batch['y']
# Get output predictions
device = 'cuda' if torch.cuda.is_available() else 'cpu'
x, y = x.to(device), y.to(device)
out = model(x)
# Predict and store accuracy
predictions = torch.argmax(out, dim=1)
batch_accuracies.append(compute_accuracy(predictions, y))
# Compute loss
loss = F.cross_entropy(out, y)
losses.append(loss.data.item())
# If training, do an update.
if is_training:
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Calculate epoch level scores
avg_loss = np.mean(losses)
avg_accuracy = np.mean(batch_accuracies)
return avg_loss, avg_accuracy