2021-05-30 22:52:16 +00:00
|
|
|
"""Probabilistic-LVQ example using the Iris dataset."""
|
2021-05-25 18:26:15 +00:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2021-05-30 22:52:16 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
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()
|
|
|
|
|
|
|
|
# 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
|
|
|
|
num_classes = 3
|
|
|
|
prototypes_per_class = 2
|
|
|
|
hparams = dict(
|
|
|
|
distribution=(num_classes, prototypes_per_class),
|
|
|
|
lr=0.05,
|
2021-05-30 22:52:16 +00:00
|
|
|
variance=1.0,
|
2021-05-25 18:26:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2021-05-28 15:13:06 +00:00
|
|
|
model = pt.models.probabilistic.LikelihoodRatioLVQ(
|
2021-05-25 18:26:15 +00:00
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
2021-05-30 22:52:16 +00:00
|
|
|
# prototype_initializer=pt.components.UniformInitializer(2),
|
|
|
|
prototype_initializer=pt.components.SMI(train_ds),
|
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],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|