prototorch_models/examples/gmlvq_iris.py

39 lines
1.0 KiB
Python
Raw Normal View History

2021-05-04 13:11:16 +00:00
"""GMLVQ example using all four dimensions of the Iris dataset."""
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-04 13:11:16 +00:00
if __name__ == "__main__":
# Dataset
2021-05-07 13:25:04 +00:00
from sklearn.datasets import load_iris
2021-05-04 13:11:16 +00:00
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-11 14:15:08 +00:00
nclasses = 3
prototypes_per_class = 1
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-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],
prototype_initializer=pt.components.SMI(train_ds),
2021-05-04 13:11:16 +00:00
lr=0.01,
)
# Initialize the model
2021-05-07 13:25:04 +00:00
model = pt.models.GMLVQ(hparams)
2021-05-04 13:11:16 +00:00
# Setup trainer
2021-05-07 13:25:04 +00:00
trainer = pl.Trainer(max_epochs=100)
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()