-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
169 lines (132 loc) · 5.62 KB
/
training.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
162
163
164
165
166
167
168
169
import math
import sys
import numpy as np
import torch
from sklearn.metrics import f1_score, accuracy_score, recall_score
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import DataLoader,TensorDataset
def progress(loss, epoch, batch, batch_size, dataset_size):
"""
Print the progress of the training for each epoch
"""
batches = math.ceil(float(dataset_size) / batch_size)
count = batch * batch_size
bar_len = 40
filled_len = int(round(bar_len * count / float(dataset_size)))
bar = '=' * filled_len + '-' * (bar_len - filled_len)
status = 'Epoch {}, Loss: {:.4f}'.format(epoch, loss)
_progress_str = "\r \r [{}] ...{}".format(bar, status)
sys.stdout.write(_progress_str)
sys.stdout.flush()
if batch == batches:
print()
def train_dataset(_epoch, dataloader, model, loss_function, optimizer):
# IMPORTANT: switch to train mode
# enable regularization layers, such as Dropout
model.train()
running_loss = 0.0
# obtain the model's device ID
device = next(model.parameters()).device
for index, batch in enumerate(dataloader, 1):
# get the inputs (batch)
inputs, labels, lengths = batch
# move the batch tensors to the right device
inputs = inputs.to(device)
labels = labels.to(device)
lengths = lengths.to(device) # EX9
# Step 1 - zero the gradients
# Remember that PyTorch accumulates gradients.
# We need to clear them out before each batch!
optimizer.zero_grad() # EX9
# Step 2 - forward pass: y' = model(x)
if model.__class__.__name__ in ['PrepLabBaselineDNN','BaselineDNN', 'LSTM']: # EX9
outputs = model(inputs, lengths)
else:
outputs = model(inputs)
# Step 3 - compute loss: L = loss_function(y, y')
try:
loss = loss_function(outputs,labels) # EX9
except ValueError:
# fix labels for 'BCEWithLogitsLoss' loss function
bin_labels = torch.nn.functional.one_hot(labels.long(), num_classes=2)
loss = loss_function(outputs, bin_labels.float())
# Step 4 - backward pass: compute gradient wrt model parameters
loss.backward() # EX9
# Step 5 - update weights
optimizer.step() # EX9
running_loss += loss.data.item()
# print statistics
progress(loss=loss.data.item(),
epoch=_epoch,
batch=index,
batch_size=dataloader.batch_size,
dataset_size=len(dataloader.dataset))
return running_loss / index
def eval_dataset(dataloader, model, loss_function):
# IMPORTANT: switch to eval mode
# disable regularization layers, such as Dropout
model.eval()
running_loss = 0.0
y_pred = [] # the predicted labels
y = [] # the gold labels
# obtain the model's device ID
device = next(model.parameters()).device
# IMPORTANT: in evaluation mode, we don't want to keep the gradients
# so we do everything under torch.no_grad()
with torch.no_grad():
for index, batch in enumerate(dataloader, 1):
# get the inputs (batch)
inputs, labels, lengths = batch
# move the batch tensors to the right device
inputs = inputs.to(device)
labels = labels.to(device)
lengths = lengths.to(device) # EX9
# Step 1 - zero the gradients
# Remember that PyTorch accumulates gradients.
# We need to clear them out before each batch!
#model.zero_grad() # EX9
# Step 2 - forward pass: y' = model(x)
if model.__class__.__name__ in ['PrepLabBaselineDNN', 'BaselineDNN', 'LSTM']: # EX9
outputs = model(inputs, lengths)
else:
outputs = model(inputs)
#outputs = model(inputs)
# Step 3 - compute loss: L = loss_function(y, y')
loss = loss_function(outputs,labels) # EX9
# Step 4 - make predictions (class = argmax of posteriors)
_, predictions = torch.max(outputs, 1)
#predictions=torch.argmax(outputs, dim=1) # EX9
# Step 5 - collect the predictions, gold labels and batch loss
# EX9
y_pred.append(predictions)
y.append(labels)
running_loss += loss.data.item()
return running_loss / index, (y_pred, y)
def torch_train_val_split(
dataset, batch_train, batch_eval, val_size=0.2, shuffle=True, seed=420
):
# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
val_split = int(np.floor(val_size * dataset_size))
if shuffle:
np.random.seed(seed)
np.random.shuffle(indices)
train_indices = indices[val_split:]
val_indices = indices[:val_split]
# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
val_sampler = SubsetRandomSampler(val_indices)
train_loader = DataLoader(
dataset, batch_size=batch_train, sampler=train_sampler)
val_loader = DataLoader(
dataset, batch_size=batch_eval, sampler=val_sampler)
return train_loader, val_loader
def get_metrics_report(y, y_hat):
# Convert values to lists
y = np.concatenate([y_i.cpu() for y_i in y], axis=0)
y_hat = np.concatenate([y_hat_i.cpu() for y_hat_i in y_hat], axis=0)
# report metrics
report = f' accuracy: {accuracy_score(y, y_hat)}\n recall: ' + \
f'{recall_score(y, y_hat, average="macro")}\n f1-score: {f1_score(y, y_hat,average="macro")}'
return report