prototorch_models/examples/liramlvq_tecator.py

90 lines
2.2 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
import matplotlib.pyplot as plt
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
def plot_matrix(matrix):
title = "Lambda matrix"
plt.figure(title)
plt.title(title)
plt.imshow(matrix, cmap="gray")
plt.axis("off")
plt.colorbar()
plt.show(block=True)
2021-05-30 22:52:16 +00:00
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=10)
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
hparams = dict(
2021-05-30 22:52:16 +00:00
distribution={
"num_classes": 2,
"prototypes_per_class": 1
2021-05-30 22:52:16 +00:00
},
2021-05-07 13:25:04 +00:00
input_dim=100,
2021-05-04 13:11:16 +00:00
latent_dim=2,
proto_lr=0.0001,
bb_lr=0.0001,
2021-05-04 13:11:16 +00:00
)
# Initialize the model
model = pt.models.SiameseGMLVQ(
hparams,
# optimizer=torch.optim.SGD,
optimizer=torch.optim.Adam,
prototype_initializer=pt.components.SMI(train_ds),
)
# Summary
print(model)
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=50,
2021-05-20 12:40:02 +00:00
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],
weights_summary=None,
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
plot_matrix(saved_model.lambda_matrix)
2021-05-19 14:30:19 +00:00
# Testing
2021-05-20 12:40:02 +00:00
trainer.test(model, test_dataloaders=test_loader)