Student starter code (30% baseline)
index.html- Main HTML pagescript.js- JavaScript logicstyles.css- Styling and layoutpackage.json- Dependenciessetup.sh- Setup scriptREADME.md- Instructions (below)💡 Download the ZIP, extract it, and follow the instructions below to get started!
By completing this activity, you will:
Runtime -> Run all (or press Ctrl+F9)Expected First Run Time: ~45 seconds
The template comes with 65% working code:
Location: Section 4 - "VAE Model Architecture"
Current State: Encoder produces μ (mu) and log_σ (log-sigma), but sampling z is not implemented
Your Task: Implement the reparameterization trick to sample latent code z from N(μ, σ²):
z = μ + σ * ε where ε ~ N(0, 1)
σ = exp(log_σ / 2)
Why This Matters:
Starter Code Provided:
def reparameterize(self, mu, log_var):
"""
Reparameterization trick: z = μ + σ * ε
Args:
mu: Mean of latent distribution (batch_size, latent_dim)
log_var: Log variance of latent distribution (batch_size, latent_dim)
Returns:
z: Sampled latent code (batch_size, latent_dim)
"""
# TODO: Implement reparameterization trick
# Hint 1: std = torch.exp(log_var / 2)
# Hint 2: eps = torch.randn_like(std)
# Hint 3: z = mu + std * eps
pass
Success Criteria:
z.requires_grad == True)Test Your Implementation:
# Test: z should be different each time
mu = torch.zeros(1, 20)
log_var = torch.zeros(1, 20)
z1 = model.reparameterize(mu, log_var)
z2 = model.reparameterize(mu, log_var)
assert not torch.allclose(z1, z2), "z should be stochastic!"
print("✅ Reparameterization trick working!")
Location: Section 5 - "Loss Function"
Your Task: Implement the Evidence Lower Bound (ELBO) loss for VAE training:
ELBO = E[log p(x|z)] - KL[q(z|x) || p(z)]
Loss = -ELBO = reconstruction_loss + β * KL_divergence
Components:
Reconstruction Loss: How well can decoder reconstruct input?
BCE(x, x_reconstructed)KL Divergence: How close is q(z|x) to prior p(z) = N(0, I)?
0.5 * Σ[1 + log_σ² - μ² - σ²]Beta Weighting: Balance reconstruction vs regularization
Starter Code Provided:
def vae_loss(x, x_recon, mu, log_var, beta=1.0):
"""
Compute VAE loss (negative ELBO).
Args:
x: Original images (batch_size, 1, 28, 28)
x_recon: Reconstructed images (batch_size, 1, 28, 28)
mu: Latent mean (batch_size, latent_dim)
log_var: Latent log variance (batch_size, latent_dim)
beta: Weight for KL divergence (default 1.0)
Returns:
loss: Total loss (scalar)
recon_loss: Reconstruction loss (for logging)
kl_loss: KL divergence (for logging)
"""
# TODO 2.1: Compute reconstruction loss
# Hint: Use F.binary_cross_entropy(x_recon, x, reduction='sum')
# Hint: Divide by batch size
recon_loss = None
# TODO 2.2: Compute KL divergence
# Hint: KL = -0.5 * sum(1 + log_var - mu^2 - exp(log_var))
# Hint: Sum over latent_dim, average over batch
kl_loss = None
# TODO 2.3: Combine losses
loss = recon_loss + beta * kl_loss
return loss, recon_loss, kl_loss
Success Criteria:
Location: Section 8 - "Latent Space Analysis"
Your Task: Visualize what each dimension of the latent space controls by traversing one dimension at a time.
Requirements:
Success Criteria:
Modify VAE to condition on class labels
Implement β-VAE with β > 1.0
Implement IWAE for tighter ELBO bound
Train VAE with latent_dim=2 for full visualization
Minimum Requirements:
Target Grade:
This is normal for VAEs! Try increasing latent dimension or reducing beta.
Use KL annealing: Start with β=0, gradually increase to 1.0 over epochs.
pytorch/exampleszalandoresearch/fashion-mnistGood luck! VAEs are the foundation of modern generative AI! 🚀