Added PCA initializer and component for OmegaMatrix or LinearMappings (#6)
* Added PCA initializer and component for OmegaMatrix or LinearMappings * [QA] Add default configuration for pre commit hooks * [QA] Add more pre commit checks * [QA] Add more pre commit checks * test(githooks): Add gitlint to check commit messages on commit * docs(githooks): Add usage guide for pre-commit to readme * fix(githooks): mypy only checks source now reverts changes on docs conf.py * docs(githooks): Fix typo Co-authored-by: staps@hs-mittweida.de <staps@hs-mittweida.de> Co-authored-by: Alexander Engelsberger <alexanderengelsberger@gmail.com>
This commit is contained in:
parent
bf23d5f7f8
commit
c73f8e7a28
BIN
prototorch/components/.components.py.swp
Normal file
BIN
prototorch/components/.components.py.swp
Normal file
Binary file not shown.
BIN
prototorch/components/.initializers.py.swp
Normal file
BIN
prototorch/components/.initializers.py.swp
Normal file
Binary file not shown.
@ -44,6 +44,45 @@ def _precheck_initializer(initializer):
|
|||||||
raise TypeError(emsg)
|
raise TypeError(emsg)
|
||||||
|
|
||||||
|
|
||||||
|
class LinearMapping(torch.nn.Module):
|
||||||
|
"""LinearMapping is a learnable Mapping Matrix."""
|
||||||
|
def __init__(self,
|
||||||
|
mapping_shape=None,
|
||||||
|
initializer=None,
|
||||||
|
*,
|
||||||
|
initialized_linearmapping=None):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
# Ignore all initialization settings if initialized_components is given.
|
||||||
|
if initialized_linearmapping is not None:
|
||||||
|
self._register_mapping(initialized_linearmapping)
|
||||||
|
if num_components is not None or initializer is not None:
|
||||||
|
wmsg = "Arguments ignored while initializing Components"
|
||||||
|
warnings.warn(wmsg)
|
||||||
|
else:
|
||||||
|
self._initialize_mapping(mapping_shape, initializer)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mapping_shape(self):
|
||||||
|
return self._omega.shape
|
||||||
|
|
||||||
|
def _register_mapping(self, components):
|
||||||
|
self.register_parameter("_omega", Parameter(components))
|
||||||
|
|
||||||
|
def _initialize_mapping(self, mapping_shape, initializer):
|
||||||
|
_precheck_initializer(initializer)
|
||||||
|
_mapping = initializer.generate(mapping_shape)
|
||||||
|
self._register_mapping(_mapping)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mapping(self):
|
||||||
|
"""Tensor containing the component tensors."""
|
||||||
|
return self._omega.detach()
|
||||||
|
|
||||||
|
def forward(self):
|
||||||
|
return self._omega
|
||||||
|
|
||||||
|
|
||||||
class Components(torch.nn.Module):
|
class Components(torch.nn.Module):
|
||||||
"""Components is a set of learnable Tensors."""
|
"""Components is a set of learnable Tensors."""
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
|
@ -167,6 +167,14 @@ class StratifiedSelectionInitializer(ClassAwareInitializer):
|
|||||||
return samples
|
return samples
|
||||||
|
|
||||||
|
|
||||||
|
# Omega matrix
|
||||||
|
class PcaInitializer(DataAwareInitializer):
|
||||||
|
def generate(self, shape):
|
||||||
|
(input_dim, latent_dim) = shape
|
||||||
|
(_, eigVal, eigVec) = torch.pca_lowrank(self.data, q=latent_dim)
|
||||||
|
return eigVec
|
||||||
|
|
||||||
|
|
||||||
# Labels
|
# Labels
|
||||||
class LabelsInitializer:
|
class LabelsInitializer:
|
||||||
def generate(self):
|
def generate(self):
|
||||||
@ -222,3 +230,4 @@ SMI = StratifiedMeanInitializer
|
|||||||
Random = RandomInitializer = UniformInitializer
|
Random = RandomInitializer = UniformInitializer
|
||||||
Zeros = ZerosInitializer
|
Zeros = ZerosInitializer
|
||||||
Ones = OnesInitializer
|
Ones = OnesInitializer
|
||||||
|
PCA = PcaInitializer
|
||||||
|
Loading…
Reference in New Issue
Block a user