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-06-04 20:21:28 +00:00
|
|
|
import prototorch as pt
|
2021-04-29 17:09:10 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-05-21 16:54:47 +00:00
|
|
|
import torch
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
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-06-04 20:21:28 +00:00
|
|
|
train_ds = pt.datasets.Spiral(num_samples=500, noise=0.5)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2021-06-04 20:21:28 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds, batch_size=256)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-05-25 13:41:10 +00:00
|
|
|
num_classes = 2
|
2021-06-04 20:21:28 +00:00
|
|
|
prototypes_per_class = 10
|
2021-04-29 17:09:10 +00:00
|
|
|
hparams = dict(
|
2021-05-25 13:41:10 +00:00
|
|
|
distribution=(num_classes, prototypes_per_class),
|
2021-06-04 20:21:28 +00:00
|
|
|
transfer_function="swish_beta",
|
2021-05-04 18:56:16 +00:00
|
|
|
transfer_beta=10.0,
|
2021-06-04 20:21:28 +00:00
|
|
|
proto_lr=0.1,
|
|
|
|
bb_lr=0.1,
|
|
|
|
input_dim=2,
|
|
|
|
latent_dim=2,
|
2021-04-29 17:09:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2021-06-04 20:21:28 +00:00
|
|
|
model = pt.models.GMLVQ(
|
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
2021-06-14 18:19:08 +00:00
|
|
|
prototypes_initializer=pt.initializers.SSCI(train_ds, noise=1e-2),
|
2021-06-04 20:21:28 +00:00
|
|
|
)
|
2021-04-29 17:09:10 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2021-06-04 20:21:28 +00:00
|
|
|
vis = pt.models.VisGLVQ2D(
|
|
|
|
train_ds,
|
|
|
|
show_last_only=False,
|
|
|
|
block=False,
|
|
|
|
)
|
|
|
|
pruning = pt.models.PruneLoserPrototypes(
|
2021-06-14 18:19:08 +00:00
|
|
|
threshold=0.01,
|
2021-06-04 20:21:28 +00:00
|
|
|
idle_epochs=10,
|
|
|
|
prune_quota_per_epoch=5,
|
2021-06-14 18:19:08 +00:00
|
|
|
frequency=5,
|
2021-06-04 20:21:28 +00:00
|
|
|
replace=True,
|
2021-06-14 18:19:08 +00:00
|
|
|
prototypes_initializer=pt.initializers.SSCI(train_ds, noise=1e-1),
|
2021-06-04 20:21:28 +00:00
|
|
|
verbose=True,
|
|
|
|
)
|
|
|
|
es = pl.callbacks.EarlyStopping(
|
|
|
|
monitor="train_loss",
|
|
|
|
min_delta=1.0,
|
|
|
|
patience=5,
|
|
|
|
mode="min",
|
|
|
|
check_on_train_epoch_end=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,
|
2021-06-04 20:21:28 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
2021-07-06 15:09:21 +00:00
|
|
|
es,
|
2021-06-04 20:21:28 +00:00
|
|
|
pruning,
|
|
|
|
],
|
2021-05-21 15:55:55 +00:00
|
|
|
terminate_on_nan=True,
|
2021-04-29 17:09:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|