2021-11-15 08:57:44 +00:00
|
|
|
"""Siamese GTLVQ example using all four dimensions of the Iris dataset."""
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2023-06-20 15:30:21 +00:00
|
|
|
from lightning_fabric.utilities.seed import seed_everything
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import SiameseGTLVQ, VisSiameseGLVQ2D
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Backbone(torch.nn.Module):
|
2022-01-11 17:28:50 +00:00
|
|
|
|
2021-11-15 08:50:33 +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
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2023-06-20 15:30:21 +00:00
|
|
|
parser.add_argument("--gpus", type=int, default=0)
|
|
|
|
parser.add_argument("--fast_dev_run", type=bool, default=False)
|
2021-11-15 08:50:33 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Dataset
|
|
|
|
train_ds = pt.datasets.Iris()
|
|
|
|
|
|
|
|
# Reproducibility
|
2022-05-17 10:03:43 +00:00
|
|
|
seed_everything(seed=2)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=150)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2022-05-17 10:03:43 +00:00
|
|
|
hparams = dict(
|
|
|
|
distribution=[1, 2, 3],
|
2023-06-20 15:42:36 +00:00
|
|
|
lr=0.01,
|
2022-05-17 10:03:43 +00:00
|
|
|
input_dim=2,
|
|
|
|
latent_dim=1,
|
|
|
|
)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Initialize the backbone
|
|
|
|
backbone = Backbone(latent_size=hparams["input_dim"])
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = SiameseGTLVQ(
|
2021-11-15 08:50:33 +00:00
|
|
|
hparams,
|
|
|
|
prototypes_initializer=pt.initializers.SMCI(train_ds),
|
|
|
|
backbone=backbone,
|
|
|
|
both_path_gradients=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisSiameseGLVQ2D(data=train_ds, border=0.1)
|
2021-11-15 08:50:33 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2023-06-20 15:30:21 +00:00
|
|
|
trainer = pl.Trainer(
|
|
|
|
accelerator="cuda" if args.gpus else "cpu",
|
|
|
|
devices=args.gpus if args.gpus else "auto",
|
|
|
|
fast_dev_run=args.fast_dev_run,
|
2022-05-17 10:03:43 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
],
|
|
|
|
max_epochs=1000,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-11-15 08:50:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|