autoencodix 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of autoencodix might be problematic. Click here for more details.
- autoencodix/__init__.py +27 -0
- autoencodix/base/.ipynb_checkpoints/_base_pipeline-checkpoint.py +1274 -0
- autoencodix/base/.ipynb_checkpoints/_base_trainer-checkpoint.py +247 -0
- autoencodix/base/__init__.py +21 -0
- autoencodix/base/_base_autoencoder.py +138 -0
- autoencodix/base/_base_dataset.py +103 -0
- autoencodix/base/_base_evaluator.py +34 -0
- autoencodix/base/_base_loss.py +311 -0
- autoencodix/base/_base_pipeline.py +1355 -0
- autoencodix/base/_base_predictor.py +6 -0
- autoencodix/base/_base_preprocessor.py +1033 -0
- autoencodix/base/_base_trainer.py +259 -0
- autoencodix/base/_base_visualizer.py +578 -0
- autoencodix/base/sampleconfig.yaml +132 -0
- autoencodix/configs/__init__.py +21 -0
- autoencodix/configs/default_config.py +652 -0
- autoencodix/configs/disentanglix_config.py +15 -0
- autoencodix/configs/maskix_config.py +39 -0
- autoencodix/configs/ontix_config.py +35 -0
- autoencodix/configs/stackix_config.py +33 -0
- autoencodix/configs/vanillix_config.py +18 -0
- autoencodix/configs/varix_config.py +15 -0
- autoencodix/configs/xmodalix_config.py +43 -0
- autoencodix/disentanglix.py +72 -0
- autoencodix/evaluate/.ipynb_checkpoints/evaluate-checkpoint.py +532 -0
- autoencodix/evaluate/__init__.py +4 -0
- autoencodix/evaluate/_general_evaluator.py +730 -0
- autoencodix/evaluate/_xmodalix_evaluator.py +376 -0
- autoencodix/imagix.py +76 -0
- autoencodix/losses/disentanglix_loss.py +215 -0
- autoencodix/losses/maskix_loss.py +94 -0
- autoencodix/losses/vanillix_loss.py +28 -0
- autoencodix/losses/varix_loss.py +102 -0
- autoencodix/losses/xmodal_loss.py +304 -0
- autoencodix/maskix.py +108 -0
- autoencodix/modeling/.ipynb_checkpoints/__init__-checkpoint.py +16 -0
- autoencodix/modeling/.ipynb_checkpoints/_captum_forward-checkpoint.py +16 -0
- autoencodix/modeling/__init__.py +20 -0
- autoencodix/modeling/_captum_forward.py +20 -0
- autoencodix/modeling/_classifier.py +45 -0
- autoencodix/modeling/_imagevae_architecture.py +296 -0
- autoencodix/modeling/_imgfast_architecture.py +288 -0
- autoencodix/modeling/_layer_factory.py +71 -0
- autoencodix/modeling/_maskix_architecture.py +190 -0
- autoencodix/modeling/_ontix_architecture.py +305 -0
- autoencodix/modeling/_vanillix_architecture.py +136 -0
- autoencodix/modeling/_varix_architecture.py +192 -0
- autoencodix/notebooks/__init__.py +0 -0
- autoencodix/ontix.py +136 -0
- autoencodix/py.typed +0 -0
- autoencodix/stackix.py +122 -0
- autoencodix/trainers/__init__.py +6 -0
- autoencodix/trainers/_general_trainer.py +525 -0
- autoencodix/trainers/_maskix_trainer.py +116 -0
- autoencodix/trainers/_ontix_trainer.py +77 -0
- autoencodix/trainers/_stackix_orchestrator.py +522 -0
- autoencodix/trainers/_stackix_trainer.py +209 -0
- autoencodix/trainers/_xmodal_trainer.py +1006 -0
- autoencodix/utils/.ipynb_checkpoints/_explainer-checkpoint.py +118 -0
- autoencodix/utils/.ipynb_checkpoints/_model_output-checkpoint.py +30 -0
- autoencodix/utils/.ipynb_checkpoints/_utils-checkpoint.py +626 -0
- autoencodix/utils/.ipynb_checkpoints/adata_converter-checkpoint.py +99 -0
- autoencodix/utils/.ipynb_checkpoints/feature_importance-checkpoint.py +823 -0
- autoencodix/utils/__init__.py +22 -0
- autoencodix/utils/_annealer.py +193 -0
- autoencodix/utils/_bulkreader.py +232 -0
- autoencodix/utils/_explainer.py +246 -0
- autoencodix/utils/_imgreader.py +404 -0
- autoencodix/utils/_internals.py +23 -0
- autoencodix/utils/_llm_explainer.py +318 -0
- autoencodix/utils/_losses.py +20 -0
- autoencodix/utils/_model_output.py +30 -0
- autoencodix/utils/_result.py +393 -0
- autoencodix/utils/_screader.py +76 -0
- autoencodix/utils/_traindynamics.py +187 -0
- autoencodix/utils/_utils.py +690 -0
- autoencodix/utils/adata_converter.py +111 -0
- autoencodix/utils/example_data.py +398 -0
- autoencodix/utils/feature_importance.py +829 -0
- autoencodix/utils/prompts.py +21 -0
- autoencodix/vanillix.py +141 -0
- autoencodix/varix.py +80 -0
- autoencodix/visualize/__init__.py +5 -0
- autoencodix/visualize/_general_visualizer.py +913 -0
- autoencodix/visualize/_imagix_visualizer.py +111 -0
- autoencodix/visualize/_xmodal_visualizer.py +841 -0
- autoencodix/visualize/visualize.py +1214 -0
- autoencodix/xmodalix.py +318 -0
- autoencodix-0.2.0.dist-info/METADATA +143 -0
- autoencodix-0.2.0.dist-info/RECORD +91 -0
- autoencodix-0.2.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import torch.nn as nn
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LayerFactory:
|
|
6
|
+
"""Factory for creating configurable neural network layers."""
|
|
7
|
+
|
|
8
|
+
@staticmethod
|
|
9
|
+
def get_layer_dimensions(
|
|
10
|
+
feature_dim: int, latent_dim: int, n_layers: int, enc_factor: float
|
|
11
|
+
) -> List[int]:
|
|
12
|
+
"""Calculate progressive layer dimensions.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
feature_dim: Input feature dimension
|
|
16
|
+
latent_dim: Target latent dimension
|
|
17
|
+
n_layers: Number of layers
|
|
18
|
+
enc_factor: Reduction factor for layer sizes
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
Calculated layer dimensions
|
|
22
|
+
"""
|
|
23
|
+
if n_layers == 0:
|
|
24
|
+
return [feature_dim, latent_dim] # Direct projection from input to latent
|
|
25
|
+
|
|
26
|
+
layer_dimensions = [feature_dim]
|
|
27
|
+
for _ in range(n_layers):
|
|
28
|
+
prev_layer_size = layer_dimensions[-1]
|
|
29
|
+
next_layer_size = max(int(prev_layer_size / enc_factor), latent_dim)
|
|
30
|
+
layer_dimensions.append(next_layer_size)
|
|
31
|
+
layer_dimensions.append(latent_dim)
|
|
32
|
+
|
|
33
|
+
return layer_dimensions
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def create_layer(
|
|
37
|
+
in_features: int,
|
|
38
|
+
out_features: int,
|
|
39
|
+
dropout_p: float = 0.1,
|
|
40
|
+
last_layer: bool = False,
|
|
41
|
+
) -> List[nn.Module]:
|
|
42
|
+
"""Create a configurable layer with optional components.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
in_features: Input feature dimension
|
|
46
|
+
out_features: Output feature dimension
|
|
47
|
+
dropout_p: Dropout probability, by default 0.1
|
|
48
|
+
last_layer: Flag to skip activation/dropout for final layer, by default False
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
List of layer components
|
|
52
|
+
"""
|
|
53
|
+
if last_layer:
|
|
54
|
+
return [nn.Linear(in_features, out_features)]
|
|
55
|
+
|
|
56
|
+
return [
|
|
57
|
+
nn.Linear(in_features, out_features),
|
|
58
|
+
nn.BatchNorm1d(out_features),
|
|
59
|
+
nn.Dropout(dropout_p),
|
|
60
|
+
nn.ReLU(),
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def create_maskix_layer(in_features: int, out_features: int, last_layer: bool):
|
|
65
|
+
if last_layer:
|
|
66
|
+
return [nn.Linear(in_features, out_features)]
|
|
67
|
+
return [
|
|
68
|
+
nn.Linear(in_features, out_features),
|
|
69
|
+
nn.LayerNorm(out_features),
|
|
70
|
+
nn.Mish(inplace=True),
|
|
71
|
+
]
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from autoencodix.configs import DefaultConfig
|
|
4
|
+
from autoencodix.modeling._layer_factory import LayerFactory
|
|
5
|
+
from typing import Optional, Union, Tuple, Dict, List
|
|
6
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
7
|
+
from autoencodix.utils._model_output import ModelOutput
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MaskixArchitectureVanilla(BaseAutoencoder):
|
|
11
|
+
"""Masked Autoencoder Architecture that follows https://doi.org/10.1093/bioinformatics/btae020
|
|
12
|
+
|
|
13
|
+
To closely mimic the publication, the network is not build with our LayerFactory as in
|
|
14
|
+
other architectures.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
input_dim: number of input features
|
|
18
|
+
config: Configuration object containing model architecture parameters
|
|
19
|
+
encoder: Encoder network of the autoencoder
|
|
20
|
+
decoder: Decoder network of the autoencoder
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
config: Optional[DefaultConfig],
|
|
28
|
+
input_dim: Union[int, Tuple[int, ...]],
|
|
29
|
+
ontologies: Optional[Union[Tuple, Dict]] = None,
|
|
30
|
+
feature_order: Optional[Union[Tuple, Dict]] = None,
|
|
31
|
+
):
|
|
32
|
+
if config is None:
|
|
33
|
+
config = DefaultConfig()
|
|
34
|
+
self._config: DefaultConfig = config
|
|
35
|
+
super().__init__(config, input_dim)
|
|
36
|
+
self.input_dim: Union[int, Tuple[int, ...]] = input_dim
|
|
37
|
+
if not isinstance(self.input_dim, int):
|
|
38
|
+
raise TypeError(
|
|
39
|
+
f"input dim needs to be int for MaskixArchitecture, got {type(self.input_dim)}"
|
|
40
|
+
)
|
|
41
|
+
self.latent_dim: int = self._config.latent_dim
|
|
42
|
+
|
|
43
|
+
# populate self.encoder and self.decoder
|
|
44
|
+
self._encoder: nn.Module
|
|
45
|
+
self._decoder: nn.Module
|
|
46
|
+
self._build_network()
|
|
47
|
+
self.apply(self._init_weights)
|
|
48
|
+
|
|
49
|
+
def _build_network(self):
|
|
50
|
+
if self._config.maskix_architecture == "scMAE":
|
|
51
|
+
self._build_scMAE()
|
|
52
|
+
elif self._config.maskix_architecture == "custom":
|
|
53
|
+
self._build_custom()
|
|
54
|
+
else:
|
|
55
|
+
raise ValueError(
|
|
56
|
+
f"Got {self.config.maskix_architecture}, but expected 'scMAE' or 'custom'"
|
|
57
|
+
"This happens if you allow a new value in DefaultConfig but did not implement it here."
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def _build_custom(self):
|
|
61
|
+
self._mask_predictor = nn.Linear(self.latent_dim, self.input_dim) # ty: ignore
|
|
62
|
+
|
|
63
|
+
enc_dim = LayerFactory.get_layer_dimensions(
|
|
64
|
+
feature_dim=self.input_dim, # ty: ignore
|
|
65
|
+
latent_dim=self._config.latent_dim,
|
|
66
|
+
n_layers=self._config.n_layers,
|
|
67
|
+
enc_factor=self._config.enc_factor,
|
|
68
|
+
)
|
|
69
|
+
first_layer = nn.Dropout(p=self.config.drop_p)
|
|
70
|
+
|
|
71
|
+
encoder_layers: List[nn.Module] = []
|
|
72
|
+
if self._config.n_layers == 0:
|
|
73
|
+
self._encoder = nn.Sequential(
|
|
74
|
+
nn.Dropout(p=self.config.drop_p),
|
|
75
|
+
nn.Linear(self.input_dim, self.latent_dim), # ty: ignore
|
|
76
|
+
)
|
|
77
|
+
# print(enc_dim)
|
|
78
|
+
for i, (in_features, out_features) in enumerate(zip(enc_dim[:-1], enc_dim[1:])):
|
|
79
|
+
last_layer = i == len(enc_dim) - 2
|
|
80
|
+
encoder_layers.extend(
|
|
81
|
+
LayerFactory.create_maskix_layer(
|
|
82
|
+
in_features=in_features,
|
|
83
|
+
out_features=out_features,
|
|
84
|
+
last_layer=last_layer,
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
self._encoder = nn.Sequential(first_layer, *encoder_layers)
|
|
89
|
+
|
|
90
|
+
# dec_dimensions = enc_dim[::-1] # Reverse the dimensions and copy
|
|
91
|
+
# decoder_layers: List[nn.Module] = []
|
|
92
|
+
# for i, (in_features, out_features) in enumerate(
|
|
93
|
+
# zip(dec_dimensions[:-1], dec_dimensions[1:])
|
|
94
|
+
# ):
|
|
95
|
+
# last_layer = i == len(dec_dimensions) - 2
|
|
96
|
+
# decoder_layers.extend(
|
|
97
|
+
# LayerFactory.create_maskix_layer(
|
|
98
|
+
# in_features=in_features,
|
|
99
|
+
# out_features=out_features,
|
|
100
|
+
# last_layer=last_layer,
|
|
101
|
+
# )
|
|
102
|
+
# )
|
|
103
|
+
|
|
104
|
+
# latent_layer = nn.Linear(
|
|
105
|
+
# in_features=self.latent_dim + self.input_dim, # ty: ignore
|
|
106
|
+
# out_features=dec_dimensions[0],
|
|
107
|
+
# ) # ty: ignore
|
|
108
|
+
# self._decoder = nn.Sequential(latent_layer, *decoder_layers)
|
|
109
|
+
|
|
110
|
+
dec_start: int = self.latent_dim + self.input_dim # ty: ignore
|
|
111
|
+
dec_end: int = self.input_dim # ty: ignore
|
|
112
|
+
dec_dim = LayerFactory.get_layer_dimensions(
|
|
113
|
+
feature_dim=dec_start,
|
|
114
|
+
latent_dim=dec_end, # Repurpose 'latent_dim' param as target dim
|
|
115
|
+
n_layers=self._config.n_layers,
|
|
116
|
+
enc_factor=self._config.enc_factor,
|
|
117
|
+
)
|
|
118
|
+
decoder_layers: List[nn.Module] = []
|
|
119
|
+
for i, (in_features, out_features) in enumerate(zip(dec_dim[:-1], dec_dim[1:])):
|
|
120
|
+
last_layer = i == len(dec_dim) - 2
|
|
121
|
+
decoder_layers.extend(
|
|
122
|
+
LayerFactory.create_maskix_layer(
|
|
123
|
+
in_features=in_features,
|
|
124
|
+
out_features=out_features,
|
|
125
|
+
last_layer=last_layer,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
self._decoder = nn.Sequential(*decoder_layers)
|
|
129
|
+
|
|
130
|
+
def _build_scMAE(self):
|
|
131
|
+
self._encoder = nn.Sequential(
|
|
132
|
+
nn.Dropout(p=self.config.drop_p),
|
|
133
|
+
nn.Linear(self.input_dim, self._config.maskix_hidden_dim), # ty: ignore
|
|
134
|
+
nn.LayerNorm(self._config.maskix_hidden_dim),
|
|
135
|
+
nn.Mish(inplace=True),
|
|
136
|
+
nn.Linear(self._config.maskix_hidden_dim, self.latent_dim),
|
|
137
|
+
nn.LayerNorm(self.latent_dim),
|
|
138
|
+
nn.Mish(inplace=True),
|
|
139
|
+
nn.Linear(self.latent_dim, self.latent_dim),
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
self._mask_predictor = nn.Linear(self.latent_dim, self.input_dim) # ty: ignore
|
|
143
|
+
self._decoder = nn.Linear(
|
|
144
|
+
in_features=self.latent_dim + self.input_dim, # ty: ignore
|
|
145
|
+
out_features=self.input_dim, # ty: ignore
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def encode(self, x: torch.Tensor) -> torch.Tensor:
|
|
149
|
+
"""Encodes the input data.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
x: input Tensor
|
|
153
|
+
Returns:
|
|
154
|
+
torch.Tensor
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
return self._encoder(x)
|
|
158
|
+
|
|
159
|
+
def get_latent_space(self, x: torch.Tensor) -> torch.Tensor:
|
|
160
|
+
"""Returns the latent space representation of the input data.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
x: input Tensor
|
|
164
|
+
Returns:
|
|
165
|
+
torch.Tensor
|
|
166
|
+
|
|
167
|
+
"""
|
|
168
|
+
return self.encode(x)
|
|
169
|
+
|
|
170
|
+
def decode(self, x: torch.Tensor) -> torch.Tensor:
|
|
171
|
+
"""Decodes the latent representation.
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
x: input Tensor
|
|
175
|
+
Returns:
|
|
176
|
+
torch.Tensor
|
|
177
|
+
|
|
178
|
+
"""
|
|
179
|
+
return self._decoder(x)
|
|
180
|
+
|
|
181
|
+
def forward(self, x: torch.Tensor) -> ModelOutput:
|
|
182
|
+
latent: torch.Tensor = self.encode(x=x)
|
|
183
|
+
predicted_mask: torch.Tensor = self._mask_predictor(latent)
|
|
184
|
+
return ModelOutput(
|
|
185
|
+
reconstruction=self.decode(torch.cat([latent, predicted_mask], dim=1)),
|
|
186
|
+
latentspace=latent,
|
|
187
|
+
latent_mean=None,
|
|
188
|
+
latent_logvar=None,
|
|
189
|
+
additional_info={"predicted_mask": predicted_mask},
|
|
190
|
+
)
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
from typing import Optional, Union, Tuple
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
7
|
+
from autoencodix.utils._model_output import ModelOutput
|
|
8
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
9
|
+
|
|
10
|
+
from ._layer_factory import LayerFactory
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class OntixArchitecture(BaseAutoencoder):
|
|
14
|
+
"""Ontology Autoencoder implementation with separate encoder and decoder construction.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
input_dim: number of input features
|
|
18
|
+
config: Configuration object containing model architecture parameters
|
|
19
|
+
_encoder: Encoder network of the autoencoder
|
|
20
|
+
_decoder: Decoder network of the autoencoder
|
|
21
|
+
mu: Linear layer to compute the mean of the latent distribution
|
|
22
|
+
logvar: Linear layer to compute the log-variance of the latent distribution
|
|
23
|
+
masks: Tuple of weight masks for the decoder layers based on ontology
|
|
24
|
+
latent_dim: Dimension of the latent space, inferred from the first mask
|
|
25
|
+
ontologies: Ontology information.
|
|
26
|
+
feature_order: Order of features for input data.
|
|
27
|
+
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
config: Optional[Union[None, DefaultConfig]],
|
|
33
|
+
input_dim: int,
|
|
34
|
+
ontologies: tuple,
|
|
35
|
+
feature_order: list,
|
|
36
|
+
) -> None:
|
|
37
|
+
"""Initialize the Vanilla Autoencoder with the given configuration.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
config: Configuration object containing model parameters.
|
|
41
|
+
input_dim: Number of input features.
|
|
42
|
+
ontologies: Ontology information.
|
|
43
|
+
feature_order: Order of features for input data.
|
|
44
|
+
"""
|
|
45
|
+
if config is None:
|
|
46
|
+
config = DefaultConfig()
|
|
47
|
+
self._config = config
|
|
48
|
+
super().__init__(config, input_dim)
|
|
49
|
+
self.input_dim = input_dim
|
|
50
|
+
self._mu: nn.Module
|
|
51
|
+
self._logvar: nn.Module
|
|
52
|
+
# create masks for sparse decoder
|
|
53
|
+
self.ontologies = ontologies
|
|
54
|
+
self.feature_order = feature_order
|
|
55
|
+
self.masks = self._make_masks(config=self._config, feature_order=feature_order)
|
|
56
|
+
self.latent_dim = self.masks[0].shape[1]
|
|
57
|
+
print("Latent Dim: " + str(self.latent_dim))
|
|
58
|
+
# populate self.encoder and self.decoder
|
|
59
|
+
self._build_network()
|
|
60
|
+
self.apply(self._init_weights)
|
|
61
|
+
self._decoder.apply(
|
|
62
|
+
self._positive_dec
|
|
63
|
+
) # Sparse decoder only has positive weights
|
|
64
|
+
|
|
65
|
+
# Apply weight mask to create ontology-based decoder
|
|
66
|
+
with torch.no_grad():
|
|
67
|
+
# Check that the decoder has the same number of layers as masks
|
|
68
|
+
if len(self.masks) != len(self._decoder):
|
|
69
|
+
print(len(self.masks), len(self._decoder))
|
|
70
|
+
print(self._encoder)
|
|
71
|
+
print(self._decoder)
|
|
72
|
+
raise ValueError(
|
|
73
|
+
"Number of masks does not match number of decoder layers"
|
|
74
|
+
)
|
|
75
|
+
else:
|
|
76
|
+
for i, mask in enumerate(self.masks):
|
|
77
|
+
self._decoder[i].weight.mul_(mask)
|
|
78
|
+
|
|
79
|
+
def _make_masks(
|
|
80
|
+
self, config: DefaultConfig, feature_order: list
|
|
81
|
+
) -> Tuple[torch.Tensor, ...]:
|
|
82
|
+
"""Create masks for sparse decoder based on ontology via config
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
config: Configuration object containing model parameters
|
|
86
|
+
feature_order: Order of features for input data
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
Tuple containing the masks for the decoder network
|
|
90
|
+
|
|
91
|
+
"""
|
|
92
|
+
# Read ontology from config
|
|
93
|
+
|
|
94
|
+
masks = tuple()
|
|
95
|
+
# feature_names are all values in the last ontology layer
|
|
96
|
+
all_feature_names = set()
|
|
97
|
+
for key, values in self.ontologies[-1].items():
|
|
98
|
+
all_feature_names.update(values)
|
|
99
|
+
all_feature_names = list(all_feature_names)
|
|
100
|
+
print("Ontix checks:")
|
|
101
|
+
print(f"All possible feature names length: {len(all_feature_names)}")
|
|
102
|
+
print(f"Feature order length: {len(feature_order)}")
|
|
103
|
+
# Check if all features in feature_order are present in all_feature_names
|
|
104
|
+
feature_names = [f for f in feature_order]
|
|
105
|
+
missing_features = [f for f in feature_order if f not in all_feature_names]
|
|
106
|
+
if missing_features:
|
|
107
|
+
print(
|
|
108
|
+
f"Features in feature_order not found in all_feature_names: {missing_features}"
|
|
109
|
+
)
|
|
110
|
+
print(f"Feature names without filtering: {len(feature_names)}")
|
|
111
|
+
|
|
112
|
+
# Enumerate through the ontologies
|
|
113
|
+
for x, ont_dic in enumerate(self.ontologies):
|
|
114
|
+
prev_lay_dim = len(ont_dic.keys())
|
|
115
|
+
|
|
116
|
+
if x == len(self.ontologies) - 1:
|
|
117
|
+
# fixed sort of feature list
|
|
118
|
+
node_list = feature_names
|
|
119
|
+
else:
|
|
120
|
+
node_list = list(self.ontologies[x + 1].keys())
|
|
121
|
+
next_lay_dim = len(node_list)
|
|
122
|
+
# create masks for sparse decoder
|
|
123
|
+
mask = torch.zeros(next_lay_dim, prev_lay_dim)
|
|
124
|
+
p_int = 0
|
|
125
|
+
if len(node_list) == next_lay_dim:
|
|
126
|
+
if len(ont_dic.keys()) == prev_lay_dim:
|
|
127
|
+
for p_id in ont_dic:
|
|
128
|
+
feature_list = ont_dic[p_id]
|
|
129
|
+
for f_id in feature_list:
|
|
130
|
+
if f_id in node_list:
|
|
131
|
+
f_int = node_list.index(f_id)
|
|
132
|
+
mask[f_int, p_int] = 1
|
|
133
|
+
|
|
134
|
+
p_int += 1
|
|
135
|
+
else:
|
|
136
|
+
print(
|
|
137
|
+
"Mask layer cannot be calculated. Ontology key list does not match previous layer dimension"
|
|
138
|
+
)
|
|
139
|
+
print("Returning zero mask")
|
|
140
|
+
else:
|
|
141
|
+
print(f"node list: {len(node_list)} vs next_lay_dim:{next_lay_dim}")
|
|
142
|
+
print(
|
|
143
|
+
"Mask layer cannot be calculated. Output layer list does not match next layer dimension"
|
|
144
|
+
)
|
|
145
|
+
print("Returning zero mask")
|
|
146
|
+
print(
|
|
147
|
+
f"Mask layer {x} with shape {mask.shape} and {torch.sum(mask)} connections"
|
|
148
|
+
)
|
|
149
|
+
masks += (mask,)
|
|
150
|
+
|
|
151
|
+
if torch.max(mask) < 1:
|
|
152
|
+
print(
|
|
153
|
+
"You provided an ontology with no connections between layers in the decoder. Please check your ontology definition."
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
return masks
|
|
157
|
+
|
|
158
|
+
def _build_network(self) -> None:
|
|
159
|
+
"""Construct the encoder and decoder networks.
|
|
160
|
+
|
|
161
|
+
Handles cases where `n_layers=0` by skipping the encoder and using only mu/logvar.
|
|
162
|
+
"""
|
|
163
|
+
#### Encoder copied from varix architecture ####
|
|
164
|
+
enc_dim = LayerFactory.get_layer_dimensions(
|
|
165
|
+
feature_dim=self.input_dim,
|
|
166
|
+
latent_dim=self.latent_dim,
|
|
167
|
+
n_layers=self._config.n_layers,
|
|
168
|
+
enc_factor=self._config.enc_factor,
|
|
169
|
+
)
|
|
170
|
+
#
|
|
171
|
+
|
|
172
|
+
# Case 1: No Hidden Layers (Direct Mapping)
|
|
173
|
+
self._encoder = nn.Sequential()
|
|
174
|
+
self._mu = nn.Linear(self.input_dim, self.latent_dim)
|
|
175
|
+
self._logvar = nn.Linear(self.input_dim, self.latent_dim)
|
|
176
|
+
|
|
177
|
+
# Case 2: At Least One Hidden Layer
|
|
178
|
+
if self._config.n_layers > 0:
|
|
179
|
+
encoder_layers = []
|
|
180
|
+
# print(enc_dim)
|
|
181
|
+
for i, (in_features, out_features) in enumerate(
|
|
182
|
+
zip(enc_dim[:-1], enc_dim[1:])
|
|
183
|
+
):
|
|
184
|
+
# since we add mu and logvar, we will remove the last layer
|
|
185
|
+
if i == len(enc_dim) - 2:
|
|
186
|
+
break
|
|
187
|
+
encoder_layers.extend(
|
|
188
|
+
LayerFactory.create_layer(
|
|
189
|
+
in_features=in_features,
|
|
190
|
+
out_features=out_features,
|
|
191
|
+
dropout_p=self._config.drop_p,
|
|
192
|
+
last_layer=False, # only for decoder relevant
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
self._encoder = nn.Sequential(*encoder_layers)
|
|
197
|
+
self._mu = nn.Linear(enc_dim[-2], self.latent_dim)
|
|
198
|
+
self._logvar = nn.Linear(enc_dim[-2], self.latent_dim)
|
|
199
|
+
#### Encoder copied from varix architecture ####
|
|
200
|
+
|
|
201
|
+
# Construct Decoder with Sparse Connections via masks
|
|
202
|
+
# Decoder dimension is determined by the masks
|
|
203
|
+
dec_dim = [self.latent_dim] + [
|
|
204
|
+
mask.shape[0] for mask in self.masks
|
|
205
|
+
] # + [self.input_dim]
|
|
206
|
+
decoder_layers = []
|
|
207
|
+
for i, (in_features, out_features) in enumerate(zip(dec_dim[:-1], dec_dim[1:])):
|
|
208
|
+
# last_layer = i == len(dec_dim) - 2
|
|
209
|
+
last_layer = True ## Only linear layers in sparse decoder
|
|
210
|
+
decoder_layers.extend(
|
|
211
|
+
LayerFactory.create_layer(
|
|
212
|
+
in_features=in_features,
|
|
213
|
+
out_features=out_features,
|
|
214
|
+
dropout_p=0, ## No dropout in sparse decoder
|
|
215
|
+
last_layer=last_layer,
|
|
216
|
+
# only_linear=True,
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
self._decoder = nn.Sequential(*decoder_layers)
|
|
221
|
+
|
|
222
|
+
def _positive_dec(self, m):
|
|
223
|
+
if isinstance(m, nn.Linear):
|
|
224
|
+
m.weight.data = m.weight.data.clamp(min=0)
|
|
225
|
+
|
|
226
|
+
def encode(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
227
|
+
"""Encode the input tensor x
|
|
228
|
+
|
|
229
|
+
Args:
|
|
230
|
+
x: Input tensor
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
Encoded tensor
|
|
234
|
+
"""
|
|
235
|
+
latent = x # for case where n_layers=0
|
|
236
|
+
if len(self._encoder) > 0:
|
|
237
|
+
latent = self._encoder(x)
|
|
238
|
+
mu = self._mu(latent)
|
|
239
|
+
logvar = self._logvar(latent)
|
|
240
|
+
# numeric stability
|
|
241
|
+
logvar = torch.clamp(logvar, 0.01, 20)
|
|
242
|
+
mu = torch.where(mu < 0.0000001, torch.zeros_like(mu), mu)
|
|
243
|
+
return mu, logvar
|
|
244
|
+
|
|
245
|
+
def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
|
|
246
|
+
"""Reparameterization trick for VAE
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
mu: Mean tensor
|
|
250
|
+
logvar: Log variance tensor
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
Reparameterized latent tensor
|
|
254
|
+
"""
|
|
255
|
+
std = torch.exp(0.5 * logvar)
|
|
256
|
+
eps = torch.randn_like(std)
|
|
257
|
+
return mu + eps * std
|
|
258
|
+
|
|
259
|
+
def decode(self, x: torch.Tensor) -> torch.Tensor:
|
|
260
|
+
"""Decode the latent tensor x
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
x: Latent tensor
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
torch.Tensor
|
|
267
|
+
Decoded tensor
|
|
268
|
+
|
|
269
|
+
"""
|
|
270
|
+
return self._decoder(x)
|
|
271
|
+
|
|
272
|
+
def forward(self, x: torch.Tensor) -> ModelOutput:
|
|
273
|
+
"""Forward pass of the model, fill
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
x: Input tensor
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
ModelOutput object containing the reconstructed tensor and latent tensor
|
|
280
|
+
|
|
281
|
+
"""
|
|
282
|
+
mu, logvar = self.encode(x)
|
|
283
|
+
z = self.reparameterize(mu, logvar)
|
|
284
|
+
x_hat = self.decode(z)
|
|
285
|
+
return ModelOutput(
|
|
286
|
+
reconstruction=x_hat,
|
|
287
|
+
latentspace=z,
|
|
288
|
+
latent_mean=mu,
|
|
289
|
+
latent_logvar=logvar,
|
|
290
|
+
additional_info=None,
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
def get_latent_space(self, x: torch.Tensor) -> torch.Tensor:
|
|
294
|
+
"""Returns the latent space representation of the input.
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
x: Input tensor
|
|
298
|
+
|
|
299
|
+
Returns:
|
|
300
|
+
Latent space representation
|
|
301
|
+
|
|
302
|
+
"""
|
|
303
|
+
mu, logvar = self.encode(x)
|
|
304
|
+
z = self.reparameterize(mu, logvar)
|
|
305
|
+
return z
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
from typing import Optional, Union, Tuple, Dict
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
import torch.nn as nn
|
|
5
|
+
|
|
6
|
+
from autoencodix.base._base_autoencoder import BaseAutoencoder
|
|
7
|
+
from autoencodix.utils._model_output import ModelOutput
|
|
8
|
+
from autoencodix.configs.default_config import DefaultConfig
|
|
9
|
+
|
|
10
|
+
from ._layer_factory import LayerFactory
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# internal check done
|
|
14
|
+
# write tests: done
|
|
15
|
+
class VanillixArchitecture(BaseAutoencoder):
|
|
16
|
+
"""Vanilla Autoencoder implementation with separate encoder and decoder construction.
|
|
17
|
+
|
|
18
|
+
Attributes:
|
|
19
|
+
input_dim: number of input features
|
|
20
|
+
config: Configuration object containing model architecture parameters
|
|
21
|
+
encoder: Encoder network of the autoencoder
|
|
22
|
+
decoder: Decoder network of the autoencoder
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
config: Optional[Union[None, DefaultConfig]],
|
|
29
|
+
input_dim: int,
|
|
30
|
+
ontologies: Optional[Union[Tuple, Dict]] = None,
|
|
31
|
+
feature_order: Optional[Union[Tuple, Dict]] = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
"""Initialize the Vanilla Autoencoder with the given configuration.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
config: Configuration object containing model parameters.
|
|
37
|
+
input_dim: Number of input features.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
if config is None:
|
|
41
|
+
config = DefaultConfig()
|
|
42
|
+
self._config = config
|
|
43
|
+
super().__init__(config, input_dim)
|
|
44
|
+
self.input_dim = input_dim
|
|
45
|
+
|
|
46
|
+
# populate self.encoder and self.decoder
|
|
47
|
+
self._build_network()
|
|
48
|
+
self.apply(self._init_weights)
|
|
49
|
+
|
|
50
|
+
def _build_network(self) -> None:
|
|
51
|
+
"""Construct the encoder with linear layers."""
|
|
52
|
+
# Calculate layer dimensions
|
|
53
|
+
enc_dim = LayerFactory.get_layer_dimensions(
|
|
54
|
+
feature_dim=self.input_dim,
|
|
55
|
+
latent_dim=self._config.latent_dim,
|
|
56
|
+
n_layers=self._config.n_layers,
|
|
57
|
+
enc_factor=self._config.enc_factor,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
encoder_layers = []
|
|
61
|
+
for i, (in_features, out_features) in enumerate(zip(enc_dim[:-1], enc_dim[1:])):
|
|
62
|
+
last_layer = i == len(enc_dim) - 2
|
|
63
|
+
encoder_layers.extend(
|
|
64
|
+
LayerFactory.create_layer(
|
|
65
|
+
in_features=in_features,
|
|
66
|
+
out_features=out_features,
|
|
67
|
+
dropout_p=self._config.drop_p,
|
|
68
|
+
last_layer=last_layer,
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
dec_dim = enc_dim[::-1] # Reverse the dimensions and copy
|
|
73
|
+
decoder_layers = []
|
|
74
|
+
for i, (in_features, out_features) in enumerate(zip(dec_dim[:-1], dec_dim[1:])):
|
|
75
|
+
last_layer = i == len(dec_dim) - 2
|
|
76
|
+
decoder_layers.extend(
|
|
77
|
+
LayerFactory.create_layer(
|
|
78
|
+
in_features=in_features,
|
|
79
|
+
out_features=out_features,
|
|
80
|
+
dropout_p=self._config.drop_p,
|
|
81
|
+
last_layer=last_layer,
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
self._encoder = nn.Sequential(*encoder_layers)
|
|
85
|
+
self._decoder = nn.Sequential(*decoder_layers)
|
|
86
|
+
|
|
87
|
+
def encode(self, x: torch.Tensor) -> torch.Tensor:
|
|
88
|
+
"""Encodes the input data.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
x: input Tensor
|
|
92
|
+
Returns:
|
|
93
|
+
torch.Tensor
|
|
94
|
+
|
|
95
|
+
"""
|
|
96
|
+
return self._encoder(x)
|
|
97
|
+
|
|
98
|
+
def get_latent_space(self, x: torch.Tensor) -> torch.Tensor:
|
|
99
|
+
"""Returns the latent space representation of the input data.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
x: input Tensor
|
|
103
|
+
Returns:
|
|
104
|
+
torch.Tensor
|
|
105
|
+
|
|
106
|
+
"""
|
|
107
|
+
return self.encode(x)
|
|
108
|
+
|
|
109
|
+
def decode(self, x: torch.Tensor) -> torch.Tensor:
|
|
110
|
+
"""Decodes the latent representation.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
x: input Tensor
|
|
114
|
+
Returns:
|
|
115
|
+
torch.Tensor
|
|
116
|
+
|
|
117
|
+
"""
|
|
118
|
+
return self._decoder(x)
|
|
119
|
+
|
|
120
|
+
def forward(self, x: torch.Tensor) -> ModelOutput:
|
|
121
|
+
"""Forward pass of the model.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
x: input Tensor
|
|
125
|
+
Returns:
|
|
126
|
+
ModelOutput
|
|
127
|
+
|
|
128
|
+
"""
|
|
129
|
+
latent = self.encode(x)
|
|
130
|
+
return ModelOutput(
|
|
131
|
+
reconstruction=self.decode(latent),
|
|
132
|
+
latentspace=latent,
|
|
133
|
+
latent_mean=None,
|
|
134
|
+
latent_logvar=None,
|
|
135
|
+
additional_info=None,
|
|
136
|
+
)
|