2021-05-04 13:11:16 +00:00
|
|
|
"""GMLVQ example using all four dimensions of the Iris dataset."""
|
|
|
|
|
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
|
|
|
|
import torch
|
2021-05-21 15:55:55 +00:00
|
|
|
from sklearn.datasets import load_iris
|
2021-05-06 12:10:09 +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
|
|
|
|
x_train, y_train = load_iris(return_X_y=True)
|
2021-05-07 13:25:04 +00:00
|
|
|
train_ds = pt.datasets.NumpyDataset(x_train, y_train)
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2021-05-07 13:25:04 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds,
|
|
|
|
num_workers=0,
|
|
|
|
batch_size=150)
|
2021-05-04 13:11:16 +00:00
|
|
|
# Hyperparameters
|
2021-05-25 13:41:10 +00:00
|
|
|
num_classes = 3
|
2021-05-11 14:15:08 +00:00
|
|
|
prototypes_per_class = 1
|
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-04 13:11:16 +00:00
|
|
|
input_dim=x_train.shape[1],
|
2021-05-07 13:25:04 +00:00
|
|
|
latent_dim=x_train.shape[1],
|
2021-05-17 15:03:37 +00:00
|
|
|
proto_lr=0.01,
|
|
|
|
bb_lr=0.01,
|
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
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-21 15:55:55 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(args, )
|
2021-05-04 13:11:16 +00:00
|
|
|
|
|
|
|
# Training loop
|
|
|
|
trainer.fit(model, train_loader)
|
2021-05-07 13:25:04 +00:00
|
|
|
|
|
|
|
# Display the Lambda matrix
|
|
|
|
model.show_lambda()
|