-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
308 lines (243 loc) · 10.5 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import argparse
import os
import time
from models.cnn import CNN, CNNLayered
from models.lstm import LSTM, LSTMAtt
from models.siarn import SIARN
from models.miarn import MIARN
from models.siarn3 import SIARN3
import numpy as np
import torch
import torch.nn as nn
MODELS = {
"cnn": CNN,
"3cnn": CNNLayered,
"lstm": LSTM,
"lstm_att": LSTMAtt,
"siarn": SIARN,
"miarn": MIARN,
"siarn3": SIARN3
}
def select_model(model_name, embed_size, vocab_size, seq_len, load_path=""):
"""
Given the name of the desired model, and an optional path to a pretrained model, construct
and return the model, loading its parameters from the .pth file if specified.
"""
if model_name == "cnn":
# Parameters for CNN
filter_count = 100
filter_size = 3
params = [filter_count, filter_size, embed_size, vocab_size, seq_len]
elif model_name == "3cnn":
# Parameters for 3CNN
filter_counts = [100, 100, 100]
filter_sizes = [3, 4, 5]
params = [filter_counts, filter_sizes, embed_size, vocab_size, seq_len]
elif model_name == "lstm":
# Parameters for LSTM
hidden_dim = 100
params = [embed_size, hidden_dim, vocab_size]
elif model_name == "lstm_att":
# Parameters for LSTM_ATT
hidden_dim = 100
params = [embed_size, hidden_dim, vocab_size, seq_len]
elif model_name == "siarn":
# Parameters for SIARN
hidden_dim = 100
params = [embed_size, hidden_dim, vocab_size, seq_len]
elif model_name == 'miarn':
# Parameters for MIARN
hidden_dim = 100
params = [embed_size, hidden_dim, vocab_size, seq_len]
elif model_name == "siarn3":
# Parameters for SIARN
hidden_dim = 100
params = [embed_size, hidden_dim, vocab_size, seq_len]
# Construct the model
model = MODELS[model_name](*params)
# If specified, load a pretrained model
if load_path:
model.load_state_dict(torch.load(load_path, map_location=torch.device('cpu')))
model.eval()
return model
def train_model(model, num_epochs, train_loader, optimizer, loss_function, clip):
"""
Train the given model over a number of epochs, using the data in train_loader.
Uses the specified optimizer and los function. Clip gives the amount to clip
the gradient to avoid exploding gradients.
Returns two lists: the training losses and the accuracies at each iteration.
# General loop structure seen at the following:
https://adventuresinmachinelearning.com/convolutional-neural-networks-tutorial-in-pytorch/
"""
print("Training model...")
time0 = time.time()
t0 = time0
train_losses = []
accuracies = []
model.train()
for epoch in range(1, num_epochs + 1):
for i, (xs, labels) in enumerate(train_loader):
# Perform forward pass
outputs = model(xs)
# Compute loss and perform backprop
loss = loss_function(outputs, labels.float())
optimizer.zero_grad()
loss.backward()
# Prevent exploding gradient
nn.utils.clip_grad_norm_(model.parameters(), clip)
optimizer.step()
# Compute accuracy and save it as well as the training loss
pred_labels = convert_output_to_label(outputs)
accuracy = get_accuracy(pred_labels, labels)
train_losses.append(loss.item())
accuracies.append(accuracy)
t1 = time.time()
time_elapsed = (t1 - t0)
t0 = t1
print(f'\tEpoch [{epoch}/{num_epochs}], Time: {time_elapsed:.2f} sec, Loss: {loss:.4f}, Accuracy: {accuracy:.2f}')
time_elapsed = time.time() - time0
print(f" Time Elapsed: {time_elapsed:.2f} sec")
return train_losses, accuracies
def long_train_model(model, model_name, train_loader, optimizer, loss_function, clip, num_epochs, storage_step):
"""
Train the given model over a number of epochs, using the data in train_loader.
Uses the specified optimizer and loss function. Clip gives the amount to clip
the gradient to avoid exploding gradients.
Returns two lists: the training losses and the accuracies at each iteration.
"""
base_path = "out/long_train/" + model_name + "/"
os.makedirs(base_path, exist_ok=True)
print("Training model...")
time0 = time.time()
t0 = time0
train_losses = []
accuracies = []
model.train()
# train for many, many epochs to get a large parameter space
for epoch in range(1, num_epochs + 1):
for i, (xs, labels) in enumerate(train_loader):
# Perform forward pass
outputs = model(xs)
# Compute loss and perform backprop
loss = loss_function(outputs, labels.float())
optimizer.zero_grad()
loss.backward()
# Prevent exploding gradient
nn.utils.clip_grad_norm_(model.parameters(), clip)
optimizer.step()
# Compute accuracy and save it as well as the training loss
pred_labels = convert_output_to_label(outputs)
accuracy = get_accuracy(pred_labels, labels)
train_losses.append(loss.item())
accuracies.append(accuracy)
t1 = time.time()
time_elapsed = (t1 - t0)
t0 = t1
print(
f'\tEpoch [{epoch}/{num_epochs}], Time: {time_elapsed:.2f} sec, Loss: {loss:.4f}, Accuracy: {accuracy:.2f}')
#save model params every 5 epochs
if epoch%storage_step==0:
torch.save(model.state_dict(), base_path + str(epoch) + ".pth")
time_elapsed = time.time() - time0
print(f" Time Elapsed: {time_elapsed:.2f} sec")
return train_losses, accuracies
def evaluate_long_train(model_name, valid_loader, embed_size, vocab_size, seq_len, use_gpu,
num_epochs, storage_step, valid_criterion):
base_path = "out/long_train/" + model_name + "/"
accuracies = np.zeros(len(range(storage_step,num_epochs+1, storage_step)))
fscores = np.zeros(len(range(storage_step,num_epochs+1, storage_step)))
for i, epoch in enumerate(range(storage_step,num_epochs+1, storage_step)):
model_path = base_path + str(epoch) + ".pth"
model = select_model(model_name, embed_size, vocab_size, seq_len, model_path)
if use_gpu:
model.cuda()
model.eval()
with torch.no_grad():
correct = 0
total = 0
false_positives = 0
false_negatives = 0
true_positives = 0
for xs, labels in valid_loader:
outputs = model(xs)
pred_labels = convert_output_to_label(outputs)
if valid_criterion == "accuracy":
total += len(labels)
correct += (pred_labels == labels).sum().int()
elif valid_criterion == "fscore":
false_negatives += get_false_negatives(pred_labels, labels)
false_positives += get_false_positives(pred_labels, labels)
true_positives += get_true_positives(pred_labels, labels)
if valid_criterion == "accuracy":
accuracy = correct / total
accuracies[i] = correct / total
elif valid_criterion == "fscore":
precision = true_positives / (true_positives + false_positives)
recall = true_positives / (true_positives + false_negatives)
fscore = 2. * (precision * recall) / (precision + recall)
fscores[i] = fscore
# Choose best model based on specified criterion
if valid_criterion == "accuracy":
best_i = np.argmax(accuracies)
return accuracies[best_i], base_path + str(best_i*storage_step+storage_step) + ".pth"
elif valid_criterion == "fscore":
best_i = np.nanargmax(fscores)
return fscores[best_i], base_path + str(best_i*storage_step+storage_step) + ".pth"
def test_model(model, test_loader):
"""
Test the given model on the test data in test_loader.
Returns the precision, recall, accuracy, and F-score.
"""
print("Testing model...")
time0 = time.time()
model.eval()
with torch.no_grad():
correct = 0
total = 0
false_positives = 0
false_negatives = 0
true_positives = 0
for xs, labels in test_loader:
outputs = model(xs)
pred_labels = convert_output_to_label(outputs)
total += len(labels)
correct += (pred_labels == labels).sum().int()
false_negatives += get_false_negatives(pred_labels, labels)
false_positives += get_false_positives(pred_labels, labels)
true_positives += get_true_positives(pred_labels, labels)
time_elapsed = time.time() - time0
print(f" Time Elapsed: {time_elapsed:.2f} sec")
# Calculate precision, recall, accuracy, and F-score
precision = true_positives / (true_positives + false_positives)
recall = true_positives / (true_positives + false_negatives)
accuracy = correct / total
fscore = 2. * (precision * recall) / (precision + recall)
print(" Testing Results:")
print(f"\tPrecision: {precision:.4f}")
print(f"\tRecall: {recall:.4f}")
print(f"\tAccuracy: {accuracy:.4f}")
print(f"\tF-score: {fscore:.4f}")
return precision, recall, accuracy, fscore
def save_training_results(model_name, losses, accuracies, outdir, save_suffix=""):
fpath = f"{outdir}/training_results_{model_name}{save_suffix}.txt"
np.savetxt(fpath, np.array([range(len(losses)), losses, accuracies]).T,
header='epoch, loss, accuracy')
print(f"Training results saved to {fpath}")
def save_testing_results(model_name, precision, recall, accuracy, fscore, outdir, save_suffix=""):
fpath = f"{outdir}/testing_results_{model_name}{save_suffix}.txt"
np.savetxt(fpath, np.array([precision, recall, accuracy, fscore]),
header='precision, recall, accuracy, fscore')
print(f"Testing results saved to {fpath}")
########################
## Helper functions ##
########################
def convert_output_to_label(output):
return torch.round(output).int()
def get_accuracy(y_pred, y_true):
return (y_pred == y_true).sum().float() / y_true.shape[0]
def get_false_positives(y_pred, y_true):
return torch.logical_and(y_pred != y_true, y_pred == 1).sum().int()
def get_false_negatives(y_pred, y_true):
return torch.logical_and(y_pred != y_true, y_pred == 0).sum().int()
def get_true_positives(y_pred, y_true):
return torch.logical_and(y_pred == y_true, y_pred == 1).sum().int()