Update examples/new_components.py to use the new API

This commit is contained in:
Jensun Ravichandran 2021-06-16 13:39:28 +02:00
parent 7763a57058
commit c95f91cc29

View File

@ -1,39 +1,35 @@
"""This example script shows the usage of the new components architecture. """This example script shows the usage of the new components architecture.
Serialization/deserialization also works as expected. Serialization/deserialization also works as expected.
""" """
# DATASET
import torch import torch
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler() import prototorch as pt
x_train, y_train = load_iris(return_X_y=True)
x_train = x_train[:, [0, 2]]
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_train = torch.Tensor(x_train) ds = pt.datasets.Iris()
y_train = torch.Tensor(y_train)
num_classes = len(torch.unique(y_train))
# CREATE NEW COMPONENTS unsupervised = pt.components.Components(
from prototorch.components import * 6,
from prototorch.components.initializers import * initializer=pt.initializers.ZCI(2),
)
unsupervised = Components(6, SelectionInitializer(x_train))
print(unsupervised()) print(unsupervised())
prototypes = LabeledComponents( prototypes = pt.components.LabeledComponents(
(3, 2), StratifiedSelectionInitializer(x_train, y_train)) (3, 2),
components_initializer=pt.initializers.SSCI(ds),
)
print(prototypes()) print(prototypes())
components = ReasoningComponents( components = pt.components.ReasoningComponents(
(3, 6), StratifiedSelectionInitializer(x_train, y_train)) (3, 2),
print(components()) components_initializer=pt.initializers.SSCI(ds),
reasonings_initializer=pt.initializers.PPRI(),
)
print(prototypes())
# TEST SERIALIZATION # Test Serialization
import io import io
save = io.BytesIO() save = io.BytesIO()
@ -41,25 +37,20 @@ torch.save(unsupervised, save)
save.seek(0) save.seek(0)
serialized_unsupervised = torch.load(save) serialized_unsupervised = torch.load(save)
assert torch.all(unsupervised.components == serialized_unsupervised.components assert torch.all(unsupervised.components == serialized_unsupervised.components)
), "Serialization of Components failed."
save = io.BytesIO() save = io.BytesIO()
torch.save(prototypes, save) torch.save(prototypes, save)
save.seek(0) save.seek(0)
serialized_prototypes = torch.load(save) serialized_prototypes = torch.load(save)
assert torch.all(prototypes.components == serialized_prototypes.components assert torch.all(prototypes.components == serialized_prototypes.components)
), "Serialization of Components failed." assert torch.all(prototypes.labels == serialized_prototypes.labels)
assert torch.all(prototypes.component_labels == serialized_prototypes.
component_labels), "Serialization of Components failed."
save = io.BytesIO() save = io.BytesIO()
torch.save(components, save) torch.save(components, save)
save.seek(0) save.seek(0)
serialized_components = torch.load(save) serialized_components = torch.load(save)
assert torch.all(components.components == serialized_components.components assert torch.all(components.components == serialized_components.components)
), "Serialization of Components failed." assert torch.all(components.reasonings == serialized_components.reasonings)
assert torch.all(components.reasonings == serialized_components.reasonings
), "Serialization of Components failed."