2021-06-01 22:21:42 +00:00
|
|
|
"""Growing Neural Gas example using the Iris dataset."""
|
|
|
|
|
2021-06-01 15:19:43 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import logging
|
|
|
|
import warnings
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
2021-06-04 20:21:28 +00:00
|
|
|
import torch
|
2023-06-20 15:30:21 +00:00
|
|
|
from lightning_fabric.utilities.seed import seed_everything
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import GrowingNeuralGas, VisNG2D
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2023-06-20 15:30:21 +00:00
|
|
|
parser.add_argument("--gpus", type=int, default=0)
|
|
|
|
parser.add_argument("--fast_dev_run", type=bool, default=False)
|
2021-06-01 15:19:43 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-06-01 22:21:42 +00:00
|
|
|
# Reproducibility
|
2022-05-17 10:03:43 +00:00
|
|
|
seed_everything(seed=42)
|
2021-06-01 22:21:42 +00:00
|
|
|
|
2021-06-01 15:19:43 +00:00
|
|
|
# Prepare the data
|
2021-06-04 20:21:28 +00:00
|
|
|
train_ds = pt.datasets.Iris(dims=[0, 2])
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=64)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-06-03 13:42:54 +00:00
|
|
|
hparams = dict(
|
|
|
|
num_prototypes=5,
|
2021-06-07 16:35:08 +00:00
|
|
|
input_dim=2,
|
2021-06-03 13:42:54 +00:00
|
|
|
lr=0.1,
|
|
|
|
)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = GrowingNeuralGas(
|
2021-06-03 13:42:54 +00:00
|
|
|
hparams,
|
2021-06-14 18:29:31 +00:00
|
|
|
prototypes_initializer=pt.initializers.ZCI(2),
|
2021-06-03 13:42:54 +00:00
|
|
|
)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
2021-06-04 20:21:28 +00:00
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
2021-06-01 15:19:43 +00:00
|
|
|
# Model summary
|
2022-05-17 10:03:43 +00:00
|
|
|
logging.info(model)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisNG2D(data=train_loader)
|
2021-06-01 15:19:43 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2023-06-20 15:30:21 +00:00
|
|
|
trainer = pl.Trainer(
|
|
|
|
accelerator="cuda" if args.gpus else "cpu",
|
|
|
|
devices=args.gpus if args.gpus else "auto",
|
|
|
|
fast_dev_run=args.fast_dev_run,
|
2022-05-17 10:03:43 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
],
|
2021-06-01 15:19:43 +00:00
|
|
|
max_epochs=100,
|
2022-05-17 10:03:43 +00:00
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-06-01 15:19:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|