2021-06-02 00:40:29 +00:00
|
|
|
"""Dynamically prune 'loser' prototypes in GLVQ-type models."""
|
2021-06-02 00:36:37 +00:00
|
|
|
|
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import logging
|
|
|
|
import warnings
|
2021-06-02 00:36:37 +00:00
|
|
|
|
|
|
|
import prototorch as pt
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
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 (
|
|
|
|
CELVQ,
|
|
|
|
PruneLoserPrototypes,
|
|
|
|
VisGLVQ2D,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.callbacks import EarlyStopping
|
|
|
|
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-02 00:36:37 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
|
|
|
|
2021-06-02 00:36:37 +00:00
|
|
|
# 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-02 00:36:37 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Dataset
|
|
|
|
num_classes = 4
|
|
|
|
num_features = 2
|
|
|
|
num_clusters = 1
|
2022-05-17 10:03:43 +00:00
|
|
|
train_ds = pt.datasets.Random(
|
|
|
|
num_samples=500,
|
|
|
|
num_classes=num_classes,
|
|
|
|
num_features=num_features,
|
|
|
|
num_clusters=num_clusters,
|
|
|
|
separation=3.0,
|
|
|
|
seed=42,
|
|
|
|
)
|
2021-06-02 00:36:37 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=256)
|
2021-06-02 00:36:37 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
|
|
|
prototypes_per_class = num_clusters * 5
|
|
|
|
hparams = dict(
|
|
|
|
distribution=(num_classes, prototypes_per_class),
|
2021-06-04 20:21:28 +00:00
|
|
|
lr=0.2,
|
2021-06-02 00:36:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = CELVQ(
|
2021-06-02 00:36:37 +00:00
|
|
|
hparams,
|
2021-06-14 18:31:39 +00:00
|
|
|
prototypes_initializer=pt.initializers.FVCI(2, 3.0),
|
2021-06-02 00:36:37 +00:00
|
|
|
)
|
|
|
|
|
2021-06-04 20:21:28 +00:00
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
|
|
|
# Summary
|
2022-05-17 10:03:43 +00:00
|
|
|
logging.info(model)
|
2021-06-04 20:21:28 +00:00
|
|
|
|
2021-06-02 00:36:37 +00:00
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisGLVQ2D(train_ds)
|
|
|
|
pruning = PruneLoserPrototypes(
|
2021-06-02 00:36:37 +00:00
|
|
|
threshold=0.01, # prune prototype if it wins less than 1%
|
2021-06-04 13:56:46 +00:00
|
|
|
idle_epochs=20, # pruning too early may cause problems
|
|
|
|
prune_quota_per_epoch=2, # prune at most 2 prototypes per epoch
|
|
|
|
frequency=1, # prune every epoch
|
2021-06-02 00:36:37 +00:00
|
|
|
verbose=True,
|
|
|
|
)
|
2022-05-17 10:03:43 +00:00
|
|
|
es = EarlyStopping(
|
2021-06-03 11:38:16 +00:00
|
|
|
monitor="train_loss",
|
|
|
|
min_delta=0.001,
|
2021-06-04 13:56:46 +00:00
|
|
|
patience=20,
|
2021-06-02 10:44:34 +00:00
|
|
|
mode="min",
|
2021-06-04 13:56:46 +00:00
|
|
|
verbose=True,
|
2021-06-03 11:38:16 +00:00
|
|
|
check_on_train_epoch_end=True,
|
2021-06-02 10:44:34 +00:00
|
|
|
)
|
2021-06-02 00:36:37 +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,
|
2021-06-02 00:36:37 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
pruning,
|
2021-06-02 10:44:34 +00:00
|
|
|
es,
|
2021-06-02 00:36:37 +00:00
|
|
|
],
|
2022-05-17 10:03:43 +00:00
|
|
|
detect_anomaly=True,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
max_epochs=1000,
|
2021-06-02 00:36:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|