prototorch_models/examples/cbc_iris.py

49 lines
1.2 KiB
Python
Raw Normal View History

2021-04-22 14:01:44 +00:00
"""CBC example using the Iris dataset."""
2021-05-07 13:25:04 +00:00
import prototorch as pt
2021-04-22 14:01:44 +00:00
import pytorch_lightning as pl
import torch
if __name__ == "__main__":
# Dataset
2021-05-07 13:25:04 +00:00
from sklearn.datasets import load_iris
2021-04-22 14:01:44 +00:00
x_train, y_train = load_iris(return_X_y=True)
x_train = x_train[:, [0, 2]]
2021-05-07 13:25:04 +00:00
train_ds = pt.datasets.NumpyDataset(x_train, y_train)
2021-04-22 14:01:44 +00:00
2021-05-07 13:46:09 +00:00
# Reproducibility
pl.utilities.seed.seed_everything(seed=2)
2021-04-22 14:01:44 +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-04-22 14:01:44 +00:00
# Hyperparameters
2021-04-23 15:27:47 +00:00
hparams = dict(
input_dim=x_train.shape[1],
2021-05-07 13:25:04 +00:00
nclasses=3,
2021-05-07 13:46:09 +00:00
num_components=5,
component_initializer=pt.components.SSI(train_ds, noise=0.01),
2021-04-23 15:27:47 +00:00
lr=0.01,
)
2021-04-22 14:01:44 +00:00
# Initialize the model
2021-05-07 13:25:04 +00:00
model = pt.models.CBC(hparams)
2021-04-22 14:01:44 +00:00
# Callbacks
2021-05-07 13:25:04 +00:00
dvis = pt.models.VisCBC2D(data=(x_train, y_train),
title="CBC Iris Example")
2021-04-22 14:01:44 +00:00
# Setup trainer
trainer = pl.Trainer(
2021-05-13 13:22:01 +00:00
gpus=-1,
2021-05-07 13:46:09 +00:00
max_epochs=200,
2021-04-22 14:01:44 +00:00
callbacks=[
dvis,
2021-04-22 14:01:44 +00:00
],
)
# Training loop
2021-05-07 13:25:04 +00:00
trainer.fit(model, train_loader)