Make siamese example script reproducible
This commit is contained in:
parent
1b9bcf21f6
commit
e87663d10c
@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
import pytorch_lightning as pl
|
import pytorch_lightning as pl
|
||||||
import torch
|
import torch
|
||||||
from prototorch.components import (
|
from prototorch.components import initializers as cinit
|
||||||
StratifiedMeanInitializer
|
|
||||||
)
|
|
||||||
from prototorch.datasets.abstract import NumpyDataset
|
from prototorch.datasets.abstract import NumpyDataset
|
||||||
|
from prototorch.models.callbacks.visualization import VisSiameseGLVQ2D
|
||||||
|
from prototorch.models.glvq import SiameseGLVQ
|
||||||
from sklearn.datasets import load_iris
|
from sklearn.datasets import load_iris
|
||||||
from torch.utils.data import DataLoader
|
from torch.utils.data import DataLoader
|
||||||
|
|
||||||
from prototorch.models.callbacks.visualization import VisSiameseGLVQ2D
|
|
||||||
from prototorch.models.glvq import SiameseGLVQ
|
|
||||||
|
|
||||||
|
|
||||||
class Backbone(torch.nn.Module):
|
class Backbone(torch.nn.Module):
|
||||||
|
"""Two fully connected layers with ReLU activation."""
|
||||||
def __init__(self, input_size=4, hidden_size=10, latent_size=2):
|
def __init__(self, input_size=4, hidden_size=10, latent_size=2):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.input_size = input_size
|
self.input_size = input_size
|
||||||
@ -24,7 +22,9 @@ class Backbone(torch.nn.Module):
|
|||||||
self.relu = torch.nn.ReLU()
|
self.relu = torch.nn.ReLU()
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
return self.relu(self.dense2(self.relu(self.dense1(x))))
|
x = self.relu(self.dense1(x))
|
||||||
|
out = self.relu(self.dense2(x))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -32,16 +32,20 @@ if __name__ == "__main__":
|
|||||||
x_train, y_train = load_iris(return_X_y=True)
|
x_train, y_train = load_iris(return_X_y=True)
|
||||||
train_ds = NumpyDataset(x_train, y_train)
|
train_ds = NumpyDataset(x_train, y_train)
|
||||||
|
|
||||||
|
# Reproducibility
|
||||||
|
pl.utilities.seed.seed_everything(seed=2)
|
||||||
|
|
||||||
# Dataloaders
|
# Dataloaders
|
||||||
train_loader = DataLoader(train_ds, num_workers=0, batch_size=150)
|
train_loader = DataLoader(train_ds, num_workers=0, batch_size=150)
|
||||||
|
|
||||||
# Hyperparameters
|
# Hyperparameters
|
||||||
hparams = dict(
|
hparams = dict(
|
||||||
nclasses=3,
|
nclasses=3,
|
||||||
prototypes_per_class=1,
|
prototypes_per_class=2,
|
||||||
prototype_initializer=StratifiedMeanInitializer(
|
prototype_initializer=cinit.SMI(torch.Tensor(x_train),
|
||||||
torch.Tensor(x_train), torch.Tensor(y_train)),
|
torch.Tensor(y_train)),
|
||||||
lr=0.01,
|
proto_lr=0.001,
|
||||||
|
bb_lr=0.001,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize the model
|
# Initialize the model
|
||||||
@ -54,7 +58,7 @@ if __name__ == "__main__":
|
|||||||
print(model)
|
print(model)
|
||||||
|
|
||||||
# Callbacks
|
# Callbacks
|
||||||
vis = VisSiameseGLVQ2D(x_train, y_train)
|
vis = VisSiameseGLVQ2D(x_train, y_train, border=0.1)
|
||||||
|
|
||||||
# Setup trainer
|
# Setup trainer
|
||||||
trainer = pl.Trainer(max_epochs=100, callbacks=[vis])
|
trainer = pl.Trainer(max_epochs=100, callbacks=[vis])
|
||||||
|
Loading…
Reference in New Issue
Block a user