2021-04-29 17:09:10 +00:00
|
|
|
"""GLVQ example using the spiral dataset."""
|
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
|
|
|
|
2021-05-07 13:25:04 +00:00
|
|
|
import prototorch as pt
|
2021-04-29 17:09:10 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-21 15:55:55 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-04-29 17:09:10 +00:00
|
|
|
# Dataset
|
2021-05-07 13:25:04 +00:00
|
|
|
train_ds = pt.datasets.Spiral(n_samples=600, noise=0.6)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2021-05-07 13:25:04 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds,
|
|
|
|
num_workers=0,
|
|
|
|
batch_size=256)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-05-11 14:15:08 +00:00
|
|
|
nclasses = 2
|
|
|
|
prototypes_per_class = 20
|
2021-04-29 17:09:10 +00:00
|
|
|
hparams = dict(
|
2021-05-11 14:15:08 +00:00
|
|
|
distribution=(nclasses, prototypes_per_class),
|
2021-05-04 18:56:16 +00:00
|
|
|
transfer_function="sigmoid_beta",
|
|
|
|
transfer_beta=10.0,
|
2021-04-29 17:09:10 +00:00
|
|
|
lr=0.01,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2021-05-12 14:36:22 +00:00
|
|
|
model = pt.models.GLVQ(hparams,
|
|
|
|
prototype_initializer=pt.components.SSI(train_ds,
|
|
|
|
noise=1e-1))
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2021-05-18 08:15:38 +00:00
|
|
|
vis = pt.models.VisGLVQ2D(train_ds, show_last_only=True, block=True)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-21 15:55:55 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
|
|
|
callbacks=[vis],
|
|
|
|
terminate_on_nan=True,
|
2021-04-29 17:09:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|