2021-05-07 13:25:04 +00:00
|
|
|
"""Limited Rank Matrix LVQ example using the Tecator dataset."""
|
2021-05-04 13:11:16 +00:00
|
|
|
|
2021-05-07 13:25:04 +00:00
|
|
|
import prototorch as pt
|
2021-05-04 13:11:16 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-05-07 13:25:04 +00:00
|
|
|
import torch
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Dataset
|
2021-05-07 13:25:04 +00:00
|
|
|
train_ds = pt.datasets.Tecator(root="~/datasets/", train=True)
|
2021-05-19 14:30:19 +00:00
|
|
|
test_ds = pt.datasets.Tecator(root="~/datasets/", train=False)
|
2021-05-04 13:11:16 +00:00
|
|
|
|
2021-05-07 13:25:04 +00:00
|
|
|
# Reproducibility
|
|
|
|
pl.utilities.seed.seed_everything(seed=42)
|
2021-05-04 13:11:16 +00:00
|
|
|
|
2021-05-07 13:25:04 +00:00
|
|
|
# Dataloaders
|
2021-05-19 14:30:19 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds, batch_size=32)
|
|
|
|
test_loader = torch.utils.data.DataLoader(test_ds, batch_size=32)
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-05-11 14:15:08 +00:00
|
|
|
nclasses = 2
|
|
|
|
prototypes_per_class = 2
|
2021-05-04 13:11:16 +00:00
|
|
|
hparams = dict(
|
2021-05-11 14:15:08 +00:00
|
|
|
distribution=(nclasses, prototypes_per_class),
|
2021-05-07 13:25:04 +00:00
|
|
|
input_dim=100,
|
2021-05-04 13:11:16 +00:00
|
|
|
latent_dim=2,
|
2021-05-20 12:40:02 +00:00
|
|
|
proto_lr=0.001,
|
|
|
|
bb_lr=0.001,
|
2021-05-04 13:11:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Initialize the model
|
2021-05-12 14:36:22 +00:00
|
|
|
model = pt.models.GMLVQ(hparams,
|
|
|
|
prototype_initializer=pt.components.SMI(train_ds))
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2021-05-07 13:25:04 +00:00
|
|
|
vis = pt.models.VisSiameseGLVQ2D(train_ds, border=0.1)
|
2021-05-20 12:40:02 +00:00
|
|
|
es = pl.callbacks.EarlyStopping(monitor="val_loss",
|
|
|
|
min_delta=0.001,
|
|
|
|
patience=3,
|
|
|
|
verbose=False,
|
|
|
|
mode="min")
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-19 14:30:19 +00:00
|
|
|
trainer = pl.Trainer(
|
|
|
|
gpus=0,
|
2021-05-20 12:40:02 +00:00
|
|
|
max_epochs=100,
|
|
|
|
callbacks=[vis, es],
|
2021-05-19 14:30:19 +00:00
|
|
|
weights_summary=None,
|
|
|
|
)
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Training loop
|
2021-05-19 14:30:19 +00:00
|
|
|
trainer.fit(model, train_loader, test_loader)
|
2021-05-10 12:30:02 +00:00
|
|
|
|
|
|
|
# Save the model
|
|
|
|
torch.save(model, "liramlvq_tecator.pt")
|
|
|
|
|
|
|
|
# Load a saved model
|
|
|
|
saved_model = torch.load("liramlvq_tecator.pt")
|
|
|
|
|
|
|
|
# Display the Lambda matrix
|
|
|
|
saved_model.show_lambda()
|
2021-05-19 14:30:19 +00:00
|
|
|
|
|
|
|
# Testing
|
2021-05-20 12:40:02 +00:00
|
|
|
trainer.test(model, test_dataloaders=test_loader)
|