生成式对抗网络(Generative Adversarial Network, GAN)是一种对抗机器学习框架。它由两种神经网络组成:
- 生成器(Generator):产生新的样本数据,试图生成真实数据的 replica。
- 判别器(Discriminator):判断样本数据是否来自训练数据集,和生成器对抗。
GAN 的训练过程是生成器和判别器之间的矛盾对抗过程:
- 生成器产生假样本数据,尽量生成真实的样本。
- 判别器判断样本真假,提高自己的判断准确率。
- 根据判别器的判断,生成器不断提高自己生成数据的真实度。
- 根据生成器的提高,判别器也不断提高判断准确率。
- 这样形成一个спираль上升进程,最终达到纳什均衡。
GAN 的代码示例:
python
import torch
import torch.nn as nn
# Generator
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.lin1 = nn.Linear(64, 256)
self.lin2 = nn.Linear(256, 512)
self.lin3 = nn.Linear(512, 28*28)
def forward(self, z):
x = torch.relu(self.lin1(z))
x = torch.relu(self.lin2(x))
x = torch.sigmoid(self.lin3(x))
x = x.view(-1, 1, 28, 28) # reshape into 1 channel image
return x
# Discriminator
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.lin1 = nn.Linear(28*28, 512)
self.lin2 = nn.Linear(512, 256)
self.lin3 = nn.Linear(256, 1)
def forward(self, x):
x = x.view(-1, 28*28) # reshape into vector
x = torch.relu(self.lin1(x))
x = torch.relu(self.lin2(x))
x = torch.sigmoid(self.lin3(x))
return x
# Training
z = torch.randn(64, 64) # noise vector
fake_img = generator(z) # generate fake images
real_img = some_real_images # get some real images
fake_out = discriminator(fake_img) # discriminate
real_out = discriminator(real_img)
loss_fn = nn.BCELoss()
gan_loss = loss_fn(real_out, torch.ones_like(real_out)) + loss_fn(fake_out, torch.zeros_like(fake_out))
discriminator.zero_grad() # set gradients to 0
gan_loss.backward() # calculate gradients
optimizer.step() # update discriminator weights
# Similar process for updating generator
所以,GAN 通过 rivals 生成器和判别器的角色互换训练,实现模型的无监督学习。它在图像生成、语音生成和图像增强等领域有很 broad 的应用。