A package to simplify the implementing a variational-autoencoder model with spacial latent heads.
Project description
🧠 Variational Autoencoder (VAE) in PyTorch
A modular and customizable implementation of a Convolutional Variational Autoencoder (VAE) in PyTorch, designed for image reconstruction and unsupervised representation learning. Built with residual blocks, RMS normalization, and flexible architecture scaling.
🚀 Features
- 🔁 Encoder–Decoder VAE with reparameterization trick
- 🧱 Residual blocks with RMS normalization
- 🧩 Fully modular, easy to customize
- 🔄 Downsampling/Upsampling using
einopsandnn.Conv2d - 🧪 Dropout regularization for improved generalization
- ⚡ Fast inference with
.reconstruct()method - 🧼 Clean, production-ready code
📦 Installation
pip install variational-autoencoder-pytorch
📁 Project Structure
variational-autoencoder-pytorch/
├── variational_autoencoder_pytorch/
│ ├── __init__.py
│ └── module.py # All architecture classes and logic
├── pyproject.toml
├── LICENSE
└── README.md
🚀 Quick Start
1. Import the package and create the model
import torch
from variational_autoencoder_pytorch import VariationalAutoEncoder
model = AutoEncoder(
dim=64,
dim_mults=(1, 2, 4, 8),
dim_latent=128,
image_channels=3
)
2. Forward pass and reconstruction
x = torch.randn(8, 3, 256, 256) # batch of images
x_recon, mu, logvar = model(x)
# Or just get the reconstruction
x_recon = model.reconstruct(x)
3. Training step (sample loop)
import torch.nn.functional as F
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
def train_step(x):
model.train()
optimizer.zero_grad()
x_recon, mu, logvar = model(x)
loss = vae_loss(x, x_recon, mu, logvar)
loss.backward()
optimizer.step()
return loss.item()
🧠 Model Output
-
x_recon: Reconstructed image -
mu: Mean of the latent distribution -
logvar: Log-variance of the latent distribution
⚙️ Configuration Options
| Argument | Type | Default | Description |
|---|---|---|---|
dim |
int |
64 |
Base number of channels |
dim_mults |
tuple |
(1, 2, 4, 8) |
Multipliers for feature map dimensions |
dim_latent |
int |
64 |
Latent space dimension |
image_channels |
int |
3 |
Input/output image channels (e.g., 3) |
dropout |
float |
0.0 |
Dropout probability |
🧪 Example: Loss Function
Here's a basic VAE loss function combining reconstruction and KL divergence:
def vae_loss(x, x_recon, mu, logvar):
recon_loss = F.mse_loss(x_recon, x, reduction='sum')
kl_div = -0.5 * torch.sum(torch.mean(1 + logvar - mu.pow(2) - logvar.exp(), dim=[2, 3]))
loss = recon_loss + (kl_div * 0.0001) # beta = 0.0001
return loss
🙋♂️ Author
Developed by Mehran Bazrafkan
Built from scratch with inspiration from modern deep generative modeling architectures. This package reflects personal experience with VAEs and convolutional design patterns.
⭐️ Support & Contribute
If you find this project useful, consider:
-
⭐️ Starring the repo
-
🐛 Submitting issues
-
📦 Suggesting improvements
🔗 Related Projects
📜 License
This project is licensed under the terms of the MIT LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file variational_autoencoder_pytorch_lib-0.1.1.tar.gz.
File metadata
- Download URL: variational_autoencoder_pytorch_lib-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb4082b0ac446ef24c97caffe5a1ed7c3f80abab52549a15154a1d2126038b10
|
|
| MD5 |
bbe13f7dbfe3c1e8a56e6c2dbb0a034b
|
|
| BLAKE2b-256 |
766214825d876cd95b79ece5b39d2d4bccd4a94dcadfd7d21692e7a457e6b572
|
File details
Details for the file variational_autoencoder_pytorch_lib-0.1.1-py3-none-any.whl.
File metadata
- Download URL: variational_autoencoder_pytorch_lib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
290e59f2d0c615b9b9946c29c4f635a19a852a678e24d2dd90a69efa1941a553
|
|
| MD5 |
9b869ad69b3baa4f7bd083f902c0ff93
|
|
| BLAKE2b-256 |
77794fb30aedbff61f485164b97403b5a31e9fc5563bf31faa4b6547e191b2d3
|