2021-06-14 18:42:57 +00:00
|
|
|
"""Warm-starting GLVQ with prototypes from Growing Neural Gas."""
|
|
|
|
|
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-06-14 18:42:57 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import (
|
|
|
|
GLVQ,
|
|
|
|
KNN,
|
|
|
|
GrowingNeuralGas,
|
|
|
|
PruneLoserPrototypes,
|
|
|
|
VisGLVQ2D,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.callbacks import EarlyStopping
|
|
|
|
from pytorch_lightning.utilities.seed import seed_everything
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
2021-06-14 18:42:57 +00:00
|
|
|
from torch.optim.lr_scheduler import ExponentialLR
|
2022-05-17 10:03:43 +00:00
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
2021-06-14 18:42:57 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
|
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
2021-06-14 18:42:57 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Prepare the data
|
|
|
|
train_ds = pt.datasets.Iris(dims=[0, 2])
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=64, num_workers=0)
|
2021-06-14 18:42:57 +00:00
|
|
|
|
|
|
|
# Initialize the gng
|
2022-05-17 10:03:43 +00:00
|
|
|
gng = GrowingNeuralGas(
|
2021-06-14 18:42:57 +00:00
|
|
|
hparams=dict(num_prototypes=5, insert_freq=2, lr=0.1),
|
|
|
|
prototypes_initializer=pt.initializers.ZCI(2),
|
|
|
|
lr_scheduler=ExponentialLR,
|
|
|
|
lr_scheduler_kwargs=dict(gamma=0.99, verbose=False),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
es = EarlyStopping(
|
2021-06-14 18:42:57 +00:00
|
|
|
monitor="loss",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=20,
|
|
|
|
mode="min",
|
|
|
|
verbose=False,
|
|
|
|
check_on_train_epoch_end=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Setup trainer for GNG
|
|
|
|
trainer = pl.Trainer(
|
2022-05-17 10:03:43 +00:00
|
|
|
max_epochs=1000,
|
|
|
|
callbacks=[
|
|
|
|
es,
|
|
|
|
],
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-06-14 18:42:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(gng, train_loader)
|
|
|
|
|
|
|
|
# Hyperparameters
|
|
|
|
hparams = dict(
|
|
|
|
distribution=[],
|
|
|
|
lr=0.01,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Warm-start prototypes
|
2022-05-17 10:03:43 +00:00
|
|
|
knn = KNN(dict(k=1), data=train_ds)
|
2021-06-14 18:42:57 +00:00
|
|
|
prototypes = gng.prototypes
|
|
|
|
plabels = knn.predict(prototypes)
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = GLVQ(
|
2021-06-14 18:42:57 +00:00
|
|
|
hparams,
|
|
|
|
optimizer=torch.optim.Adam,
|
|
|
|
prototypes_initializer=pt.initializers.LCI(prototypes),
|
|
|
|
labels_initializer=pt.initializers.LLI(plabels),
|
|
|
|
lr_scheduler=ExponentialLR,
|
|
|
|
lr_scheduler_kwargs=dict(gamma=0.99, verbose=False),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisGLVQ2D(data=train_ds)
|
|
|
|
pruning = PruneLoserPrototypes(
|
2021-06-30 14:04:26 +00:00
|
|
|
threshold=0.02,
|
|
|
|
idle_epochs=2,
|
|
|
|
prune_quota_per_epoch=5,
|
|
|
|
frequency=1,
|
|
|
|
verbose=True,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
es = EarlyStopping(
|
2021-06-30 14:04:26 +00:00
|
|
|
monitor="train_loss",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=10,
|
|
|
|
mode="min",
|
|
|
|
verbose=True,
|
|
|
|
check_on_train_epoch_end=True,
|
|
|
|
)
|
2021-06-14 18:42:57 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
2021-06-30 14:04:26 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
pruning,
|
|
|
|
es,
|
|
|
|
],
|
2022-05-17 10:03:43 +00:00
|
|
|
max_epochs=1000,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-06-14 18:42:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|