2021-04-22 14:01:44 +00:00
|
|
|
"""CBC example using the Iris dataset."""
|
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
|
|
|
|
2021-04-22 14:01:44 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
|
|
|
|
2021-05-30 22:52:16 +00:00
|
|
|
import prototorch as pt
|
|
|
|
|
2021-04-22 14:01:44 +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-04-22 14:01:44 +00:00
|
|
|
# Dataset
|
2021-05-20 15:36:00 +00:00
|
|
|
train_ds = pt.datasets.Iris(dims=[0, 2])
|
2021-04-22 14:01:44 +00:00
|
|
|
|
2021-05-07 13:46:09 +00:00
|
|
|
# Reproducibility
|
2021-05-20 15:36:00 +00:00
|
|
|
pl.utilities.seed.seed_everything(seed=3)
|
2021-05-07 13:46:09 +00:00
|
|
|
|
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(
|
2021-05-20 15:36:00 +00:00
|
|
|
distribution=[3, 2, 2],
|
|
|
|
proto_lr=0.01,
|
|
|
|
bb_lr=0.01,
|
2021-04-23 15:27:47 +00:00
|
|
|
)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Initialize the model
|
2021-05-20 15:36:00 +00:00
|
|
|
model = pt.models.CBC(
|
|
|
|
hparams,
|
|
|
|
prototype_initializer=pt.components.SSI(train_ds, noise=0.01),
|
|
|
|
)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2021-05-21 15:55:55 +00:00
|
|
|
vis = pt.models.VisCBC2D(data=train_ds,
|
|
|
|
title="CBC Iris Example",
|
|
|
|
resolution=300,
|
|
|
|
axis_off=True)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-21 15:55:55 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
|
|
|
callbacks=[vis],
|
2021-04-22 14:01:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
2021-05-07 13:25:04 +00:00
|
|
|
trainer.fit(model, train_loader)
|