prototorch_models/examples/glvq_spiral.py

45 lines
1.2 KiB
Python
Raw Normal View History

"""GLVQ example using the spiral dataset."""
2021-05-07 13:25:04 +00:00
import prototorch as pt
import pytorch_lightning as pl
import torch
2021-05-13 13:22:01 +00:00
from prototorch.models.callbacks import StopOnNaN
if __name__ == "__main__":
# Dataset
2021-05-07 13:25:04 +00:00
train_ds = pt.datasets.Spiral(n_samples=600, noise=0.6)
# Dataloaders
2021-05-07 13:25:04 +00:00
train_loader = torch.utils.data.DataLoader(train_ds,
num_workers=0,
batch_size=256)
# Hyperparameters
2021-05-11 14:15:08 +00:00
nclasses = 2
prototypes_per_class = 20
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,
lr=0.01,
)
# Initialize the model
model = pt.models.GLVQ(hparams,
prototype_initializer=pt.components.SSI(train_ds,
noise=1e-1))
# Callbacks
2021-05-13 13:22:01 +00:00
vis = pt.models.VisGLVQ2D(train_ds, show_last_only=False, block=True)
snan = StopOnNaN(model.proto_layer.components)
# Setup trainer
trainer = pl.Trainer(
2021-05-13 13:22:01 +00:00
gpus=-1,
max_epochs=200,
callbacks=[vis, snan],
)
# Training loop
trainer.fit(model, train_loader)