Update examples

This commit is contained in:
Jensun Ravichandran 2021-05-17 17:03:37 +02:00
parent 81346785bd
commit d812bb0620
7 changed files with 69 additions and 10 deletions

View File

@ -28,12 +28,12 @@ The plugin should then be available for use in your Python environment as
- Generalized Relevance Learning Vector Quantization (GRLVQ) - Generalized Relevance Learning Vector Quantization (GRLVQ)
- Generalized Matrix Learning Vector Quantization (GMLVQ) - Generalized Matrix Learning Vector Quantization (GMLVQ)
- Limited-Rank Matrix Learning Vector Quantization (LiRaMLVQ) - Limited-Rank Matrix Learning Vector Quantization (LiRaMLVQ)
- Learning Vector Quantization Multi-Layer Network (LVQMLN)
- Siamese GLVQ - Siamese GLVQ
- Neural Gas (NG) - Neural Gas (NG)
## Work in Progress ## Work in Progress
- Learning Vector Quantization Multi-Layer Network (LVQMLN)
- Classification-By-Components Network (CBC) - Classification-By-Components Network (CBC)
- Learning Vector Quantization 2.1 (LVQ2.1) - Learning Vector Quantization 2.1 (LVQ2.1)

View File

@ -30,7 +30,7 @@ if __name__ == "__main__":
noise=1e-1)) noise=1e-1))
# Callbacks # Callbacks
vis = pt.models.VisGLVQ2D(train_ds, show_last_only=False, block=True) vis = pt.models.VisGLVQ2D(train_ds, show_last_only=True, block=True)
snan = StopOnNaN(model.proto_layer.components) snan = StopOnNaN(model.proto_layer.components)
# Setup trainer # Setup trainer

View File

@ -21,7 +21,8 @@ if __name__ == "__main__":
distribution=(nclasses, prototypes_per_class), distribution=(nclasses, prototypes_per_class),
input_dim=x_train.shape[1], input_dim=x_train.shape[1],
latent_dim=x_train.shape[1], latent_dim=x_train.shape[1],
lr=0.01, proto_lr=0.01,
bb_lr=0.01,
) )
# Initialize the model # Initialize the model

View File

@ -23,7 +23,7 @@ if __name__ == "__main__":
model = pt.models.KNN(hparams, data=train_ds) model = pt.models.KNN(hparams, data=train_ds)
# Callbacks # Callbacks
vis = pt.models.VisGLVQ2D(data=(x_train, y_train)) vis = pt.models.VisGLVQ2D(data=(x_train, y_train), resolution=200)
# Setup trainer # Setup trainer
trainer = pl.Trainer(max_epochs=1, callbacks=[vis], gpus=0) trainer = pl.Trainer(max_epochs=1, callbacks=[vis], gpus=0)

View File

@ -23,7 +23,8 @@ if __name__ == "__main__":
distribution=(nclasses, prototypes_per_class), distribution=(nclasses, prototypes_per_class),
input_dim=100, input_dim=100,
latent_dim=2, latent_dim=2,
lr=0.001, proto_lr=0.001,
bb_lr=0.001,
) )
# Initialize the model # Initialize the model

54
examples/lvqmln_iris.py Normal file
View File

@ -0,0 +1,54 @@
"""LVQMLN example using all four dimensions of the Iris dataset."""
import prototorch as pt
import pytorch_lightning as pl
import torch
from siamese_glvq_iris import Backbone
if __name__ == "__main__":
# Dataset
train_ds = pt.datasets.Iris()
# Reproducibility
pl.utilities.seed.seed_everything(seed=42)
# Dataloaders
train_loader = torch.utils.data.DataLoader(train_ds,
num_workers=0,
batch_size=150)
# Hyperparameters
hparams = dict(
distribution=[1, 2, 2],
proto_lr=0.001,
bb_lr=0.001,
)
# Initialize the backbone
backbone = Backbone()
# Initialize the model
model = pt.models.LVQMLN(
hparams,
prototype_initializer=pt.components.SSI(train_ds, transform=backbone),
backbone=backbone,
)
# Model summary
print(model)
# Callbacks
vis = pt.models.VisSiameseGLVQ2D(
data=train_ds,
map_protos=False,
border=0.1,
resolution=500,
axis_off=True,
)
# Setup trainer
trainer = pl.Trainer(max_epochs=100, callbacks=[vis], gpus=0)
# Training loop
trainer.fit(model, train_loader)

View File

@ -6,7 +6,6 @@ import torch
class Backbone(torch.nn.Module): class Backbone(torch.nn.Module):
"""Two fully connected layers with ReLU activation."""
def __init__(self, input_size=4, hidden_size=10, latent_size=2): def __init__(self, input_size=4, hidden_size=10, latent_size=2):
super().__init__() super().__init__()
self.input_size = input_size self.input_size = input_size
@ -14,11 +13,11 @@ class Backbone(torch.nn.Module):
self.latent_size = latent_size self.latent_size = latent_size
self.dense1 = torch.nn.Linear(self.input_size, self.hidden_size) self.dense1 = torch.nn.Linear(self.input_size, self.hidden_size)
self.dense2 = torch.nn.Linear(self.hidden_size, self.latent_size) self.dense2 = torch.nn.Linear(self.hidden_size, self.latent_size)
self.relu = torch.nn.ReLU() self.activation = torch.nn.Sigmoid()
def forward(self, x): def forward(self, x):
x = self.relu(self.dense1(x)) x = self.activation(self.dense1(x))
out = self.relu(self.dense2(x)) out = self.activation(self.dense2(x))
return out return out
@ -41,11 +40,15 @@ if __name__ == "__main__":
bb_lr=0.01, bb_lr=0.01,
) )
# Initialize the backbone
backbone = Backbone()
# Initialize the model # Initialize the model
model = pt.models.SiameseGLVQ( model = pt.models.SiameseGLVQ(
hparams, hparams,
prototype_initializer=pt.components.SMI(train_ds), prototype_initializer=pt.components.SMI(train_ds),
backbone_module=Backbone, backbone=backbone,
both_path_gradients=True,
) )
# Model summary # Model summary