2021-04-29 16:06:26 +00:00
|
|
|
"""This example script shows the usage of the new components architecture.
|
|
|
|
|
|
|
|
Serialization/deserialization also works as expected.
|
2021-06-16 11:39:28 +00:00
|
|
|
|
2021-04-29 16:06:26 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-26 18:49:50 +00:00
|
|
|
import torch
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
import prototorch as pt
|
2021-04-26 18:49:50 +00:00
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
ds = pt.datasets.Iris()
|
2021-04-26 18:49:50 +00:00
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
unsupervised = pt.components.Components(
|
|
|
|
6,
|
|
|
|
initializer=pt.initializers.ZCI(2),
|
|
|
|
)
|
2021-04-26 18:49:50 +00:00
|
|
|
print(unsupervised())
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
prototypes = pt.components.LabeledComponents(
|
|
|
|
(3, 2),
|
|
|
|
components_initializer=pt.initializers.SSCI(ds),
|
|
|
|
)
|
2021-04-26 18:49:50 +00:00
|
|
|
print(prototypes())
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
components = pt.components.ReasoningComponents(
|
|
|
|
(3, 2),
|
|
|
|
components_initializer=pt.initializers.SSCI(ds),
|
|
|
|
reasonings_initializer=pt.initializers.PPRI(),
|
|
|
|
)
|
|
|
|
print(prototypes())
|
2021-04-26 18:49:50 +00:00
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
# Test Serialization
|
2021-04-26 18:49:50 +00:00
|
|
|
import io
|
|
|
|
|
|
|
|
save = io.BytesIO()
|
|
|
|
torch.save(unsupervised, save)
|
|
|
|
save.seek(0)
|
|
|
|
serialized_unsupervised = torch.load(save)
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
assert torch.all(unsupervised.components == serialized_unsupervised.components)
|
2021-04-26 18:49:50 +00:00
|
|
|
|
|
|
|
save = io.BytesIO()
|
|
|
|
torch.save(prototypes, save)
|
|
|
|
save.seek(0)
|
|
|
|
serialized_prototypes = torch.load(save)
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
assert torch.all(prototypes.components == serialized_prototypes.components)
|
|
|
|
assert torch.all(prototypes.labels == serialized_prototypes.labels)
|
2021-04-26 18:49:50 +00:00
|
|
|
|
|
|
|
save = io.BytesIO()
|
|
|
|
torch.save(components, save)
|
|
|
|
save.seek(0)
|
|
|
|
serialized_components = torch.load(save)
|
|
|
|
|
2021-06-16 11:39:28 +00:00
|
|
|
assert torch.all(components.components == serialized_components.components)
|
|
|
|
assert torch.all(components.reasonings == serialized_components.reasonings)
|