Autoencoders and GAN Lab Programs - Algorithms and Explanations
Autoencoders and GAN Lab Programs - Algorithms and Explanations
Explanations
Program 1: Undercomplete Autoencoder
Algorithm:
1. Load and normalize MNIST dataset (28×28 grayscale images)
2. Build encoder: Conv2D(32) → MaxPool → Conv2D(16) → MaxPool
Important Modules:
tensorflow.keras : Deep learning framework
matplotlib.pyplot : Visualization
Approach/Logic:
Undercomplete means the hidden layer has fewer neurons than input, forcing the network to learn
compressed representations. The encoder compresses 28×28=784 pixels into a smaller latent space, then
the decoder reconstructs the original image. This learns essential features by removing noise and
redundancy.
Algorithm:
1. Load and flatten MNIST data to 784-dimensional vectors
2. Create encoder: Dense(32) with ReLU activation
Important Modules:
tensorflow.keras : Neural network framework
layers.Dense : Fully connected layers
Approach/Logic:
Unlike regular autoencoders, denoising autoencoders are trained to reconstruct clean data from
corrupted inputs. The 32-dimensional bottleneck forces the network to learn robust features that can
handle noise. The reconstruction loss encourages learning meaningful representations rather than just
memorizing inputs.
Algorithm:
1. Define encoder that outputs mean (μ) and log-variance (σ²) of latent distribution
Important Modules:
keras.layers.Lambda : Custom sampling layer
Approach/Logic:
VAEs are probabilistic autoencoders. Instead of deterministic encoding, they learn probability
distributions in latent space. The reparameterization trick allows backpropagation through stochastic
nodes. KL divergence loss ensures latent space follows standard normal distribution, enabling smooth
interpolation and generation of new samples.
Algorithm:
1. Load MovieLens dataset and convert to binary ratings (like/dislike)
Important Modules:
torch : PyTorch deep learning framework
Approach/Logic:
RBMs are energy-based generative models. They learn probability distributions over visible units
through hidden units. Contrastive Divergence training approximates maximum likelihood by comparing
positive (data) and negative (model) statistics. The reconstruction error indicates how well the model
captures data patterns.
Algorithm:
1. Load digits dataset and normalize to [0,1]
Important Modules:
sklearn.neural_network.BernoulliRBM : RBM implementation
Approach/Logic:
Deep learning through layer-wise pretraining. Each RBM learns increasingly abstract features: RBM₁
learns edge-like features, RBM₂ learns part-like features. This hierarchical feature learning was crucial
before modern deep learning techniques, providing better initialization than random weights.
Algorithm:
1. Load MNIST dataset with standard preprocessing
Important Modules:
torch.nn : Neural network modules
Approach/Logic:
DBNs combine generative pretraining with discriminative fine-tuning. Though this implementation
shows the discriminative part, true DBNs first pretrain each layer as an RBM, then fine-tune the entire
network. This creates robust feature hierarchies for classification tasks.
Algorithm:
1. Initialize two weight matrices W₁(visible↔hidden₁), W₂(hidden₁↔hidden₂)
Important Modules:
numpy : Numerical computing
Approach/Logic:
DBMs are undirected graphical models with multiple hidden layers. Unlike stacked RBMs, all layers
interact bidirectionally during inference. The mean-field approximation is used for inference, making
training more complex but potentially more powerful for learning hierarchical representations.
Algorithm:
1. Load MNIST and reshape for convolutional processing
Important Modules:
keras.Sequential : Sequential model building
Approach/Logic:
Spatial compression using convolutions. Max pooling reduces spatial dimensions while preserving
important features. The bottleneck layer represents compressed data. Upsampling reconstructs spatial
dimensions. This is more efficient than fully-connected autoencoders for images as it preserves spatial
relationships.
Algorithm:
1. Load and normalize MNIST dataset
3. Decoder: Conv2D(8)→UpSample→Conv2D(16)→UpSample→Conv2D(1)
Important Modules:
tensorflow.keras.models.Model : Functional API model
matplotlib.pyplot : Visualization
Approach/Logic:
CNN-based generation through reconstruction. The model learns to generate images by
reconstructing training examples. The convolutional structure captures spatial hierarchies: early layers
learn edges, deeper layers learn textures and shapes. This forms the basis for more complex generative
models.
Algorithm:
1. Create Generator: Linear(100→256→784) with Tanh output
2. Create Discriminator: Linear(784→256→1) with Sigmoid output
Important Modules:
torch.nn.Sequential : Sequential model building
Approach/Logic:
Adversarial training between two networks. Generator learns to create realistic data from noise, while
Discriminator learns to distinguish real from fake data. This creates a minimax game: G tries to minimize
log(1-D(G(z))) while D tries to maximize log(D(x)) + log(1-D(G(z))). The competition drives both networks
to improve, eventually generating high-quality samples.
Key Concepts Summary:
1. Autoencoders: Self-supervised learning through reconstruction
Each approach solves different aspects of representation learning and generation, from compression to
probabilistic modeling to adversarial training.