-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformers.py
295 lines (227 loc) · 9.39 KB
/
transformers.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
# modeling libraries
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import AdamW
import numpy as np
import pandas as pd
import wandb
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.rnn import pad_sequence
from sklearn.metrics import precision_recall_fscore_support
# nicies
from tqdm import tqdm
# stdlib utilities
import json
import glob
import math
import random
VALIDATE_EVERY = 20
# initialize the device
DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device("mps") if torch.backends.mps.is_available() else torch.device('cpu')
# initialize the model
CONFIG = {
"epochs": 128,
"lr": 0.00001,
"batch_size": 32,
"hidden": 256,
"heads": 8,
"encoder_layers": 6,
"featureset": "combined"
}
# set up the run
# run = wandb.init(project="nacc", entity="jemoka", config=CONFIG)
run = wandb.init(project="nacc", entity="jemoka", config=CONFIG, mode="disabled")
config = run.config
BATCH_SIZE = config.batch_size
EPOCHS = config.epochs
LEARNING_RATE=config.lr
HIDDEN = config.hidden
HEADS = config.heads
LAYERS = config.encoder_layers
FEATURESET = config.featureset
# loading data
class NACCNeuralPsychDataset(Dataset):
def __init__(self, file_path, feature_path,
# skipping 2 impaired because of labeling inconsistency
target_feature="NACCUDSD", target_indicies=[1,3,4],
emph=3, val=0.001):
"""The NeuralPsycology Dataset
Arguments:
file_path (str): path to the NACC csv
feature_path (str): path to a text file with the input features to scean
[target_feature] (str): the name of feature to serve as the target
[target_indicies] ([int]): how to translate the output key values
to the indicies of an array
[emph] (int): the index to emphasize
[val] (float): number of samples to leave in the validation set
"""
# initialize superclass
super(NACCNeuralPsychDataset, self).__init__()
# Read the raw dataset
self.raw_data = pd.read_csv(file_path)
# shuffle
self.raw_data = self.raw_data.sample(frac=1)
# get the fature variables
with open(feature_path, 'r') as df:
lines = df.readlines()
self.features = [i.strip() for i in lines]
# basic dataaug
if emph:
self.raw_data = pd.concat([self.raw_data, self.raw_data[self.raw_data[target_feature]==emph]])
# skip elements whose target is not in the list
self.raw_data = self.raw_data[self.raw_data[target_feature].isin(target_indicies)]
# Calculate the target data
self.targets = self.raw_data[target_feature]
self.data = self.raw_data[self.features]
# store the traget indicies
self.__target_indicies = target_indicies
# get number of features, by hoisting the get function up and getting length
self._num_features = len(self.features)
# crop the data for validatino
val_count = int(len(self.data)*val)
self.val_data = self.data.iloc[:val_count]
self.val_targets = self.targets.iloc[:val_count]
self.data = self.data.iloc[val_count:]
self.targets = self.targets.iloc[val_count:]
def __process(self, data, target, index=None):
# the discussed dataprep
# if a data entry is <0 or >80, it is "not found"
# so, we encode those values as 0 in the FEATURE
# column, and encode another feature of "not-found"ness
data_found = (data > 80) | (data < 0)
data[data_found] = 0
# then, the found-ness becomes a mask
data_found_mask = (data_found)
# if it is a sample with no tangible data
# well give up and get another sample:
if sum(~data_found_mask) == 0:
if not index:
raise ValueError("All-Zero found in validation!")
indx = random.randint(2,5)
if index-indx <= 0:
return self[index+indx]
else:
return self[index-indx]
# seed the one-hot vector
one_hot_target = [0 for _ in range(len(self.__target_indicies))]
# and set it
one_hot_target[self.__target_indicies.index(target)] = 1
return torch.tensor(data).long(), torch.tensor(data_found_mask).bool(), torch.tensor(one_hot_target).float()
def __getitem__(self, index):
# index the data
data = self.data.iloc[index].copy()
target = self.targets.iloc[index].copy()
return self.__process(data, target, index)
def val(self):
"""Return the validation set"""
# collect dataset
dataset = []
# get it
for index in range(len(self.val_data)):
try:
dataset.append(self.__process(self.val_data.iloc[index].copy(),
self.val_targets.iloc[index].copy()))
except ValueError:
continue # all zero ignore
# return parts
inp, mask, out = zip(*dataset)
return torch.stack(inp).long(), torch.stack(mask).bool(), torch.stack(out).float()
def __len__(self):
return len(self.data)
# the transformer network
class NACCModel(nn.Module):
def __init__(self, num_features, num_classes, nhead=HEADS, nlayers=LAYERS, hidden=HIDDEN):
# call early initializers
super(NACCModel, self).__init__()
# the entry network ("linear embedding")
# bigger than 80 means that its going to be out of bounds and therefore
# be masked out; so hard code 81
self.embedding = nn.Embedding(81, hidden)
# the encoder network
encoder_layer = nn.TransformerEncoderLayer(d_model=hidden, nhead=nhead)
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=nlayers)
# flatten!
self.flatten = nn.Flatten()
# dropoutp!
self.dropout = nn.Dropout(0.1)
# prediction network
self.linear1 = nn.Linear(hidden*num_features, hidden)
self.gelu = nn.GELU()
self.linear2 = nn.Linear(hidden, num_classes)
self.softmax = nn.Softmax(1)
# loss
self.cross_entropy = nn.CrossEntropyLoss()
def forward(self, x, mask, labels=None):
net = self.embedding(x)
# recall transformers are seq first
net = self.encoder(net.transpose(0,1), src_key_padding_mask=mask).transpose(0,1)
net = self.dropout(net)
net = self.flatten(net)
net = self.linear1(net)
net = self.gelu(net)
net = self.linear2(net)
net = self.dropout(net)
net = self.softmax(net)
loss = None
if labels is not None:
# TODO put weight on MCI
# loss = (torch.log(net)*labels)*torch.tensor([1,1.3,1,1])
loss = self.cross_entropy(net, labels)
return { "logits": net, "loss": loss }
dataset = NACCNeuralPsychDataset("./investigator_nacc57.csv", f"./features/{FEATURESET}")
dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)
VALIDATION_SET = [i.to(DEVICE) for i in dataset.val()]
model = NACCModel(dataset._num_features, 3).to(DEVICE)
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
# calculate the f1 from tensors
def tensor_metrics(logits, labels):
label_indicies = torch.argmax(labels.cpu(), 1)
logits_indicies = logits.detach().cpu()
class_names = ["Control", "MCI", "Dementia"]
pr_curve = wandb.plot.pr_curve(label_indicies, logits_indicies, labels = class_names)
roc = wandb.plot.roc_curve(label_indicies, logits_indicies, labels = class_names)
cm = wandb.plot.confusion_matrix(
y_true=np.array(label_indicies), # can't labels index by scalar tensor
probs=logits_indicies,
class_names=class_names
)
acc = sum(label_indicies == torch.argmax(logits_indicies, dim=1))/len(label_indicies)
return pr_curve, roc, cm, acc
model.train()
for epoch in range(EPOCHS):
print(f"Currently training epoch {epoch}...")
for i, batch in tqdm(enumerate(iter(dataloader)), total=len(dataloader)):
# send batch to GPU if needed
batch = [i.to(DEVICE) for i in batch]
# generating validation output
if i % VALIDATE_EVERY == 0:
# model.eval()
output = model(*VALIDATION_SET)
try:
prec_recc, roc, cm, acc = tensor_metrics(output["logits"], VALIDATION_SET[2])
if i == 0 or i == len(dataloader)-1:
run.log({"val_loss": output["loss"].detach().cpu().item(),
"val_prec_recc": prec_recc,
"val_confusion": cm,
"val_roc": roc,
"val_acc": acc})
else:
run.log({"val_loss": output["loss"].detach().cpu().item(),
"val_acc": acc})
except ValueError:
breakpoint()
# run with actual backprop
output = model(*batch)
# backprop
output["loss"].backward()
optimizer.step()
optimizer.zero_grad()
# logging
run.log({"loss": output["loss"].detach().cpu().item()})
# Saving
print("Saving model...")
os.mkdir(f"./models/{run.name}")
torch.save(model, f"./models/{run.name}/model.save")
torch.save(optimizer, f"./models/{run.name}/optimizer.save")