2021-05-17 15:03:37 +00:00
|
|
|
"""LVQMLN example using all four dimensions of the Iris dataset."""
|
2021-05-30 22:52:16 +00:00
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-05-17 15:03:37 +00:00
|
|
|
|
2021-06-14 19:00:26 +00:00
|
|
|
import prototorch as pt
|
2021-05-17 15:03:37 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import (
|
|
|
|
LVQMLN,
|
|
|
|
PruneLoserPrototypes,
|
|
|
|
VisSiameseGLVQ2D,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.utilities.seed import seed_everything
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
2021-05-17 15:03:37 +00:00
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
|
|
|
|
class Backbone(torch.nn.Module):
|
2022-01-11 17:28:50 +00:00
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
def __init__(self, input_size=4, hidden_size=10, latent_size=2):
|
|
|
|
super().__init__()
|
|
|
|
self.input_size = input_size
|
|
|
|
self.hidden_size = hidden_size
|
|
|
|
self.latent_size = latent_size
|
|
|
|
self.dense1 = torch.nn.Linear(self.input_size, self.hidden_size)
|
|
|
|
self.dense2 = torch.nn.Linear(self.hidden_size, self.latent_size)
|
|
|
|
self.activation = torch.nn.Sigmoid()
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.activation(self.dense1(x))
|
|
|
|
out = self.activation(self.dense2(x))
|
|
|
|
return out
|
|
|
|
|
2021-05-17 15:03:37 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-21 15:55:55 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-05-17 15:03:37 +00:00
|
|
|
# Dataset
|
|
|
|
train_ds = pt.datasets.Iris()
|
|
|
|
|
|
|
|
# Reproducibility
|
2022-05-17 10:03:43 +00:00
|
|
|
seed_everything(seed=42)
|
2021-05-17 15:03:37 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=150)
|
2021-05-17 15:03:37 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
|
|
|
hparams = dict(
|
2021-06-14 19:00:26 +00:00
|
|
|
distribution=[3, 4, 5],
|
2021-05-17 15:03:37 +00:00
|
|
|
proto_lr=0.001,
|
|
|
|
bb_lr=0.001,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the backbone
|
|
|
|
backbone = Backbone()
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = LVQMLN(
|
2021-05-17 15:03:37 +00:00
|
|
|
hparams,
|
2021-06-14 19:00:26 +00:00
|
|
|
prototypes_initializer=pt.initializers.SSCI(
|
|
|
|
train_ds,
|
|
|
|
transform=backbone,
|
|
|
|
),
|
2021-05-17 15:03:37 +00:00
|
|
|
backbone=backbone,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisSiameseGLVQ2D(
|
2021-05-17 15:03:37 +00:00
|
|
|
data=train_ds,
|
|
|
|
map_protos=False,
|
|
|
|
border=0.1,
|
|
|
|
resolution=500,
|
|
|
|
axis_off=True,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
pruning = PruneLoserPrototypes(
|
2021-06-14 19:00:26 +00:00
|
|
|
threshold=0.01,
|
|
|
|
idle_epochs=20,
|
|
|
|
prune_quota_per_epoch=2,
|
|
|
|
frequency=10,
|
|
|
|
verbose=True,
|
|
|
|
)
|
2021-05-17 15:03:37 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-21 15:55:55 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
2021-06-14 19:00:26 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
pruning,
|
|
|
|
],
|
2022-05-17 10:03:43 +00:00
|
|
|
log_every_n_steps=1,
|
|
|
|
max_epochs=1000,
|
|
|
|
detect_anomaly=True,
|
2021-05-21 15:55:55 +00:00
|
|
|
)
|
2021-05-17 15:03:37 +00:00
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|