-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorch_model.py
163 lines (133 loc) · 5.28 KB
/
torch_model.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
import torch.nn
import torch
import pytorch_lightning as pl
import numpy as np
from einops import rearrange
import os
import csv
class SimpleTransformer(pl.LightningModule):
def __init__(
self,
lr,
emb_size,
scheduler,
num_features,
num_heads,
num_layers,
num_classes,
weight,
classes,
dropout,
savedir,
exp_number,
trial_number
):
super().__init__()
self.save_hyperparameters()
self.lr = lr
self.emb_size = emb_size
self.scheduler = scheduler
self.classes = classes
self.savedir = savedir
self.exp_number = exp_number
self.trial_num = trial_number
if weight is not None:
self.loss = torch.nn.CrossEntropyLoss(weight=torch.FloatTensor(weight))
else:
self.loss = torch.nn.CrossEntropyLoss()
encoder_layer = torch.nn.TransformerEncoderLayer(
d_model = num_features,
nhead = num_heads,
dim_feedforward=emb_size,
batch_first=True,
dropout=dropout
)
self.encoder = torch.nn.TransformerEncoder(
encoder_layer=encoder_layer,
num_layers = num_layers,
enable_nested_tensor=False,
)
self.decoder = torch.nn.Sequential(torch.nn.Flatten(start_dim=0, end_dim=1),
torch.nn.Linear(num_features, num_features),
torch.nn.LazyBatchNorm1d(),
torch.nn.ReLU(),
#torch.nn.Dropout(dropout),
torch.nn.Linear(num_features, num_features),
torch.nn.LazyBatchNorm1d(),
torch.nn.ReLU(),
torch.nn.Linear(num_features, num_classes),
)
def calc_ova(self, x, target):
predicted = torch.argmax(x, dim=1)
#expected = torch.argmax(target, dim=1)
expected = target
conf_matrix = np.zeros((len(self.classes.keys()), len(self.classes.keys())), dtype=np.int32)
#conf_matrix is: rows-predicted, columns-expected
zip_count = 0
for p, e in zip(predicted, expected):
zip_count +=1
conf_matrix[p, e] += 1
ova = np.trace(conf_matrix)/conf_matrix.sum()
assert conf_matrix.sum() == len(expected), "error calculating confusion matrix"
return ova, conf_matrix
def forward(self, batch, softmax = False):
inp = batch['input']
pad_mask= batch['pad_mask']
target = batch['target']
x = self.encoder(inp, src_key_padding_mask = pad_mask)
x = self.decoder(x)
pad_mask = rearrange(~pad_mask, 'b p -> (b p)')
target = rearrange(target, 'b p -> (b p)')[pad_mask]
x = x[pad_mask]
if softmax:
x = torch.nn.functional.softmax(x, 1)
return x, target
def training_step(self, batch, batch_idx):
x, target = self.forward(batch)
loss = self.loss(x, target)
self.log('loss', loss)
return loss
def validation_step(self, batch, batch_idx):
x, target = self.forward(batch)
loss = self.loss(x, target)
self.log("val_loss", loss, prog_bar=True)
ova, _ = self.calc_ova(x, target)
self.log("val_ova", ova, prog_bar=True)
self.log("hp_metric", ova)
return loss
def test_step(self, batch, batch_idx):
x, target = self.forward(batch)
loss = self.loss(x, target)
self.log("test_loss", loss)
ova, conf_matrix = self.calc_ova(x, target)
self.log("test_ova", ova)
print(self.classes)
print(conf_matrix)
self.write_conf_matrix(conf_matrix)
tb = self.logger.experiment
tb.add_text("class_labels", str(self.classes))
tb.add_text("confusion_matrix", str(conf_matrix).replace("\n", " \n"))
return loss
def configure_optimizers(self):
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, self.parameters()), lr=self.lr)
to_return = {"optimizer": optimizer, "monitor": "train_loss"}
if self.scheduler:
scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer=optimizer, T_0=40)
to_return['lr_scheduler'] = scheduler
return to_return
def write_conf_matrix(self, conf_matrix):
rows = []
for row in conf_matrix:
rows = rows + [[f'{num}' for num in row]]
classes = list(self.classes.keys())
num_classes = len(classes)
with open(os.path.join(self.savedir,f'{self.exp_number}_{self.trial_num}_conf_matrix.csv'), 'w') as conf_file:
conf_writer = csv.writer(conf_file)
header = ['' for x in range(num_classes+1)]
header[1] = 'Expected'
conf_writer.writerow(header)
header_2 = ['Predicted'] + classes
conf_writer.writerow(header_2)
for ix, row in enumerate(rows):
to_write = [classes[ix]] + row
conf_writer.writerow(to_write)