2022-05-19 14:13:08 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from prototorch.core.components import LabeledComponents
|
|
|
|
from prototorch.core.initializers import (
|
|
|
|
AbstractComponentsInitializer,
|
|
|
|
LabelsInitializer,
|
2022-06-12 09:12:55 +00:00
|
|
|
ZerosCompInitializer,
|
2022-05-19 14:13:08 +00:00
|
|
|
)
|
2022-08-15 10:14:14 +00:00
|
|
|
from prototorch.models import BaseYArchitecture
|
2022-05-19 14:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SupervisedArchitecture(BaseYArchitecture):
|
|
|
|
"""
|
|
|
|
Supervised Architecture
|
|
|
|
|
|
|
|
An architecture that uses labeled Components as component Layer.
|
|
|
|
"""
|
|
|
|
components_layer: LabeledComponents
|
|
|
|
|
|
|
|
# HyperParameters
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@dataclass
|
|
|
|
class HyperParameters:
|
|
|
|
"""
|
|
|
|
distribution: A valid prototype distribution. No default possible.
|
|
|
|
components_initializer: An implementation of AbstractComponentsInitializer. No default possible.
|
|
|
|
"""
|
|
|
|
distribution: "dict[str, int]"
|
|
|
|
component_initializer: AbstractComponentsInitializer
|
|
|
|
|
|
|
|
# Steps
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def init_components(self, hparams: HyperParameters):
|
2022-06-12 09:12:55 +00:00
|
|
|
if hparams.component_initializer is not None:
|
|
|
|
self.components_layer = LabeledComponents(
|
|
|
|
distribution=hparams.distribution,
|
|
|
|
components_initializer=hparams.component_initializer,
|
|
|
|
labels_initializer=LabelsInitializer(),
|
|
|
|
)
|
|
|
|
proto_shape = self.components_layer.components.shape[1:]
|
|
|
|
self.hparams["initialized_proto_shape"] = proto_shape
|
|
|
|
else:
|
|
|
|
# when restoring a checkpointed model
|
|
|
|
self.components_layer = LabeledComponents(
|
|
|
|
distribution=hparams.distribution,
|
|
|
|
components_initializer=ZerosCompInitializer(
|
|
|
|
self.hparams["initialized_proto_shape"]),
|
|
|
|
)
|
2022-05-19 14:13:08 +00:00
|
|
|
|
|
|
|
# Properties
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@property
|
|
|
|
def prototypes(self):
|
|
|
|
"""
|
|
|
|
Returns the position of the prototypes.
|
|
|
|
"""
|
|
|
|
return self.components_layer.components.detach().cpu()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def prototype_labels(self):
|
|
|
|
"""
|
|
|
|
Returns the labels of the prototypes.
|
|
|
|
"""
|
|
|
|
return self.components_layer.labels.detach().cpu()
|