Chuyển đến nội dung chính

第 3 課:VAE — 變分自動編碼器與潛在空間

自動編碼器回顧。 VAE:ELBO、重新參數化技巧、KL 散度。潛在空間探索和插值。有條件的 VAE。用於離散潛在變數的 VQ-VAE。比較 VAE 與 GAN。

🧠 人工智慧與機器學習 — 第 2 課 第 3 課:VAE — 變分自動編碼器 & 潛在空間

生成式 AI:使用 AI 創建圖像和視頻

第 1 部分:生成式 AI 平台 — 理論與架構

亞洲開發網

簡介

VAE(變分自動編碼器)是一種將深度學習與貝葉斯推理結合的生成模型。與針對兩個網路的 GAN 不同,VAE 學習結構化的潛在空間——允許插值、受控生成和精確的可能性估計。


1. 自動編碼器回顧

import torch.nn as nn

class Autoencoder(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder = nn.Sequential(
            nn.Linear(784, 256),
            nn.ReLU(),
            nn.Linear(256, 64),  # bottleneck
        )
        self.decoder = nn.Sequential(
            nn.Linear(64, 256),
            nn.ReLU(),
            nn.Linear(256, 784),
            nn.Sigmoid(),
        )

    def forward(self, x):
        z = self.encoder(x)  # compress
        x_hat = self.decoder(z)  # reconstruct
        return x_hat

# Loss: ||x - x_hat||² (reconstruction error)

問題: 自動編碼器潛在空間是間歇性的 → 無法取樣隨機 z 來產生。


2. VAE——核心理念

┌─────────────────────────────────────────────────────────┐
│                    VAE Architecture                      │
│                                                         │
│  x ──→ Encoder ──→ μ, σ ──→ z = μ + σ·ε ──→ Decoder → x̂│
│                          ↑                               │
│                     ε ~ N(0, 1)                          │
│                (reparameterization trick)                 │
│                                                         │
│  Loss = Reconstruction + KL Divergence                  │
│       = ||x - x̂||² + KL(q(z|x) || p(z))               │
└─────────────────────────────────────────────────────────┘

ELBO — 證據下界

$$\mathcal{L} = \mathbb{E}{q(z|x)}[\log p(x|z)] - D{KL}(q(z|x) || p(z))$$

  • 重建損失:$\mathbb{E}_{q(z|x)}[\log p(x|z)]$ - 解碼必須與輸入相同
  • KL 散度:$D_{KL}(q(z|x) || p(z))$ — 迫使潛在分佈接近 $\mathcal{N}(0, I)$

3. 實施 VAE

class VAE(nn.Module):
    def __init__(self, input_dim=784, latent_dim=20):
        super().__init__()
        # Encoder
        self.fc1 = nn.Linear(input_dim, 400)
        self.fc_mu = nn.Linear(400, latent_dim)      # mean
        self.fc_logvar = nn.Linear(400, latent_dim)   # log variance

        # Decoder
        self.fc3 = nn.Linear(latent_dim, 400)
        self.fc4 = nn.Linear(400, input_dim)

    def encode(self, x):
        h = torch.relu(self.fc1(x))
        mu = self.fc_mu(h)
        logvar = self.fc_logvar(h)
        return mu, logvar

    def reparameterize(self, mu, logvar):
        """Reparameterization trick: z = μ + σ · ε"""
        std = torch.exp(0.5 * logvar)
        eps = torch.randn_like(std)  # ε ~ N(0, 1)
        return mu + eps * std

    def decode(self, z):
        h = torch.relu(self.fc3(z))
        return torch.sigmoid(self.fc4(h))

    def forward(self, x):
        mu, logvar = self.encode(x.view(-1, 784))
        z = self.reparameterize(mu, logvar)
        x_hat = self.decode(z)
        return x_hat, mu, logvar

def vae_loss(x_hat, x, mu, logvar):
    # Reconstruction loss (BCE)
    recon = nn.functional.binary_cross_entropy(
        x_hat, x.view(-1, 784), reduction='sum'
    )
    # KL divergence: -0.5 * Σ(1 + log(σ²) - μ² - σ²)
    kl = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
    return recon + kl

4. 重新參數化技巧

Vấn đề: z ~ q(z|x) → sampling không differentiable → không thể backprop

Giải pháp: z = μ + σ · ε, với ε ~ N(0, 1)
- μ, σ là output của encoder (differentiable)
- ε là random noise (không phụ thuộc parameters)
→ Gradient có thể flow qua μ, σ
# ❌ Không thể backprop qua sampling
z = torch.distributions.Normal(mu, std).sample()

# ✅ Reparameterization trick
eps = torch.randn_like(std)
z = mu + eps * std  # gradient flows through mu and std

5.潛在空間探索

插值

def interpolate(model, x1, x2, steps=10):
    """Interpolate between 2 images in latent space"""
    model.eval()
    with torch.no_grad():
        mu1, _ = model.encode(x1.view(-1, 784))
        mu2, _ = model.encode(x2.view(-1, 784))

        images = []
        for alpha in torch.linspace(0, 1, steps):
            z = (1 - alpha) * mu1 + alpha * mu2
            img = model.decode(z)
            images.append(img.view(28, 28))

    return images  # smooth transition từ x1 → x2

隨機生成

def generate(model, num_images=16):
    """Generate new images by sampling from latent space"""
    model.eval()
    with torch.no_grad():
        z = torch.randn(num_images, 20)  # sample từ N(0, I)
        images = model.decode(z)
    return images.view(num_images, 28, 28)

6. 條件 VAE (CVAE)

class ConditionalVAE(nn.Module):
    """VAE conditioned on label → kiểm soát generation"""
    def __init__(self, input_dim=784, latent_dim=20, num_classes=10):
        super().__init__()
        # Encoder nhận cả x và label
        self.fc1 = nn.Linear(input_dim + num_classes, 400)
        self.fc_mu = nn.Linear(400, latent_dim)
        self.fc_logvar = nn.Linear(400, latent_dim)

        # Decoder nhận z và label
        self.fc3 = nn.Linear(latent_dim + num_classes, 400)
        self.fc4 = nn.Linear(400, input_dim)

    def encode(self, x, y_onehot):
        h = torch.relu(self.fc1(torch.cat([x, y_onehot], dim=1)))
        return self.fc_mu(h), self.fc_logvar(h)

    def decode(self, z, y_onehot):
        h = torch.relu(self.fc3(torch.cat([z, y_onehot], dim=1)))
        return torch.sigmoid(self.fc4(h))

# Generate digit "7":
y = torch.zeros(1, 10)
y[0, 7] = 1  # one-hot cho số 7
z = torch.randn(1, 20)
img = model.decode(z, y)  # → ảnh số 7

7. VQ-VAE — 向量量化 VAE

Ý tưởng: Thay continuous latent → discrete codebook
- Encoder output → tìm nearest codebook vector
- Codebook: tập các learnable vectors
- Decoder nhận discrete code → reconstruct

Ưu điểm:
- Avoid posterior collapse
- Codebook = "vocabulary" của visual concepts
- Nền tảng cho DALL-E 1 (VQ-VAE + Transformer)
class VectorQuantizer(nn.Module):
    def __init__(self, num_embeddings=512, embedding_dim=64):
        super().__init__()
        self.codebook = nn.Embedding(num_embeddings, embedding_dim)

    def forward(self, z):
        # Tìm nearest codebook vector
        distances = torch.cdist(z, self.codebook.weight)
        indices = distances.argmin(dim=-1)
        z_q = self.codebook(indices)

        # Straight-through estimator
        z_q = z + (z_q - z).detach()
        return z_q, indices

8.VAE 與 GAN

特點VAE甘
訓練穩定、單一目標不穩定,極小極大
生成的品質模糊夏普
潛在空間層次分明、流暢非結構化
可能性易處理(ELBO)棘手的
多元化好模式崩潰風險
插值光滑不可預測
使用案例潛在操縱高品質合成

總結

概念描述
VAE具有機率潛在空間的編碼器-解碼器
埃爾博證據下限 = Recon + KL
重新參數化z = μ + σ·ε — 透過取樣啟用反向傳播
KL 背離按 N(0, I) 附近潛伏
CVAE條件VAE——受控發電
VQ-VAE離散碼本—DALL-E 1 平台

📌 下一篇文章: 擴散模型 — 從頭開始的數學、直覺和 DDPM。