Views
No views yet
1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4import matplotlib.pyplot as plt
5from torchvision import datasets, transforms
6
7# Load the model (after downloading the files)
8class VAE(nn.Module):
9 def __init__(self, input_dim=784, latent_dim=2, hidden_dim=256, beta=1.0):
10 super(VAE, self).__init__()
11 # ... (full implementation in the notebook)
12
13 def forward(self, x):
14 # ... (full implementation in the notebook)
15 pass
16
17# Load trained model
18model = VAE()
19model.load_state_dict(torch.load('vae_logs_latent2_beta1.0/best_vae_model.pth'))
20model.eval()
21
22# Generate new samples
23with torch.no_grad():
24 # Sample from latent space
25 z = torch.randn(16, 2) # 16 samples, 2D latent space
26 generated_images = model.decode(z)
27
28 # Reshape and visualize
29 generated_images = generated_images.view(-1, 28, 28)
30 # Plot the generated images...Untitled.ipynb: Complete implementation with training and visualizationbest_vae_model.pth: Trained model weightstraining_metrics.csv: Detailed training metricsgenerated_samples.png: Grid of generated digit sampleslatent_space_visualization.png: 2D latent space plotreconstruction_comparison.png: Original vs reconstructed imageslatent_interpolation.png: Interpolation between digit pairscomprehensive_training_curves.png: Training loss curves1@misc{vae_mnist_implementation,
2 title={Variational Autoencoder Implementation for MNIST},
3 author={Gruhesh Kurra},
4 year={2024},
5 url={https://huggingface.co/karthik-2905/VariationalAutoencoders}
6}grok.md for comprehensive VAE explanations