生成对抗网络(GAN)
伪造者对抗侦探:两个网络相互竞争,直到都变得很强。
GAN 有两个网络:生成器把噪声变成伪造数据,判别器努力区分真假。两者轮流训练,直到生成器产生的数据让判别器无法分辨。
示例训练 GAN 生成圆周上 8 个簇的点,并检查是否覆盖全部 8 个簇——用来检测模式坍塌。
D: max E[log D(x)] + E[log(1 − D(G(z)))]
G: max E[log D(G(z))]
WGAN-GP critic: E[C(fake)] − E[C(real)] + λ·E[(‖∇C(x̂)‖ − 1)²]
| 问题 | 表现 | 解决办法 |
|---|---|---|
| Mode collapse | 只生成少数几种结果 | WGAN-GP, minibatch tricks |
| 梯度消失 | D 太强,G 学不到东西 | Wasserstein 损失 + 梯度惩罚 |
| 想指定类别 | 采样结果混在一起 | Conditional GAN (CGAN) |
要点
- GAN 不建模 p(x),而是直接学习采样,所以图像很清晰
- 训练 GAN 的关键在于平衡 D 和 G
import math, torch, torch.nn as nn
torch.manual_seed(0)
# Real data: 8 Gaussian blobs on a circle, a classic test for "mode collapse"
centers = torch.tensor([[math.cos(2 * math.pi * k / 8), math.sin(2 * math.pi * k / 8)] for k in range(8)]) * 2
def real_batch(n=256):
return centers[torch.randint(0, 8, (n,))] + 0.05 * torch.randn(n, 2)
def mlp(out):
return nn.Sequential(nn.Linear(2, 256), nn.LeakyReLU(0.2),
nn.Linear(256, 256), nn.LeakyReLU(0.2), nn.Linear(256, out))
G, D = mlp(2), mlp(1) # generator: noise -> point, discriminator: point -> real?
opt_g = torch.optim.Adam(G.parameters(), lr=1e-4, betas=(0.5, 0.9))
opt_d = torch.optim.Adam(D.parameters(), lr=1e-4, betas=(0.5, 0.9))
bce = nn.BCEWithLogitsLoss()
for step in range(4001):
real, fake = real_batch(), G(torch.randn(256, 2))
# 1) discriminator: real -> 1, fake -> 0
d_loss = bce(D(real), torch.ones(256, 1)) + bce(D(fake.detach()), torch.zeros(256, 1))
opt_d.zero_grad(); d_loss.backward(); opt_d.step()
# 2) generator: fool D into saying 1
g_loss = bce(D(fake), torch.ones(256, 1))
opt_g.zero_grad(); g_loss.backward(); opt_g.step()
if step % 1000 == 0:
with torch.no_grad():
dist, nearest = torch.cdist(G(torch.randn(4000, 2)), centers).min(1)
hit = dist < 0.25 # sample landed on a real blob
covered = (torch.bincount(nearest[hit], minlength=8) > 100).sum().item()
print(f"step {step:4d} D {d_loss.item():.2f} G {g_loss.item():.2f} "
f"on target {hit.float().mean():.0%} modes covered {covered}/8")
输出结果
step 0 D 1.41 G 0.75 on target 0% modes covered 0/8 step 1000 D 1.26 G 0.79 on target 44% modes covered 8/8 step 2000 D 1.16 G 0.92 on target 60% modes covered 8/8 step 3000 D 1.19 G 0.92 on target 69% modes covered 8/8 step 4000 D 1.21 G 1.00 on target 74% modes covered 8/8
- #GenerativeAI
- #DeepLearning
- #PythonສຳລັບຄົນLaos
- #AI
- #ບອກຕໍ່ຄວາມຮູ້