[WIP] Update examples/liramlvq_tecator.py

This commit is contained in:
Jensun Ravichandran 2021-06-02 00:02:31 +02:00
parent 757f4e980d
commit 7e241ff7d8

View File

@ -2,10 +2,21 @@
import argparse import argparse
import matplotlib.pyplot as plt
import prototorch as pt
import pytorch_lightning as pl import pytorch_lightning as pl
import torch import torch
import prototorch as pt
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)
if __name__ == "__main__": if __name__ == "__main__":
# Command-line arguments # Command-line arguments
@ -18,7 +29,7 @@ if __name__ == "__main__":
test_ds = pt.datasets.Tecator(root="~/datasets/", train=False) test_ds = pt.datasets.Tecator(root="~/datasets/", train=False)
# Reproducibility # Reproducibility
pl.utilities.seed.seed_everything(seed=42) pl.utilities.seed.seed_everything(seed=10)
# Dataloaders # Dataloaders
train_loader = torch.utils.data.DataLoader(train_ds, batch_size=32) train_loader = torch.utils.data.DataLoader(train_ds, batch_size=32)
@ -28,23 +39,30 @@ if __name__ == "__main__":
hparams = dict( hparams = dict(
distribution={ distribution={
"num_classes": 2, "num_classes": 2,
"prototypes_per_class": 2 "prototypes_per_class": 1
}, },
input_dim=100, input_dim=100,
latent_dim=2, latent_dim=2,
proto_lr=0.001, proto_lr=0.0001,
bb_lr=0.001, bb_lr=0.0001,
) )
# Initialize the model # Initialize the model
model = pt.models.GMLVQ(hparams, model = pt.models.SiameseGMLVQ(
prototype_initializer=pt.components.SMI(train_ds)) hparams,
# optimizer=torch.optim.SGD,
optimizer=torch.optim.Adam,
prototype_initializer=pt.components.SMI(train_ds),
)
# Summary
print(model)
# Callbacks # Callbacks
vis = pt.models.VisSiameseGLVQ2D(train_ds, border=0.1) vis = pt.models.VisSiameseGLVQ2D(train_ds, border=0.1)
es = pl.callbacks.EarlyStopping(monitor="val_loss", es = pl.callbacks.EarlyStopping(monitor="val_loss",
min_delta=0.001, min_delta=0.001,
patience=3, patience=50,
verbose=False, verbose=False,
mode="min") mode="min")
@ -52,6 +70,7 @@ if __name__ == "__main__":
trainer = pl.Trainer.from_argparse_args( trainer = pl.Trainer.from_argparse_args(
args, args,
callbacks=[vis, es], callbacks=[vis, es],
weights_summary=None,
) )
# Training loop # Training loop
@ -64,7 +83,7 @@ if __name__ == "__main__":
saved_model = torch.load("liramlvq_tecator.pt") saved_model = torch.load("liramlvq_tecator.pt")
# Display the Lambda matrix # Display the Lambda matrix
saved_model.show_lambda() plot_matrix(saved_model.lambda_matrix)
# Testing # Testing
trainer.test(model, test_dataloaders=test_loader) trainer.test(model, test_dataloaders=test_loader)