prototorch_models/examples/ng_iris.py

41 lines
1.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-07 13:25:04 +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
2021-04-23 15:30:23 +00:00
if __name__ == "__main__":
2021-05-07 13:25:04 +00:00
# Prepare and pre-process the dataset
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
2021-04-23 15:30:23 +00:00
x_train, y_train = load_iris(return_X_y=True)
x_train = x_train[:, [0, 2]]
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
2021-05-07 13:25:04 +00:00
train_loader = torch.utils.data.DataLoader(train_ds,
num_workers=0,
batch_size=150)
2021-04-23 15:30:23 +00:00
# Hyperparameters
2021-05-07 13:25:04 +00:00
hparams = dict(num_prototypes=30, lr=0.03)
2021-04-23 15:30:23 +00:00
# Initialize the model
2021-05-07 13:25:04 +00:00
model = pt.models.NeuralGas(hparams)
2021-04-23 15:30:23 +00:00
# Model summary
print(model)
# Callbacks
2021-05-07 13:25:04 +00:00
vis = pt.models.VisNG2D(data=train_ds)
2021-04-23 15:30:23 +00:00
# Setup trainer
2021-05-13 13:22:01 +00:00
trainer = pl.Trainer(gpus=-1, max_epochs=200, callbacks=[vis])
2021-04-23 15:30:23 +00:00
# Training loop
trainer.fit(model, train_loader)