AUTOENCODER → VAE · 15 OF 17 · THE CODE
Autoencoder vs. VAE in PyTorch
The code Torch Canvas writes for the two drawings; the VAE’s new lines are highlighted
class Autoencoder(nn.Module):
def __init__(self):
self.enc1 = nn.Linear(64, 128)
self.enc2 = nn.Linear(128, 64)
self.enc3 = nn.Linear(64, 2)
self.dec1 = nn.Linear(2, 64)
self.dec2 = nn.Linear(64, 128)
self.dec3 = nn.Linear(128, 64)
def forward(self, x):
a = F.relu(self.enc1(x)) # (e, 128)
a = F.relu(self.enc2(a)) # (e, 64)
z = self.enc3(a) # (e, 2)
a = F.relu(self.dec1(z)) # (e, 64)
a = F.relu(self.dec2(a)) # (e, 128)
return self.dec3(a)
def loss(self, recon, x):
return 64 * F.binary_cross_entropy_with_logits(recon, x)
class VAE(nn.Module):
def __init__(self):
self.enc1 = nn.Linear(64, 128)
self.enc2 = nn.Linear(128, 64)
self.enc_mu = nn.Linear(64, 2)
self.enc_logvar = nn.Linear(64, 2)
self.dec1 = nn.Linear(2, 64)
self.dec2 = nn.Linear(64, 128)
self.dec3 = nn.Linear(128, 64)
def forward(self, x):
a = F.relu(self.enc1(x)) # (e, 128)
a = F.relu(self.enc2(a)) # (e, 64)
mu = self.enc_mu(a) # (e, 2)
logvar = self.enc_logvar(a) # (e, 2)
std = torch.exp(logvar * 0.5) # (e, 2)
z = mu + torch.randn_like(std) * std # (e, 2)
a = F.relu(self.dec1(z)) # (e, 64)
a = F.relu(self.dec2(a)) # (e, 128)
return self.dec3(a), mu, logvar
def loss(self, recon, mu, logvar, x):
return (64 * F.binary_cross_entropy_with_logits(recon, x)
+ kl_normal(mu, logvar))
Ten changed lines turn an autoencoder into a generator.
kl_normal(μ, log σ²) = −½ · mean over the batch of Σ(1 + log σ² − μ² − σ²). super().__init__() and predict() left out; x.float() shortened to x.
© 2026 Dr. Jesse Roberts