2021-06-04 20:21:28 +00:00
|
|
|
"""k-NN example using the Iris dataset from scikit-learn."""
|
2021-05-11 15:22:02 +00:00
|
|
|
|
2021-05-21 15:55:55 +00:00
|
|
|
import argparse
|
2022-05-17 10:03:43 +00:00
|
|
|
import logging
|
|
|
|
import warnings
|
2021-05-21 15:55:55 +00:00
|
|
|
|
2021-06-25 14:56:10 +00:00
|
|
|
import prototorch as pt
|
2021-05-11 15:22:02 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2022-05-17 10:03:43 +00:00
|
|
|
from prototorch.models import KNN, VisGLVQ2D
|
|
|
|
from pytorch_lightning.utilities.warnings import PossibleUserWarning
|
2021-05-21 15:55:55 +00:00
|
|
|
from sklearn.datasets import load_iris
|
2021-09-10 17:19:51 +00:00
|
|
|
from sklearn.model_selection import train_test_split
|
2022-05-17 10:03:43 +00:00
|
|
|
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=PossibleUserWarning)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
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-05-11 15:22:02 +00:00
|
|
|
# Dataset
|
2021-09-10 17:19:51 +00:00
|
|
|
X, y = load_iris(return_X_y=True)
|
2022-05-17 10:03:43 +00:00
|
|
|
X = X[:, 0:3:2]
|
2021-09-10 17:19:51 +00:00
|
|
|
|
2022-05-17 10:03:43 +00:00
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
|
|
X,
|
|
|
|
y,
|
|
|
|
test_size=0.5,
|
|
|
|
random_state=42,
|
|
|
|
)
|
2021-09-10 17:19:51 +00:00
|
|
|
|
|
|
|
train_ds = pt.datasets.NumpyDataset(X_train, y_train)
|
|
|
|
test_ds = pt.datasets.NumpyDataset(X_test, y_test)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
# Dataloaders
|
2022-05-17 10:03:43 +00:00
|
|
|
train_loader = DataLoader(train_ds, batch_size=16)
|
|
|
|
test_loader = DataLoader(test_ds, batch_size=16)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
# Hyperparameters
|
2021-06-04 20:21:28 +00:00
|
|
|
hparams = dict(k=5)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
# Initialize the model
|
2022-05-17 10:03:43 +00:00
|
|
|
model = KNN(hparams, data=train_ds)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
2021-06-04 20:21:28 +00:00
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
|
|
|
# Summary
|
2022-05-17 10:03:43 +00:00
|
|
|
logging.info(model)
|
2021-06-04 20:21:28 +00:00
|
|
|
|
2021-05-11 15:22:02 +00:00
|
|
|
# Callbacks
|
2022-05-17 10:03:43 +00:00
|
|
|
vis = VisGLVQ2D(
|
2021-09-10 17:19:51 +00:00
|
|
|
data=(X_train, y_train),
|
2021-06-04 20:21:28 +00:00
|
|
|
resolution=200,
|
|
|
|
block=True,
|
|
|
|
)
|
2021-05-11 15:22:02 +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,
|
2021-06-04 20:21:28 +00:00
|
|
|
max_epochs=1,
|
2022-05-17 10:03:43 +00:00
|
|
|
callbacks=[
|
|
|
|
vis,
|
|
|
|
],
|
|
|
|
log_every_n_steps=1,
|
|
|
|
detect_anomaly=True,
|
2021-05-21 15:55:55 +00:00
|
|
|
)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
# Training loop
|
|
|
|
# This is only for visualization. k-NN has no training phase.
|
|
|
|
trainer.fit(model, train_loader)
|
|
|
|
|
|
|
|
# Recall
|
2021-09-10 17:19:51 +00:00
|
|
|
y_pred = model.predict(torch.tensor(X_train))
|
2022-05-17 10:03:43 +00:00
|
|
|
logging.info(y_pred)
|
2021-09-10 17:19:51 +00:00
|
|
|
|
|
|
|
# Test
|
|
|
|
trainer.test(model, dataloaders=test_loader)
|