-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_1.py
423 lines (349 loc) · 12.4 KB
/
train_1.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""Implementation of Mutation Validation Training of MNIST dataset for Learning Vector Quantization"""
import logging
import random
import warnings
from dataclasses import dataclass, field
import os
from pathlib import Path
import numpy as np
import torch
from lightning_fabric.utilities.warnings import PossibleUserWarning
from prototorch.models import PruneLoserPrototypes
from pytorch_lightning.callbacks import EarlyStopping
from torch.utils.data import DataLoader
from torch.utils import data
import prototorch as pt
import prototorch.models as ps
import pytorch_lightning as pl
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score
from mutate_labels import MutatedValidation
from mutated_validation import MutatedValidationScore, TrainRun, EvaluationMetricsType
from train import (
LVQ,
ValidationType,
HyperParameterSearch,
TrainModelSummary,
)
from dataset import DATA
torch.set_float32_matmul_precision(precision="high")
warnings.filterwarnings("ignore", category=PossibleUserWarning)
warnings.filterwarnings("ignore", category=UserWarning)
def reset_weights(m):
for layer in m.children():
if hasattr(layer, "reset_parameters"):
print(f"Reset trainable parameters of layer = {layer}")
layer.reset_parameters()
Path("./evaluation").mkdir(parents=True, exist_ok=True)
logging.basicConfig(
filename="evaluation/evaluation_metrics_scores_0.2.txt",
encoding="utf-8",
filemode="w",
level=logging.INFO,
)
def evaluation_metric_logs(
evaluation_metric_scores: list[float | int],
model_name: str,
validation: str,
log: bool = True,
) -> None:
report = [{validation: evaluation_metric_scores}]
match log:
case True:
return logging.info("%s:%s", model_name, report)
kfold = StratifiedKFold(n_splits=5, random_state=4, shuffle=True)
mean_scores = []
def train_hold_out(
input_data: torch.Tensor,
labels: torch.Tensor,
model_name: str,
optimal_search: str,
dynamic_proto_pruning: bool,
save_model: bool = False,
) -> TrainModelSummary:
train_set, train_labels = input_data[:1000], labels[:1000]
test_set, test_labels = input_data[-1000:], labels[-1000:]
train_ds = data.TensorDataset(train_set, train_labels)
train_loader = DataLoader(train_ds, batch_size=256, num_workers=2)
model = specified_lvq(model_name, train_ds, train_loader)
trainer = model_trainer(optimal_search, dynamic_proto_pruning)
trainer.fit(model, train_loader)
outputs = model.predict(torch.Tensor(test_set))
if save_model:
save_train_model(
saved_model_dir="./saved_models",
model_name=model_name + "_" + ValidationType.HOLDOUT,
estimator=model,
scoring_metric=EvaluationMetricsType.ACCURACY,
)
return TrainModelSummary([accuracy_score(test_labels, outputs)]) # type: ignore
def train_model_cv(
input_data: torch.Tensor,
labels: torch.Tensor,
model_name: str,
optimal_search: str,
dynamic_proto_pruning: bool,
save_model: bool = False,
) -> TrainModelSummary:
metric_scores = []
for fold, (train_index, test_index) in enumerate(kfold.split(input_data, labels)): # type: ignore
train_ds = data.TensorDataset(input_data[train_index], labels[train_index])
train_loader = DataLoader(train_ds, batch_size=128, num_workers=2)
model = specified_lvq(model_name, train_ds, train_loader)
model.apply(reset_weights)
trainer = model_trainer(optimal_search, dynamic_proto_pruning)
trainer.fit(model, train_loader)
outputs = model.predict(torch.Tensor(input_data[test_index]))
metric_scores.append(accuracy_score(labels[test_index], outputs))
if fold == 4:
if save_model:
save_train_model(
saved_model_dir="./saved_models",
model_name=model_name + "_" + ValidationType.HOLDOUT,
estimator=model,
scoring_metric=EvaluationMetricsType.ACCURACY,
)
mean_scores.append(np.mean(metric_scores))
return TrainModelSummary(mean_scores)
def train_model_by_mv(
input_data: torch.Tensor,
labels: torch.Tensor,
model_name: str,
optimal_search: str,
dynamic_proto_pruning: bool = False,
save_model: bool = False,
) -> TrainModelSummary:
train_ds = data.TensorDataset(input_data, labels)
train_loader = DataLoader(train_ds, batch_size=256, num_workers=2)
model = specified_lvq(model_name, train_ds, train_loader)
model_mv = specified_lvq(model_name, train_ds, train_loader)
model.apply(reset_weights)
label = labels.cpu().detach().numpy()
mutated_validation = MutatedValidation(
labels=label.astype(np.int64),
perturbation_ratio=0.2,
perturbation_distribution="balanced",
)
mutate_list = mutated_validation.get_mutated_label_list
mutated_labels = torch.from_numpy(mutate_list).to(torch.float32)
train_ds_mutated = data.TensorDataset(input_data, mutated_labels)
train_loader_mutated = DataLoader(train_ds_mutated, batch_size=256, num_workers=2)
results, mv_score = [], []
for train_runs in range(2):
match train_runs:
case TrainRun.ORIGINAL:
trainer = model_trainer(
search=optimal_search, dynamic_proto_pruning=dynamic_proto_pruning
)
trainer.fit(model, train_loader)
results.append(model.predict(input_data))
if save_model:
save_train_model(
saved_model_dir="./saved_models",
model_name=model_name,
estimator=model,
scoring_metric=EvaluationMetricsType.ACCURACY,
)
case TrainRun.MUTATED:
model_mv.apply(reset_weights)
trainer = model_trainer(
search=optimal_search, dynamic_proto_pruning=dynamic_proto_pruning
)
trainer.fit(model_mv, train_loader_mutated)
results.append(model_mv.predict(input_data))
mv_scorer = MutatedValidationScore(
mutated_labels=mutated_validation,
mutate=mutate_list,
original_training_predicted_labels=results[0],
mutated_training_predicted_labels=results[1],
evaluation_metric=EvaluationMetricsType.ACCURACY,
)
mv_score.append(mv_scorer.get_mv_score)
return TrainModelSummary(mv_score)
def gtlvq(
train_ds: data.TensorDataset,
train_loader: torch.utils.data.DataLoader, # type: ignore
) -> ps.ImageGTLVQ:
hparams = dict(
input_dim=28 * 28,
latent_dim=28,
distribution={"num_classes": 10, "per_class": 1},
proto_lr=0.01,
bb_lr=0.01,
)
return ps.ImageGTLVQ(
hparams,
optimizer=torch.optim.Adam,
prototypes_initializer=pt.initializers.SMCI(train_ds),
# Use one batch of data for subspace initiator.
omega_initializer=pt.initializers.PCALinearTransformInitializer(
next(iter(train_loader))[0].reshape(256, 28 * 28)
),
)
def glvq(train_ds: data.TensorDataset) -> ps.ImageGLVQ:
hparams = dict(
distribution={"num_classes": 10, "per_class": 1},
lr=0.01,
)
return ps.ImageGLVQ(
hparams,
optimizer=torch.optim.Adam,
prototypes_initializer=pt.initializers.SMCI(train_ds),
)
def gmlvq(train_ds: data.TensorDataset) -> ps.ImageGMLVQ:
hparams = dict(
input_dim=28 * 28,
latent_dim=28 * 28,
distribution={"num_classes": 10, "per_class": 1},
proto_lr=0.01,
bb_lr=0.01,
)
return ps.ImageGMLVQ(
hparams,
optimizer=torch.optim.Adam,
prototypes_initializer=pt.initializers.SMCI(train_ds),
)
def specified_lvq(
model: str,
train_ds: data.TensorDataset,
train_loader: torch.utils.data.DataLoader, # type: ignore
) -> ps.ImageGLVQ | ps.ImageGMLVQ | ps.ImageGTLVQ:
match model:
case LVQ.GLVQ:
return glvq(train_ds)
case LVQ.GMLVQ:
return gmlvq(train_ds)
case LVQ.GTLVQ:
return gtlvq(train_ds, train_loader)
case _:
raise RuntimeError("specified_lvq: none of the models did match")
def model_trainer(search: str, dynamic_proto_pruning: bool) -> pl.Trainer:
if search == HyperParameterSearch.FALSE and dynamic_proto_pruning is True:
trainer = pl.Trainer(
callbacks=[
PruneLoserPrototypes(
threshold=0.01,
idle_epochs=1,
prune_quota_per_epoch=10,
frequency=1,
verbose=True,
),
EarlyStopping(
monitor="train_loss",
min_delta=0.001,
patience=15,
mode="min",
check_on_train_epoch_end=True,
),
],
max_epochs=1000,
log_every_n_steps=1,
detect_anomaly=True,
accelerator="cpu",
)
return trainer
if HyperParameterSearch.TRUE:
trainer = pl.Trainer(
max_epochs=3,
enable_progress_bar=True,
enable_checkpointing=False,
logger=False,
detect_anomaly=True,
enable_model_summary=False,
accelerator="gpu",
)
return trainer
else:
raise RuntimeError("model_trainer:none of the search marches")
def save_train_model(
*,
saved_model_dir: str,
model_name: str,
estimator: ps.ImageGLVQ | ps.ImageGMLVQ | ps.ImageGTLVQ,
scoring_metric: str,
):
Path(saved_model_dir).mkdir(parents=True, exist_ok=True)
try:
torch.save(
estimator,
os.path.join(
saved_model_dir,
model_name + scoring_metric + ".pt",
),
)
except AttributeError:
pass
@dataclass(slots=True)
class TM:
input_data: torch.Tensor
labels: torch.Tensor
model_name: str
optimal_search: str
dynamic_proto_pruning: bool = False
save_model: bool = False
summary_metric_list: list = field(default_factory=lambda: [])
@property
def train_ho(self) -> TrainModelSummary:
return train_hold_out(
self.input_data,
self.labels,
self.model_name,
self.optimal_search,
self.dynamic_proto_pruning,
self.save_model,
)
@property
def train_cv(self) -> TrainModelSummary:
return train_model_cv(
self.input_data,
self.labels,
self.model_name,
self.optimal_search,
self.dynamic_proto_pruning,
self.save_model,
)
@property
def train_mv(self) -> TrainModelSummary:
return train_model_by_mv(
self.input_data,
self.labels,
self.model_name,
self.optimal_search,
self.dynamic_proto_pruning,
self.save_model,
)
@property
def train_all(self) -> logging.INFO: # type: ignore
self.summary_metric_list.append(
self.train_ho.selected_model_evaluation_metrics_scores
)
self.summary_metric_list.append(
self.train_cv.selected_model_evaluation_metrics_scores
)
self.summary_metric_list.append(
self.train_mv.selected_model_evaluation_metrics_scores
)
return evaluation_metric_logs(
self.summary_metric_list,
self.model_name,
EvaluationMetricsType.ACCURACY.value,
)
def seed_everything(seed: int):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True # type: ignore
torch.backends.cudnn.benchmark = True # type: ignore
if __name__ == "__main__":
seed_everything(seed=4)
train_data = DATA(random=4)
train = TM(
input_data=train_data.mnist.input_data,
labels=train_data.mnist.labels,
model_name=LVQ.GMLVQ,
optimal_search=HyperParameterSearch.FALSE,
)
# train and evaluate using Holdout, CV and MV scheme
EVALUATE = train.train_all