-
Notifications
You must be signed in to change notification settings - Fork 165
/
shared_utilities.py
173 lines (134 loc) · 4.95 KB
/
shared_utilities.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
import lightning as L
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
import torchmetrics
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, Dataset
class LightningModel(L.LightningModule):
def __init__(self, model, learning_rate):
super().__init__()
self.learning_rate = learning_rate
self.model = model
self.train_acc = torchmetrics.Accuracy(task="multiclass", num_classes=2)
self.val_acc = torchmetrics.Accuracy(task="multiclass", num_classes=2)
self.test_acc = torchmetrics.Accuracy(task="multiclass", num_classes=2)
def forward(self, x):
return self.model(x)
def _shared_step(self, batch):
features, true_labels = batch
logits = self(features)
loss = F.cross_entropy(logits, true_labels)
predicted_labels = torch.argmax(logits, dim=1)
return loss, true_labels, predicted_labels
def training_step(self, batch, batch_idx):
loss, true_labels, predicted_labels = self._shared_step(batch)
self.log("train_loss", loss)
self.train_acc(predicted_labels, true_labels)
self.log(
"train_acc", self.train_acc, prog_bar=True, on_epoch=True, on_step=False
)
return loss
def validation_step(self, batch, batch_idx):
loss, true_labels, predicted_labels = self._shared_step(batch)
self.log("val_loss", loss, prog_bar=True)
self.val_acc(predicted_labels, true_labels)
self.log("val_acc", self.val_acc, prog_bar=True)
def test_step(self, batch, batch_idx):
_, true_labels, predicted_labels = self._shared_step(batch)
self.test_acc(predicted_labels, true_labels)
self.log("test_acc", self.test_acc)
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.parameters(), lr=self.learning_rate)
return optimizer
class CustomDataset(Dataset):
def __init__(self, feature_array, label_array, transform=None):
self.x = feature_array
self.y = label_array
self.transform = transform
def __getitem__(self, index):
x = self.x[index]
y = self.y[index]
if self.transform is not None:
x = self.transform(x)
return x, y
def __len__(self):
return self.y.shape[0]
class CustomDataModule(L.LightningDataModule):
def __init__(self, data_dir="./mnist", batch_size=32):
super().__init__()
self.data_dir = data_dir
self.batch_size = batch_size
def prepare_data(self):
# download
pass
def setup(self, stage: str):
X, y = make_classification(
n_samples=20000,
n_features=100,
n_informative=10,
n_redundant=40,
n_repeated=25,
n_clusters_per_class=5,
flip_y=0.05,
class_sep=0.5,
random_state=123,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=123
)
X_train, X_val, y_train, y_val = train_test_split(
X_train, y_train, test_size=0.1, random_state=123
)
self.train_dataset = CustomDataset(
feature_array=X_train.astype(np.float32),
label_array=y_train.astype(np.int64),
)
self.val_dataset = CustomDataset(
feature_array=X_val.astype(np.float32), label_array=y_val.astype(np.int64)
)
self.test_dataset = CustomDataset(
feature_array=X_test.astype(np.float32), label_array=y_test.astype(np.int64)
)
def train_dataloader(self):
train_loader = DataLoader(
dataset=self.train_dataset,
batch_size=self.batch_size,
shuffle=True,
drop_last=True,
num_workers=0,
)
return train_loader
def val_dataloader(self):
val_loader = DataLoader(
dataset=self.val_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=0,
)
return val_loader
def test_dataloader(self):
test_loader = DataLoader(
dataset=self.test_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=0,
)
return test_loader
def plot_csv_logger(
csv_path, loss_names=["train_loss", "val_loss"], eval_names=["train_acc", "val_acc"]
):
metrics = pd.read_csv(csv_path)
aggreg_metrics = []
agg_col = "epoch"
for i, dfg in metrics.groupby(agg_col):
agg = dict(dfg.mean())
agg[agg_col] = i
aggreg_metrics.append(agg)
df_metrics = pd.DataFrame(aggreg_metrics)
df_metrics[loss_names].plot(grid=True, legend=True, xlabel="Epoch", ylabel="Loss")
df_metrics[eval_names].plot(grid=True, legend=True, xlabel="Epoch", ylabel="ACC")
plt.show()