prototorch_models/examples/rslvq_iris.py

69 lines
1.6 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 prototorch.models import RSLVQ, VisGLVQ2D
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-30 22:52:16 +00:00
2021-05-25 18:26:15 +00:00
if __name__ == "__main__":
# Command-line arguments
parser = argparse.ArgumentParser()
parser = pl.Trainer.add_argparse_args(parser)
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.from_argparse_args(
args,
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)