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
|
|
|
|
|
2021-06-04 20:21:28 +00:00
|
|
|
import prototorch as pt
|
2021-05-11 15:22:02 +00:00
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
2021-05-21 15:55:55 +00:00
|
|
|
from sklearn.datasets import load_iris
|
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()
|
|
|
|
parser = pl.Trainer.add_argparse_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-05-11 15:22:02 +00:00
|
|
|
# Dataset
|
|
|
|
x_train, y_train = load_iris(return_X_y=True)
|
|
|
|
x_train = x_train[:, [0, 2]]
|
|
|
|
train_ds = pt.datasets.NumpyDataset(x_train, y_train)
|
|
|
|
|
|
|
|
# Dataloaders
|
2021-05-30 22:52:16 +00:00
|
|
|
train_loader = torch.utils.data.DataLoader(train_ds, batch_size=150)
|
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
|
|
|
|
model = pt.models.KNN(hparams, data=train_ds)
|
|
|
|
|
2021-06-04 20:21:28 +00:00
|
|
|
# Compute intermediate input and output sizes
|
|
|
|
model.example_input_array = torch.zeros(4, 2)
|
|
|
|
|
|
|
|
# Summary
|
|
|
|
print(model)
|
|
|
|
|
2021-05-11 15:22:02 +00:00
|
|
|
# Callbacks
|
2021-06-04 20:21:28 +00:00
|
|
|
vis = pt.models.VisGLVQ2D(
|
|
|
|
data=(x_train, y_train),
|
|
|
|
resolution=200,
|
|
|
|
block=True,
|
|
|
|
)
|
2021-05-11 15:22:02 +00:00
|
|
|
|
|
|
|
# Setup trainer
|
2021-05-21 15:55:55 +00:00
|
|
|
trainer = pl.Trainer.from_argparse_args(
|
|
|
|
args,
|
2021-06-04 20:21:28 +00:00
|
|
|
max_epochs=1,
|
2021-05-21 15:55:55 +00:00
|
|
|
callbacks=[vis],
|
2021-06-04 20:21:28 +00:00
|
|
|
weights_summary="full",
|
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
|
|
|
|
y_pred = model.predict(torch.tensor(x_train))
|
|
|
|
print(y_pred)
|