From 1636c847789a35298b2f38333dd56ae768952a04 Mon Sep 17 00:00:00 2001 From: Jensun Ravichandran Date: Mon, 31 May 2021 17:56:45 +0200 Subject: [PATCH] Rename rslvq example --- examples/{probabilistic.py => rslvq_iris.py} | 17 ++++++++++------- prototorch/models/__init__.py | 2 +- prototorch/models/probabilistic.py | 14 ++++---------- 3 files changed, 15 insertions(+), 18 deletions(-) rename examples/{probabilistic.py => rslvq_iris.py} (72%) diff --git a/examples/probabilistic.py b/examples/rslvq_iris.py similarity index 72% rename from examples/probabilistic.py rename to examples/rslvq_iris.py index 38df171..c8ec2d3 100644 --- a/examples/probabilistic.py +++ b/examples/rslvq_iris.py @@ -13,6 +13,9 @@ if __name__ == "__main__": parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() + # Reproducibility + pl.utilities.seed.seed_everything(seed=42) + # Dataset train_ds = pt.datasets.Iris(dims=[0, 2]) @@ -20,20 +23,17 @@ if __name__ == "__main__": train_loader = torch.utils.data.DataLoader(train_ds, batch_size=64) # Hyperparameters - num_classes = 3 - prototypes_per_class = 2 hparams = dict( - distribution=(num_classes, prototypes_per_class), + distribution=[2, 2, 3], lr=0.05, - variance=1.0, + variance=0.3, ) # Initialize the model - model = pt.models.probabilistic.LikelihoodRatioLVQ( + model = pt.models.probabilistic.RSLVQ( hparams, optimizer=torch.optim.Adam, - # prototype_initializer=pt.components.UniformInitializer(2), - prototype_initializer=pt.components.SMI(train_ds), + prototype_initializer=pt.components.SSI(train_ds, noise=0.2), ) print(model) @@ -45,6 +45,9 @@ if __name__ == "__main__": trainer = pl.Trainer.from_argparse_args( args, callbacks=[vis], + terminate_on_nan=True, + weights_summary=None, + # accelerator="ddp", ) # Training loop diff --git a/prototorch/models/__init__.py b/prototorch/models/__init__.py index 6666df7..2764f78 100644 --- a/prototorch/models/__init__.py +++ b/prototorch/models/__init__.py @@ -1,6 +1,6 @@ from importlib.metadata import PackageNotFoundError, version -from . import probabilistic +from .probabilistic import LikelihoodRatioLVQ, RSLVQ from .cbc import CBC, ImageCBC from .glvq import (CELVQ, GLVQ, GLVQ1, GLVQ21, GMLVQ, GRLVQ, LVQMLN, ImageGLVQ, ImageGMLVQ, SiameseGLVQ) diff --git a/prototorch/models/probabilistic.py b/prototorch/models/probabilistic.py index 865d3c0..72b482d 100644 --- a/prototorch/models/probabilistic.py +++ b/prototorch/models/probabilistic.py @@ -2,15 +2,14 @@ import torch from prototorch.functions.competitions import stratified_sum -from prototorch.functions.losses import (log_likelihood_ratio_loss, - robust_soft_loss) +from prototorch.functions.losses import log_likelihood_ratio_loss, robust_soft_loss from prototorch.functions.transform import gaussian from .glvq import GLVQ class ProbabilisticLVQ(GLVQ): - def __init__(self, hparams, rejection_confidence=1.0, **kwargs): + def __init__(self, hparams, rejection_confidence=0.0, **kwargs): super().__init__(hparams, **kwargs) self.conditional_distribution = gaussian @@ -45,19 +44,14 @@ class ProbabilisticLVQ(GLVQ): class LikelihoodRatioLVQ(ProbabilisticLVQ): - """Learning Vector Quantization based on Likelihood Ratios - """ + """Learning Vector Quantization based on Likelihood Ratios.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.loss_fn = log_likelihood_ratio_loss class RSLVQ(ProbabilisticLVQ): - """Learning Vector Quantization based on Likelihood Ratios - """ + """Robust Soft Learning Vector Quantization.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.loss_fn = robust_soft_loss - - -__all__ = ["LikelihoodRatioLVQ", "RSLVQ"]