使用pytorch写一个生成对抗网络
时间: 2023-05-11 15:02:57 浏览: 141
好的,生成对抗网络(GAN)是一种深度学习模型,它由两个神经网络组成:生成器和判别器。生成器的目标是生成与真实数据相似的假数据,而判别器的目标是区分真实数据和假数据。在训练过程中,生成器和判别器相互竞争,最终生成器可以生成高质量的假数据。下面是使用PyTorch编写的GAN的示例代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# 定义生成器
class Generator(nn.Module):
def __init__(self, latent_dim, img_shape):
super(Generator, self).__init__()
self.img_shape = img_shape
self.model = nn.Sequential(
nn.Linear(latent_dim, 128),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(128, 256),
nn.BatchNorm1d(256, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(256, 512),
nn.BatchNorm1d(512, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 1024),
nn.BatchNorm1d(1024, 0.8),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(1024, int(torch.prod(torch.tensor(img_shape)))),
nn.Tanh()
)
def forward(self, z):
img = self.model(z)
img = img.view(img.size(0), *self.img_shape)
return img
# 定义判别器
class Discriminator(nn.Module):
def __init__(self, img_shape):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Linear(int(torch.prod(torch.tensor(img_shape))), 512),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 256),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(256, 1),
nn.Sigmoid(),
)
def forward(self, img):
img_flat = img.view(img.size(0), -1)
validity = self.model(img_flat)
return validity
# 定义训练函数
def train(generator, discriminator, dataloader, optimizer_G, optimizer_D, device):
adversarial_loss = nn.BCELoss()
for epoch in range(n_epochs):
for i, (imgs, _) in enumerate(dataloader):
# 训练判别器
optimizer_D.zero_grad()
real_imgs = imgs.to(device)
batch_size = real_imgs.size(0)
valid = torch.ones(batch_size, 1).to(device)
fake = torch.zeros(batch_size, 1).to(device)
z = torch.randn(batch_size, latent_dim).to(device)
gen_imgs = generator(z)
real_loss = adversarial_loss(discriminator(real_imgs), valid)
fake_loss = adversarial_loss(discriminator(gen_imgs.detach()), fake)
d_loss = (real_loss + fake_loss) / 2
d_loss.backward()
optimizer_D.step()
# 训练生成器
optimizer_G.zero_grad()
z = torch.randn(batch_size, latent_dim).to(device)
gen_imgs = generator(z)
g_loss = adversarial_loss(discriminator(gen_imgs), valid)
g_loss.backward()
optimizer_G.step()
# 打印训练信息
batches_done = epoch * len(dataloader) + i
if batches_done % sample_interval == 0:
print("[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
% (epoch, n_epochs, i, len(dataloader), d_loss.item(), g_loss.item()))
# 定义超参数
img_shape = (1, 28, 28)
latent_dim = 100
n_epochs = 200
batch_size = 64
lr = 0.0002
b1 = 0.5
b2 = 0.999
sample_interval = 400
# 加载数据集
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
mnist_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
dataloader = DataLoader(mnist_dataset, batch_size=batch_size, shuffle=True)
# 初始化生成器和判别器
generator = Generator(latent_dim, img_shape).to(device)
discriminator = Discriminator(img_shape).to(device)
# 定义优化器
optimizer_G = optim.Adam(generator.parameters(), lr=lr, betas=(b1, b2))
optimizer_D = optim.Adam(discriminator.parameters(), lr=lr, betas=(b1, b2))
# 训练模型
train(generator, discriminator, dataloader, optimizer_G, optimizer_D, device)
```
这是一个简单的GAN示例,它可以生成手写数字图像。如果你想了解更多关于GAN的知识,可以参考PyTorch官方文档或者其他深度学习教程。
阅读全文