prototorch_models/examples/liramlvq_tecator.py

69 lines
1.8 KiB
Python
Raw Normal View History

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-21 15:55:55 +00:00
import argparse
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__":
2021-05-21 15:55:55 +00:00
# Command-line arguments
parser = argparse.ArgumentParser()
parser = pl.Trainer.add_argparse_args(parser)
args = parser.parse_args()
2021-05-04 13:11:16 +00:00
# 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-25 13:41:10 +00:00
num_classes = 2
2021-05-11 14:15:08 +00:00
prototypes_per_class = 2
2021-05-04 13:11:16 +00:00
hparams = dict(
2021-05-25 13:41:10 +00:00
distribution=(num_classes, 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
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-21 15:55:55 +00:00
trainer = pl.Trainer.from_argparse_args(
args,
2021-05-20 12:40:02 +00:00
callbacks=[vis, es],
2021-05-19 14:30:19 +00:00
)
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)