2021-11-15 08:57:44 +00:00
|
|
|
"""Localized-GTLVQ example using the Moons dataset."""
|
2021-11-10 17:04:24 +00:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Reproducibility
|
|
|
|
pl.utilities.seed.seed_everything(seed=2)
|
|
|
|
|
|
|
|
# Dataset
|
|
|
|
train_ds = pt.datasets.Moons(num_samples=300, noise=0.2, seed=42)
|
|
|
|
|
|
|
|
# Dataloaders
|
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds,
|
|
|
|
batch_size=256,
|
|
|
|
shuffle=True)
|
|
|
|
|
|
|
|
# Hyperparameters
|
2021-11-15 08:50:33 +00:00
|
|
|
# Latent_dim should be lower than input dim.
|
|
|
|
hparams = dict(distribution=[1, 3], input_dim=2, latent_dim=1)
|
2021-11-10 17:04:24 +00:00
|
|
|
|
|
|
|
# Initialize the model
|
|
|
|
model = pt.models.GTLVQ(
|
2021-11-15 08:50:33 +00:00
|
|
|
hparams, prototypes_initializer=pt.initializers.SMCI(train_ds))
|
2021-11-10 17:04:24 +00:00
|
|
|
|
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
|
|
|
# Summary
|
|
|
|
print(model)
|
|
|
|
|
|
|
|
# Callbacks
|
|
|
|
vis = pt.models.VisGLVQ2D(data=train_ds)
|
|
|
|
es = pl.callbacks.EarlyStopping(
|
|
|
|
monitor="train_acc",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=20,
|
|
|
|
mode="max",
|
|
|
|
verbose=False,
|
|
|
|
check_on_train_epoch_end=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Setup trainer
|
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
es,
|
|
|
|
],
|
|
|
|
weights_summary="full",
|
|
|
|
accelerator="ddp",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|