-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableclass_engine.py
More file actions
236 lines (194 loc) · 8.47 KB
/
Copy pathtableclass_engine.py
File metadata and controls
236 lines (194 loc) · 8.47 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
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
# imports
import shutil
import torch
import torchvision
from tqdm import tqdm
from sklearn.metrics import f1_score, accuracy_score, classification_report
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from torch.utils.tensorboard import SummaryWriter
from typing import Dict, List, Iterable, Tuple
# writer = SummaryWriter("../data/runs/")
matplotlib.style.use('seaborn-whitegrid')
# training function
def train(model, dataloader, optimizer, criterion, train_data, device):
print("====BEGIN TRAINING====")
model.train()
all_labels = []
all_predictions = []
running_loss = 0.0
counter = 0
optimizer.zero_grad()
for i, mini_batch in tqdm(enumerate(dataloader), total=int(len(train_data) / dataloader.batch_size)):
counter += 1
input_ids = mini_batch['input_ids'].to(device)
target = mini_batch['labels'].to(device)
# multi_hots = mini_batch["multi_hot"].to(device)
optimizer.zero_grad() # empties from memory
logits = model(input_ids)
# logits = model(input_ids, multi_hots)
# apply sigmoid activation to get all the outputs between 0 and 1
outputs = torch.sigmoid(logits)
loss = criterion(outputs, target)
# backpropagation
# loss.backward() #changed this
loss.backward()
# update optimizer parameters
optimizer.step() # updating weights
optimizer.zero_grad() # empties from memory
assert outputs.shape == target.shape
predicted = torch.round(outputs).detach().numpy()
labels = target.detach().numpy()
all_labels.extend(labels)
all_predictions.extend(predicted)
running_loss += loss.item()
final_loss = running_loss / counter
all_labels = np.stack(all_labels, axis=0)
all_predictions = np.stack(all_predictions, axis=0)
return all_labels, all_predictions, final_loss
# validation function
def validate(model, dataloader, criterion, val_data, device):
print('Validating')
model.eval()
running_loss = 0.0
counter = 0
all_labels = []
all_predictions = []
with torch.no_grad():
for i, mini_batch in tqdm(enumerate(dataloader), total=int(len(val_data) / dataloader.batch_size)):
counter += 1
input_ids = mini_batch['input_ids'].to(device)
target = mini_batch['labels'].to(device)
# multi_hots = mini_batch["multi_hot"].to(device)
# weight_rebal = torch.ones_like(target) / 95.0 + (1.0 - 1.0 / 95.0) * target
logits = model(input_ids)
# logits = model(input_ids, multi_hots)
# apply sigmoid activation to get all the outputs between 0 and 1
outputs = torch.sigmoid(logits)
loss = criterion(outputs, target)
# loss = criterion(outputs, target, weight=weight_rebal)
assert outputs.shape == target.shape
predicted = torch.round(outputs).detach().numpy()
labels = target.detach().numpy()
all_predictions.extend(predicted)
all_labels.extend(labels)
running_loss += loss.item()
final_loss = running_loss / counter
all_labels = np.stack(all_labels, axis=0)
all_predictions = np.stack(all_predictions, axis=0)
return all_labels, all_predictions, final_loss
# overfit to training data for debugging
def overfit_subset(model, sub_sample, optimizer, criterion, device):
print("====OVERFITTING SANITY CHECK====")
model.train()
input_ids = sub_sample['input_ids'].to(device)
target = sub_sample['labels'].to(device)
multi_hots = sub_sample["multi_hot"].to(device)
optimizer.zero_grad() # empties from memory
logits = model(input_ids, multi_hots)
# apply sigmoid activation to get all the outputs between 0 and 1
outputs = torch.sigmoid(logits)
loss = criterion(outputs, target)
# backpropagation
loss.backward()
# update optimizer parameters
optimizer.step() # updating weights
optimizer.zero_grad() # empties from memory
assert outputs.shape == target.shape
predicted = torch.round(outputs).detach().numpy()
labels = target.detach().numpy()
final_loss = loss.item()
return labels, predicted, final_loss
def save_checkpoint(state, is_best, run_name, file_path="../data/outputs/model_saves/"):
filename = (file_path + run_name + "checkpoint.pth.tar")
torch.save(state, filename)
if is_best:
is_best_filename = (file_path + run_name + "model_best.pth.tar")
shutil.copyfile(filename, is_best_filename)
def f1_nozeros(class_report_dict: Dict) -> Tuple[float, float]:
"""Calculates F1-score without classes where support is zero"""
acceptable_keys = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
non_zero_support_f1s = [dict(
class_name=key,
f1=v["f1-score"],
support=v["support"]) for key, v in class_report_dict.items() if
key in set(acceptable_keys) and v["support"] != 0]
# if remove_nonrel:
# non_zero_support_f1s = [x for x in non_zero_support_f1s if x["class_name"] != "4"]
# if only_notrel:
# non_zero_support_f1s = [x for x in non_zero_support_f1s if x["class_name"] == "4"]
macrof1 = 0.
weighted_f1 = 0.
if non_zero_support_f1s:
n_tp = np.sum([x["support"] for x in non_zero_support_f1s])
macrof1 = np.mean([x["f1"] for x in non_zero_support_f1s])
weighted_f1 = np.sum([x["f1"] * (x["support"] / n_tp) for x in non_zero_support_f1s])
return macrof1, weighted_f1
def label_wise_metrics(labels, preds):
"""Calculates Label-wise and global metrics for all classes"""
labels = np.asarray(labels)
preds = np.asarray(preds)
perclass_metrics = []
all_correct_preds = []
acc_list = []
counter = 0
for col, col2 in zip(labels.T, preds.T):
correct_predictions = ((col == col2).sum())
all_correct_preds.append(correct_predictions)
class_f1 = round(f1_score(y_true=col, y_pred=col2), 4)
class_acc = round(accuracy_score(y_true=col, y_pred=col2), 4)
acc_list.append(class_acc)
counter += 1
metrics_dict = {"Label": counter,
"acc": class_acc,
"f1": class_f1}
perclass_metrics.append(metrics_dict)
micro_acc = round((sum(all_correct_preds) / (preds.size)), 4)
macro_acc = round(sum(acc_list) / len(acc_list), 4)
global_metrics = {"micro_acc": micro_acc,
"macro_acc": macro_acc}
return global_metrics
def plot_loss_graph(train_loss, valid_loss, cf):
plt.figure(figsize=(10, 7))
plt.plot(train_loss, color='orange', label='train loss')
plt.plot(valid_loss, color='red', label='validataion loss')
plt.title("Loss on Train and Validation Sets")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.savefig(('../data/outputs/model_plots/loss-' + cf["run_name"] + '.png'))
plt.show()
def plot_f1_graph(train_f1, valid_f1, cf, variation: str):
plt.figure(figsize=(10, 7))
plt.plot(train_f1, color='orange', label='train f1')
plt.plot(valid_f1, color='red', label='validataion f1')
plt.title(f"Macro F1 Scores on Train and Validation sets {variation}")
plt.xlabel('Epochs')
plt.ylabel('F1')
plt.legend()
plt.savefig(('../data/outputs/model_plots/f1-' + variation + cf["run_name"] + '.png'))
plt.show()
def plot_SA_graph(hyperparameters, hyperparm_name, train_scores, val_scores):
plt.figure(figsize=(10, 7))
plt.plot(train_scores, hyperparameters, label="Training Score")
plt.plot(val_scores, hyperparameters, label="Cross Validation Score")
plt.xlabel(hyperparm_name)
plt.ylabel('Weighted F1 Score')
plt.tight_layout()
plt.show()
def plot_val_curve(hyperparameters, hyperparm_name, train_scores, train_scores_std, val_scores, val_scores_std):
plt.figure(figsize=(10, 7))
plt.plot(hyperparameters, train_scores, label="Training Score", color="darkorange", lw=2)
plt.fill_between(hyperparameters, train_scores - train_scores_std,
train_scores + train_scores_std, alpha=0.2,
color="darkorange", lw=2)
plt.plot(hyperparameters, val_scores, label="Cross Validation Score", color="navy", lw=2)
plt.fill_between(hyperparameters, val_scores - val_scores_std,
val_scores + val_scores_std, alpha=0.2,
color="navy", lw=2)
plt.legend(loc="best")
plt.xlabel(hyperparm_name)
plt.ylabel('Weighted F1 Score')
plt.tight_layout()
plt.show()