Ainsteinx Logo
Ainsteinx
👤
ยินดีต้อนรับสู่ Ainsteinx
เข้าสู่ระบบ

เปลี่ยนภาษา

สกุลเงิน

หน้าแรก
บล็อก
บริการ
บทที่ 08 · 6 เทคนิคหลัก

Diffusion Models

เพิ่ม noise ทีละน้อย แล้วเรียนลบมันออก — หัวใจของ AI วาดภาพในปัจจุบัน

Forward process เพิ่ม noise ให้ข้อมูล T ก้าวจนกลายเป็น noise ล้วน Reverse process ใช้ network (ปกติคือ U-Net) ทาย noise ที่ถูกเพิ่ม แล้วลบออกทีละก้าวจนได้ข้อมูลใหม่

โค้ดตัวอย่างคือ DDPM ขนาดเล็กใน 2 มิติ: สร้างจุด 1,000 จุดจาก noise ที่อยู่ใกล้รูปพระจันทร์เสี้ยวของข้อมูลจริง

forward: xₜ = √ᾱₜ · x₀ + √(1−ᾱₜ) · ε ← กระโดดไปก้าว t ได้ทันที loss: ‖ ε − ε_θ(xₜ, t) ‖² reverse: xₜ₋₁ = (xₜ − βₜ/√(1−ᾱₜ) · ε_θ) / √αₜ + σₜ·z
Noise scheduleกำหนดว่าแต่ละก้าวเพิ่ม noise เท่าไร (linear, cosine)
U-NetDownBlock ลดขนาด UpBlock ขยายกลับ พร้อม skip connection
DDIMสุ่มแบบ deterministic และใช้ก้าวน้อยลง จึงเร็วกว่า

สิ่งที่ควรจำ

  • Network เรียนเพียงสิ่งเดียว: ทาย noise
  • Stable Diffusion, DALL·E 2 และ Imagen ล้วนใช้แนวคิดนี้
generative-ai-ep08-diffusion.py
import torch, torch.nn as nn
from sklearn.datasets import make_moons

torch.manual_seed(0)
X = torch.tensor(make_moons(4000, noise=0.05, random_state=0)[0], dtype=torch.float32)
T = 100
betas = torch.linspace(1e-4, 0.05, T)                 # noise schedule
alphas = 1 - betas
abar = torch.cumprod(alphas, 0)

# Noise predictor eps(x_t, t): a small MLP (a U-Net does this job for images)
net = nn.Sequential(nn.Linear(3, 128), nn.SiLU(), nn.Linear(128, 128), nn.SiLU(), nn.Linear(128, 2))
opt = torch.optim.Adam(net.parameters(), lr=2e-3)

for step in range(3000):
    x0 = X[torch.randint(0, len(X), (256,))]
    t = torch.randint(0, T, (256,))
    eps = torch.randn_like(x0)
    xt = abar[t].sqrt()[:, None] * x0 + (1 - abar[t]).sqrt()[:, None] * eps   # forward: jump to step t
    loss = ((net(torch.cat([xt, t[:, None] / T], 1)) - eps) ** 2).mean()
    opt.zero_grad(); loss.backward(); opt.step()
print(f"final noise-prediction loss {loss.item():.3f}")

with torch.no_grad():                                 # reverse: denoise step by step
    x = torch.randn(1000, 2)
    for t in reversed(range(T)):
        eps = net(torch.cat([x, torch.full((1000, 1), t / T)], 1))
        x = (x - betas[t] / (1 - abar[t]).sqrt() * eps) / alphas[t].sqrt()
        if t > 0:
            x += betas[t].sqrt() * torch.randn_like(x)
dist = torch.cdist(x, X).min(1).values
print(f"generated 1000 points, median distance to real data: {dist.median():.3f}")
print(f"x range [{x[:, 0].min():.2f}, {x[:, 0].max():.2f}]  (real data: [-1.0, 2.0])")

ผลลัพธ์

final noise-prediction loss 0.359
generated 1000 points, median distance to real data: 0.031
x range [-1.44, 2.29]  (real data: [-1.0, 2.0])
  • #GenerativeAI
  • #DeepLearning
  • #PythonສຳລັບຄົນLaos
  • #AI
  • #ບອກຕໍ່ຄວາມຮູ້