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 ລ້ວນໃຊ້ແນວຄິດນີ້
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
- #ບອກຕໍ່ຄວາມຮູ້