Skip to main content

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 einops and nn.Conv2d
  • 🧪 Dropout regularization for improved generalization
  • ⚡ Fast inference with .reconstruct() method
  • 🧼 Clean, production-ready code

📦 Installation

You can access the PyPI page or install the package directly.

pip install variational-autoencoder-pytorch-lib

📁 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

variational_autoencoder_pytorch_lib-1.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file variational_autoencoder_pytorch_lib-1.0.0.tar.gz.

File metadata

File hashes

Hashes for variational_autoencoder_pytorch_lib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ce114a5d4e2cd12a8790867cd550c26a86a85763a9086dde6b264086ee450ecd
MD5 96082748bb9830d29df5fe9aa92eb92e
BLAKE2b-256 186aa81f6eaeaca05ea9222f21a5974a500064fd2bd05bef3e8469b6b7eb08d9

See more details on using hashes here.

File details

Details for the file variational_autoencoder_pytorch_lib-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for variational_autoencoder_pytorch_lib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f6cc80a51854ee92ed2afb111c413997141547406baef78bfcb1a79c7278a1f
MD5 60a4ea65d5fab79c3f9e85263bc0ef26
BLAKE2b-256 863a8affe66544da00f9d7ee5be2416bd5b24c5eea82c18c6583f9aed908ec9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page