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-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-05-25 18:26:15 +00:00
|
|
|
lr=0.05,
|
2021-06-04 20:21:28 +00:00
|
|
|
variance=0.1,
|
2021-05-25 18:26:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2021-05-31 15:56:45 +00:00
|
|
|
model = pt.models.probabilistic.RSLVQ(
|
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
|
2021-05-28 19:30:50 +00:00
|
|
|
print(model)
|
|
|
|
|
2021-05-25 18:26:15 +00:00
|
|
|
# Callbacks
|
2021-05-30 22:52:16 +00:00
|
|
|
vis = pt.models.VisGLVQ2D(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)
|