prototorch_models/examples/ng_iris.py

78 lines
2.1 KiB
Python
Raw Normal View History

2021-04-23 15:38:29 +00:00
"""Neural Gas example using the Iris dataset."""
2021-04-23 15:30:23 +00:00
2021-05-21 15:55:55 +00:00
import argparse
import warnings
2021-05-21 15:55:55 +00:00
2021-06-21 12:59:54 +00:00
import prototorch as pt
2021-04-23 15:30:23 +00:00
import pytorch_lightning as pl
2021-05-07 13:25:04 +00:00
import torch
from lightning_fabric.utilities.seed import seed_everything
from prototorch.models import NeuralGas, VisNG2D
from pytorch_lightning.utilities.warnings import PossibleUserWarning
2021-05-21 15:55:55 +00:00
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
2021-06-04 20:21:28 +00:00
from torch.optim.lr_scheduler import ExponentialLR
from torch.utils.data import DataLoader
warnings.filterwarnings("ignore", category=PossibleUserWarning)
warnings.filterwarnings("ignore", category=UserWarning)
2021-04-23 15:30:23 +00:00
if __name__ == "__main__":
# Reproducibility
seed_everything(seed=4)
2021-05-21 15:55:55 +00:00
# Command-line arguments
parser = argparse.ArgumentParser()
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-05-07 13:25:04 +00:00
# Prepare and pre-process the dataset
2021-04-23 15:30:23 +00:00
x_train, y_train = load_iris(return_X_y=True)
x_train = x_train[:, 0:3:2]
2021-04-23 15:30:23 +00:00
scaler = StandardScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
2021-05-07 13:25:04 +00:00
train_ds = pt.datasets.NumpyDataset(x_train, y_train)
2021-04-23 15:30:23 +00:00
# Dataloaders
train_loader = DataLoader(train_ds, batch_size=150)
2021-04-23 15:30:23 +00:00
# Hyperparameters
2021-06-07 16:35:08 +00:00
hparams = dict(
num_prototypes=30,
input_dim=2,
lr=0.03,
)
2021-04-23 15:30:23 +00:00
# Initialize the model
model = NeuralGas(
2021-06-04 20:21:28 +00:00
hparams,
2021-06-21 12:59:54 +00:00
prototypes_initializer=pt.core.ZCI(2),
2021-06-04 20:21:28 +00:00
lr_scheduler=ExponentialLR,
lr_scheduler_kwargs=dict(gamma=0.99, verbose=False),
)
# Compute intermediate input and output sizes
model.example_input_array = torch.zeros(4, 2)
2021-04-23 15:30:23 +00:00
# Callbacks
vis = VisNG2D(data=train_ds)
2021-04-23 15:30:23 +00:00
# Setup trainer
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,
callbacks=[
vis,
],
max_epochs=1000,
log_every_n_steps=1,
detect_anomaly=True,
2021-05-21 15:55:55 +00:00
)
2021-04-23 15:30:23 +00:00
# Training loop
trainer.fit(model, train_loader)