prototorch_models/examples/rslvq_iris.py

65 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
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
2021-06-08 13:01:08 +00:00
from torchvision.transforms import Lambda
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
pl.utilities.seed.seed_everything(seed=42)
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
2021-05-30 22:52:16 +00:00
train_loader = torch.utils.data.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,
input_dim=2,
latent_dim=2,
bb_lr=0.01,
2021-05-25 18:26:15 +00:00
)
# Initialize the model
2021-06-08 13:01:08 +00:00
model = pt.models.probabilistic.PLVQ(
2021-05-25 18:26:15 +00:00
hparams,
optimizer=torch.optim.Adam,
2021-06-04 20:21:28 +00:00
# prototype_initializer=pt.components.SMI(train_ds),
2021-05-31 15:56:45 +00:00
prototype_initializer=pt.components.SSI(train_ds, noise=0.2),
2021-06-04 20:21:28 +00:00
# prototype_initializer=pt.components.Zeros(2),
# prototype_initializer=pt.components.Ones(2, scale=2.0),
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)
# Summary
print(model)
2021-05-25 18:26:15 +00:00
# Callbacks
2021-06-08 13:01:08 +00:00
vis = pt.models.VisSiameseGLVQ2D(data=train_ds)
2021-05-25 18:26:15 +00:00
# Setup trainer
trainer = pl.Trainer.from_argparse_args(
args,
callbacks=[vis],
2021-05-31 15:56:45 +00:00
terminate_on_nan=True,
2021-06-04 20:21:28 +00:00
weights_summary="full",
accelerator="ddp",
2021-05-25 18:26:15 +00:00
)
# Training loop
trainer.fit(model, train_loader)