pyautoencoder 1.0.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.
@@ -0,0 +1,4 @@
1
+ from . import loss
2
+ from . import models
3
+
4
+ __all__ = ['loss', 'models']
pyautoencoder/loss.py ADDED
@@ -0,0 +1,92 @@
1
+ import math
2
+ import torch
3
+ import torch.nn.functional as F
4
+
5
+ def log_likelihood(x: torch.Tensor,
6
+ x_hat: torch.Tensor,
7
+ likelihood: str = 'gaussian') -> torch.Tensor:
8
+ """
9
+ Computes the log-likelihood of the reconstructed tensor x_hat given the original tensor x,
10
+ under either a Bernoulli or Gaussian likelihood assumption with unit variance and i.i.d. samples,
11
+ without applying any reduction.
12
+
13
+ Args:
14
+ x (torch.Tensor): Ground truth input tensor.
15
+ x_hat (torch.Tensor): Reconstructed tensor.
16
+ likelihood (str): Type of likelihood model to use: 'bernoulli' or 'gaussian'.
17
+
18
+ Returns:
19
+ torch.Tensor: The log-likelihood value (i.e., negative of the appropriate loss function
20
+ with normalization constant included for Gaussian).
21
+
22
+ Raises:
23
+ ValueError: If the likelihood type is not one of 'bernoulli' or 'gaussian'.
24
+
25
+ Notes:
26
+ - Bernoulli likelihood uses binary cross-entropy loss.
27
+ - Gaussian likelihood assumes unit variance and computes:
28
+ log p(x | x_hat) = -0.5 * ||x - x_hat||^2 - (D/2) * log(2pi)
29
+ where D is the number of features per sample.
30
+ """
31
+ likelihood = likelihood.lower()
32
+ if likelihood not in ['bernoulli', 'gaussian']:
33
+ raise ValueError(f"Unknown likelihood: '{likelihood}'. Choose 'bernoulli' or 'gaussian'.")
34
+
35
+ if likelihood == 'bernoulli':
36
+ return -F.binary_cross_entropy(x_hat, x, reduction='none')
37
+
38
+ if likelihood == 'gaussian':
39
+ D = x[0].numel()
40
+ mse = F.mse_loss(x_hat, x, reduction='none')
41
+ norm_constant = 0.5 * D * math.log(2 * math.pi)
42
+ return -0.5 * mse - norm_constant
43
+
44
+ def ELBO(x: torch.Tensor,
45
+ x_hat: torch.Tensor,
46
+ mu: torch.Tensor,
47
+ log_var: torch.Tensor,
48
+ likelihood: str = 'gaussian',
49
+ beta: float = 1.0) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
50
+ """
51
+ Computes the Evidence Lower Bound (ELBO) for a Variational Autoencoder.
52
+
53
+ Args:
54
+ x (torch.Tensor): Original input tensor of shape [B, ...].
55
+ x_hat (torch.Tensor): Reconstructed samples of shape [B, L, ...], where L is the number of latent samples.
56
+ mu (torch.Tensor): Mean of the approximate posterior q(z|x), shape [B, latent_dim].
57
+ log_var (torch.Tensor): Log-variance of q(z|x), shape [B, latent_dim].
58
+ likelihood (str): Likelihood model to use: 'gaussian' or 'bernoulli'.
59
+ beta (float): Weighting factor for the KL divergence term (used in beta-VAE).
60
+
61
+ Returns:
62
+ tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
63
+ - ELBO (scalar): The final ELBO estimate averaged over the batch.
64
+ - log_p_x_given_z (scalar): Expected log-likelihood term.
65
+ - kl_divergence (scalar): KL divergence term.
66
+
67
+ Notes:
68
+ The reconstruction term is averaged over the latent samples L and the batch.
69
+ The KL divergence is computed assuming a standard normal prior p(z).
70
+ """
71
+ B, L = x_hat.size(0), x_hat.size(1)
72
+
73
+ # Log-likelihood E_q[log p(x|z)]
74
+ x_exp = x.unsqueeze(1).expand(-1, L, *([-1] * (x.ndim - 1)))
75
+ log_p_x_given_z = log_likelihood(x_exp, x_hat, likelihood=likelihood)
76
+ log_p_x_given_z = log_p_x_given_z.view(B, L, -1).sum(-1)
77
+ log_p_x_given_z = log_p_x_given_z.mean(dim=1)
78
+
79
+ # KL divergence KL(q(z|x) || p(z)) = log q(z|x) - log p(z)
80
+ kl_divergence = -0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp(), dim=-1)
81
+
82
+ # ELBO
83
+ elbo_per_sample = log_p_x_given_z - beta * kl_divergence
84
+
85
+ # Final metrics
86
+ elbo = elbo_per_sample.mean()
87
+ log_p_x_given_z = log_p_x_given_z.mean()
88
+ kl_divergence = kl_divergence.mean()
89
+
90
+ return elbo, log_p_x_given_z, kl_divergence
91
+
92
+ __all__ = ['log_likelihood', 'ELBO']
@@ -0,0 +1,4 @@
1
+ from .autoencoder import Autoencoder
2
+ from .variational import VariationalAutoencoder
3
+
4
+ __all__ = ['VariationalAutoencoder', 'Autoencoder']
@@ -0,0 +1,39 @@
1
+ from typing import Tuple
2
+ import torch
3
+ import torch.nn as nn
4
+
5
+ class Autoencoder(nn.Module):
6
+ """
7
+ A simple Autoencoder model.
8
+
9
+ This class encapsulates an autoencoder composed of a user-defined encoder and decoder.
10
+ The encoder maps the input to a latent representation, and the decoder reconstructs the input from the latent space.
11
+
12
+ Args:
13
+ encoder (nn.Module): A neural network that encodes the input into a latent representation.
14
+ decoder (nn.Module): A neural network that decodes the latent representation back to the input space.
15
+
16
+ Methods:
17
+ forward(x): Computes the reconstructed input and latent representation.
18
+ encode(x): Returns the latent representation without computing gradients (inference mode) and in eval mode.
19
+
20
+ Returns:
21
+ Tuple[torch.Tensor, torch.Tensor]:
22
+ - x_hat: The reconstructed input.
23
+ - z: The latent representation.
24
+ """
25
+ def __init__(self, encoder: nn.Module, decoder: nn.Module):
26
+ super().__init__()
27
+
28
+ self.encoder = encoder
29
+ self.decoder = decoder
30
+
31
+ def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
32
+ z = self.encoder(x)
33
+ x_hat = self.decoder(z)
34
+ return x_hat, z
35
+
36
+ @torch.inference_mode()
37
+ def encode(self, x: torch.Tensor) -> torch.Tensor:
38
+ self.eval()
39
+ return self.encoder(x)
@@ -0,0 +1,3 @@
1
+ from .vae import VariationalAutoencoder
2
+
3
+ __all__ = ['VariationalAutoencoder']
@@ -0,0 +1,33 @@
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class FullyFactorizedGaussian(nn.Module):
5
+ def __init__(self, latent_dim: int):
6
+ super().__init__()
7
+ self.mu = nn.LazyLinear(latent_dim)
8
+ self.log_var = nn.LazyLinear(latent_dim)
9
+
10
+ def forward(self, x: torch.Tensor, L: int = 1):
11
+ """
12
+ Applies the reparameterization trick to sample latent variables z ~ N(mu, exp(log_var)).
13
+
14
+ Args:
15
+ x (torch.Tensor): Input tensor of shape [B, ...], where B is the batch size.
16
+ L (int): Number of samples per input in the latent space.
17
+
18
+ Returns:
19
+ torch.Tensor: Sampled latent variables z of shape [B, L, latent_dim].
20
+ """
21
+ mu = self.mu(x)
22
+ log_var = self.log_var(x)
23
+
24
+ if self.training:
25
+ std = torch.exp(0.5 * log_var)
26
+ mu = mu.unsqueeze(1).expand(-1, L, -1)
27
+ std = std.unsqueeze(1).expand(-1, L, -1)
28
+ eps = torch.randn_like(std)
29
+ z = mu + std * eps
30
+ else:
31
+ z = mu.unsqueeze(1).expand(-1, L, -1)
32
+
33
+ return z, mu, log_var
@@ -0,0 +1,65 @@
1
+ from .stochastic_layers import FullyFactorizedGaussian
2
+ from typing import Tuple
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ class VariationalAutoencoder(nn.Module):
7
+ """
8
+ Standard Variational Autoencoder (VAE) implementation using the reparameterization trick.
9
+
10
+ This model assumes a single latent layer and consists of:
11
+ - an encoder producing parameters of the approximate posterior q(z|x),
12
+ - a decoder reconstructing the input from latent samples z.
13
+
14
+ Args:
15
+ encoder (nn.Module): Encoder network mapping input x to a latent representation.
16
+ decoder (nn.Module): Decoder network reconstructing x from latent variable z.
17
+ latent_dim (int): Dimensionality of the latent space.
18
+ """
19
+ def __init__(self,
20
+ encoder: nn.Module,
21
+ decoder: nn.Module,
22
+ latent_dim: int,
23
+ sampling_layer: str = 'fully_factorized_gaussian'):
24
+ super().__init__()
25
+
26
+ self.latent_dim = latent_dim
27
+ self.encoder = encoder
28
+ self.decoder = decoder
29
+ if sampling_layer == 'fully_factorized_gaussian':
30
+ self.sampling_layer = FullyFactorizedGaussian(latent_dim=latent_dim)
31
+ else:
32
+ raise ValueError(f'Sampling layer {sampling_layer} not available.')
33
+
34
+ def forward(self,
35
+ x: torch.Tensor,
36
+ L: int = 1) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
37
+ """
38
+ Forward pass of the VAE.
39
+
40
+ Encodes the input to obtain parameters of q(z|x), samples latent variables z using
41
+ the reparameterization trick, and decodes z to reconstruct the input.
42
+
43
+ Args:
44
+ x (torch.Tensor): Input tensor of shape [B, ...], where B is the batch size.
45
+ L (int): Number of samples per input for Monte Carlo estimates (default: 1).
46
+
47
+ Returns:
48
+ Tuple containing:
49
+ - x_hat (torch.Tensor): Reconstructed inputs, shape [B, L, ...].
50
+ - z (torch.Tensor): Sampled latent variables, shape [B, L, latent_dim].
51
+ - mu (torch.Tensor): Mean of q(z|x), shape [B, latent_dim].
52
+ - log_var (torch.Tensor): Log-variance of q(z|x), shape [B, latent_dim].
53
+ """
54
+ B = x.size(0)
55
+
56
+ # z ~ q(z|x)
57
+ x_f = self.encoder(x)
58
+ z, mu, log_var = self.sampling_layer(x=x_f, L=L)
59
+
60
+ # p(x|z)
61
+ z_flat = z.reshape(B * L, -1)
62
+ x_hat = self.decoder(z_flat)
63
+ x_hat = x_hat.view(B, L, *x.shape[1:])
64
+
65
+ return x_hat, z, mu, log_var
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyautoencoder
3
+ Version: 1.0.0
4
+ Summary: A Python package offering implementations of state-of-the-art autoencoder architectures in PyTorch.
5
+ Home-page: https://github.com/andrea-pollastro/pyautoencoder
6
+ Author: Andrea Pollastro
7
+ License: MIT
8
+ Keywords: autoencoder,vae,pytorch,deep learning,machine learning,representation learning,dimensionality reduction,generative models
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.7
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: torch>=2.0.0
25
+ Dynamic: author
26
+ Dynamic: classifier
27
+ Dynamic: description
28
+ Dynamic: description-content-type
29
+ Dynamic: home-page
30
+ Dynamic: keywords
31
+ Dynamic: license
32
+ Dynamic: license-file
33
+ Dynamic: requires-dist
34
+ Dynamic: requires-python
35
+ Dynamic: summary
36
+
37
+ <p align="center">
38
+ <img src="assets/logo_nobackground.png" alt="pyautoencoders_logo" width="500"/>
39
+ </p>
40
+
41
+ **pyautoencoder** is a lightweight Python package offering clean, minimal implementations of foundational autoencoder architectures in PyTorch.
42
+ It is designed for researchers, educators, and practitioners seeking a reliable base for experimentation, extension, or instruction.
43
+
44
+ ## πŸ“¦ Installation
45
+
46
+ ```bash
47
+ pip install pyautoencoder
48
+ ```
49
+
50
+ Or install from source:
51
+ ```bash
52
+ git clone https://github.com/andrea-pollastro/pyautoencoder.git
53
+ cd pyautoencoder
54
+ pip install -e .
55
+ ```
56
+
57
+ ## πŸš€ Quick Example
58
+
59
+ ```python
60
+ import torch
61
+ from pyautoencoder.models import Autoencoder
62
+
63
+ # Define encoder and decoder
64
+ encoder = torch.nn.Sequential(
65
+ torch.nn.Flatten(),
66
+ torch.nn.Linear(784, 32)
67
+ )
68
+
69
+ decoder = torch.nn.Sequential(
70
+ torch.nn.Linear(32, 784),
71
+ torch.nn.Unflatten(1, (1, 28, 28))
72
+ )
73
+
74
+ # Initialize model
75
+ model = Autoencoder(encoder, decoder)
76
+
77
+ # Forward pass
78
+ x = torch.randn(64, 1, 28, 28)
79
+ x_hat, z = model(x)
80
+ ```
81
+
82
+ ## πŸ—ΊοΈ Roadmap
83
+ - [x] Autoencoder (AE)
84
+ - [x] Variational Autoencoder (VAE)
85
+ - [ ] Hierarchical VAE (HVAE)
86
+ - [ ] Importance-Weighted AE (IWAE)
87
+ - [ ] Denoising Autoencoder (DAE)
88
+ - [ ] Sparse Autoencoder (SAE)
89
+
90
+ ## 🀝 Contributing
91
+ Contributions are welcome β€” especially new autoencoder variants, training examples, and documentation improvements.
92
+ Please open an issue or pull request to discuss any changes.
93
+
94
+ ## πŸ“ Citing
95
+ ```bibtex
96
+ @misc{pollastro2025pyautoencoder,
97
+ Author = {Andrea Pollastro},
98
+ Title = {pyautoencoder},
99
+ Year = {2025},
100
+ Publisher = {GitHub},
101
+ Journal = {GitHub repository},
102
+ Howpublished = {\url{https://github.com/andrea-pollastro/pyautoencoder}}
103
+ }
104
+ ```
105
+
106
+ ## πŸ“„ License
107
+ This project is licensed under the MIT License. See the LICENSE file for details.
@@ -0,0 +1,12 @@
1
+ pyautoencoder/__init__.py,sha256=GaNI1Tfw-HsmpUDJhpfQObQQAOqrvswIZQLbT6-Y5FM,72
2
+ pyautoencoder/loss.py,sha256=vu7tF9XpoVLc4IL0LInGLI3T1GpNaiGHTMgPlfIlars,3892
3
+ pyautoencoder/models/__init__.py,sha256=LkoZn8E3ZKd4rR2o8WaaFwwPywwHvo9Id9iPnok7-lA,140
4
+ pyautoencoder/models/autoencoder.py,sha256=Yo0gc6LG6QcJhSgvT4sL9KCa_MEyfxiA4JB6KKSSMSs,1431
5
+ pyautoencoder/models/variational/__init__.py,sha256=2Fo7yTZf8ps4qBKI60qkUH_1k2g5V5057jhUqv6z4OI,79
6
+ pyautoencoder/models/variational/stochastic_layers.py,sha256=QM8jklRE25z-NMGjDXlfmNwl2rCrxHKeL3cg79swV-U,1128
7
+ pyautoencoder/models/variational/vae.py,sha256=Mp9Bh07jFmVivpiFrAoWc2OdVQbXFxa0ncRMPKiYT8I,2616
8
+ pyautoencoder-1.0.0.dist-info/licenses/LICENSE,sha256=gZFB44cBo3AAs5_m9gUl8QEv4uwK69BzO97FMh3plM4,1094
9
+ pyautoencoder-1.0.0.dist-info/METADATA,sha256=UBafRlML_tG1ffP7lGqtzY24GEYnySdnZ4jOFgJUWeA,3290
10
+ pyautoencoder-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ pyautoencoder-1.0.0.dist-info/top_level.txt,sha256=PwjW6SQ7aRZohAwWaqtZDP8qpn4dWIUHrVVOqWfwMdo,14
12
+ pyautoencoder-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrea Pollastro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ pyautoencoder