prototorch_models/examples/rslvq_iris.py

72 lines
1.8 KiB
Python
Raw Normal View History

2021-06-04 20:21:28 +00:00
"""RSLVQ example using the Iris dataset."""
2021-05-25 18:26:15 +00:00
import argparse
import warnings
2021-05-25 18:26:15 +00:00
2021-06-04 20:21:28 +00:00
import prototorch as pt
2021-05-25 18:26:15 +00:00
import pytorch_lightning as pl
import torch
from lightning_fabric.utilities.seed import seed_everything
from prototorch.models import RSLVQ, VisGLVQ2D
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-30 22:52:16 +00:00
2021-05-25 18:26:15 +00:00
if __name__ == "__main__":
# Command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--gpus", type=int, default=0)
parser.add_argument("--fast_dev_run", type=bool, default=False)
2021-05-25 18:26:15 +00:00
args = parser.parse_args()
2021-05-31 15:56:45 +00:00
# Reproducibility
seed_everything(seed=42)
2021-05-31 15:56:45 +00:00
2021-05-25 18:26:15 +00:00
# Dataset
2021-05-30 22:52:16 +00:00
train_ds = pt.datasets.Iris(dims=[0, 2])
2021-05-25 18:26:15 +00:00
# Dataloaders
train_loader = DataLoader(train_ds, batch_size=64)
2021-05-25 18:26:15 +00:00
# Hyperparameters
hparams = dict(
2021-05-31 15:56:45 +00:00
distribution=[2, 2, 3],
2021-06-08 13:01:08 +00:00
proto_lr=0.05,
lambd=0.1,
2021-06-14 18:56:38 +00:00
variance=1.0,
2021-06-08 13:01:08 +00:00
input_dim=2,
latent_dim=2,
bb_lr=0.01,
2021-05-25 18:26:15 +00:00
)
# Initialize the model
model = RSLVQ(
2021-05-25 18:26:15 +00:00
hparams,
optimizer=torch.optim.Adam,
2021-06-14 18:56:38 +00:00
prototypes_initializer=pt.initializers.SSCI(train_ds, noise=0.2),
2021-05-25 18:26:15 +00:00
)
2021-06-04 20:21:28 +00:00
# Compute intermediate input and output sizes
model.example_input_array = torch.zeros(4, 2)
2021-05-25 18:26:15 +00:00
# Callbacks
vis = VisGLVQ2D(data=train_ds)
2021-05-25 18:26:15 +00:00
# Setup trainer
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,
callbacks=[
vis,
],
detect_anomaly=True,
max_epochs=100,
log_every_n_steps=1,
2021-05-25 18:26:15 +00:00
)
# Training loop
trainer.fit(model, train_loader)