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:
danielstaps 2021-06-18 11:28:25 +00:00 committed by GitHub
parent bf23d5f7f8
commit c73f8e7a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -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,

View File

@ -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