2021-04-22 14:01:44 +00:00
|
|
|
"""CBC example using the Iris dataset."""
|
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import warnings
|
2021-05-21 15:55:55 +00:00
|
|
|
|
2021-06-15 13:59:47 +00:00
|
|
|
import prototorch as pt
|
2021-04-22 14:01:44 +00:00
|
|
|
import pytorch_lightning as pl
|
2023-06-20 15:30:21 +00:00
|
|
|
from lightning_fabric.utilities.seed import seed_everything
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import CBC, VisCBC2D
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-05-17 10:03:43 +00:00
|
|
|
# Reproducibility
|
|
|
|
seed_everything(seed=4)
|
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
# Command-line arguments
|
|
|
|
parser = argparse.ArgumentParser()
|
2023-06-20 15:30:21 +00:00
|
|
|
parser.add_argument("--gpus", type=int, default=0)
|
|
|
|
parser.add_argument("--fast_dev_run", type=bool, default=False)
|
2021-05-21 15:55:55 +00:00
|
|
|
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
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=32)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-04-23 15:27:47 +00:00
|
|
|
hparams = dict(
|
2021-06-15 13:59:47 +00:00
|
|
|
distribution=[1, 0, 3],
|
|
|
|
margin=0.1,
|
|
|
|
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
|
2022-05-17 10:03:43 +00:00
|
|
|
model = CBC(
|
2021-05-20 15:36:00 +00:00
|
|
|
hparams,
|
2022-05-17 10:03:43 +00:00
|
|
|
components_initializer=pt.initializers.SSCI(train_ds, noise=0.1),
|
|
|
|
reasonings_initializer=pt.initializers.
|
2021-06-15 13:59:47 +00:00
|
|
|
PurePositiveReasoningsInitializer(),
|
2021-05-20 15:36:00 +00:00
|
|
|
)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisCBC2D(
|
|
|
|
data=train_ds,
|
|
|
|
title="CBC Iris Example",
|
|
|
|
resolution=100,
|
|
|
|
axis_off=True,
|
|
|
|
)
|
2021-04-22 14:01:44 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2023-06-20 15:30:21 +00:00
|
|
|
trainer = pl.Trainer(
|
|
|
|
accelerator="cuda" if args.gpus else "cpu",
|
|
|
|
devices=args.gpus if args.gpus else "auto",
|
|
|
|
fast_dev_run=args.fast_dev_run,
|
2022-05-17 10:03:43 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
],
|
|
|
|
detect_anomaly=True,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
max_epochs=1000,
|
2021-04-22 14:01:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Training loop
|
2021-05-07 13:25:04 +00:00
|
|
|
trainer.fit(model, train_loader)
|