2021-04-21 12:54:07 +00:00
|
|
|
"""GLVQ example using the Iris dataset."""
|
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import logging
|
|
|
|
import warnings
|
2021-05-21 15:55:55 +00:00
|
|
|
|
2021-06-04 13:55:06 +00:00
|
|
|
import prototorch as pt
|
2021-04-21 12:54:07 +00:00
|
|
|
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 GLVQ, VisGLVQ2D
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
2021-06-04 13:55:06 +00:00
|
|
|
from torch.optim.lr_scheduler import ExponentialLR
|
2022-05-17 10:03:43 +00:00
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
2021-04-21 12:54:07 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
2021-05-21 15:55:55 +00:00
|
|
|
# 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-05-21 15:55:55 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-04-21 13:52:42 +00:00
|
|
|
# Dataset
|
2021-05-30 22:52:16 +00:00
|
|
|
train_ds = pt.datasets.Iris(dims=[0, 2])
|
2021-04-21 13:52:42 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=64, num_workers=4)
|
2021-04-21 12:54:07 +00:00
|
|
|
|
2021-05-06 12:10:09 +00:00
|
|
|
# Hyperparameters
|
|
|
|
hparams = dict(
|
2021-05-25 18:57:54 +00:00
|
|
|
distribution={
|
|
|
|
"num_classes": 3,
|
2021-06-14 18:13:25 +00:00
|
|
|
"per_class": 4
|
2021-05-25 18:57:54 +00:00
|
|
|
},
|
2021-05-06 12:10:09 +00:00
|
|
|
lr=0.01,
|
2021-04-21 17:16:57 +00:00
|
|
|
)
|
2021-04-21 19:35:52 +00:00
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = GLVQ(
|
2021-06-04 13:55:06 +00:00
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
2021-06-14 18:13:25 +00:00
|
|
|
prototypes_initializer=pt.initializers.SMCI(train_ds),
|
2021-06-04 13:55:06 +00:00
|
|
|
lr_scheduler=ExponentialLR,
|
|
|
|
lr_scheduler_kwargs=dict(gamma=0.99, verbose=False),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
2021-05-07 13:25:04 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisGLVQ2D(data=train_ds)
|
2021-04-21 19:35:52 +00:00
|
|
|
|
2021-05-06 12:10:09 +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=100,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-05-06 12:10:09 +00:00
|
|
|
)
|
2021-04-21 12:54:07 +00:00
|
|
|
|
2021-04-21 13:52:42 +00:00
|
|
|
# Training loop
|
2021-04-21 12:54:07 +00:00
|
|
|
trainer.fit(model, train_loader)
|
2022-02-02 18:02:26 +00:00
|
|
|
|
|
|
|
# Manual save
|
|
|
|
trainer.save_checkpoint("./glvq_iris.ckpt")
|
|
|
|
|
|
|
|
# Load saved model
|
2022-05-17 10:03:43 +00:00
|
|
|
new_model = GLVQ.load_from_checkpoint(
|
2022-02-02 20:52:01 +00:00
|
|
|
checkpoint_path="./glvq_iris.ckpt",
|
|
|
|
strict=False,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
logging.info(new_model)
|