当前位置:   article > 正文

学习笔记:Pytorch利用MNIST数据集训练生成对抗网络(GAN)_生成对抗网络loss曲线

生成对抗网络loss曲线

2023.8.27

       在进行深度学习的进阶的时候,我发了生成对抗网络是一个很神奇的东西,为什么它可以“将一堆随机噪声经过生成器变成一张图片”,特此记录一下学习心得。

一、生成对抗网络百科

        2014年,还在蒙特利尔读博士的Ian Goodfellow发表了论 文《Generative Adversarial Networks》(网址: https://arxiv.org/abs/1406.2661),将生成对抗网络引入 深度学习领域。2016年,GAN热潮席卷AI领域顶级会议, 从ICLR到NIPS,大量高质量论文被发表和探讨。Yann LeCun曾评价GAN是“20年来机器学习领域最酷的想法”。

机器学习的模型可大体分为两类,生成模型( Generative Model)和判别模型(Discriminative Model)。判别模型需要输入变量 ,通过某种模型来 预测 。生成模型是给定某种隐含信息,来随机产生观 测数据。

GAN百科:

GAN(生成对抗网络)的系统全面介绍(醍醐灌顶)_打灰人的博客-CSDN博客

二、GAN代码

训练代码:

                epoch=1000时的效果就不错啦

  1. import torch
  2. import torch.nn as nn
  3. import torch.optim as optim
  4. from torchvision import datasets, transforms
  5. from torch.utils.data import DataLoader
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. class Generator(nn.Module): # 生成器
  9. def __init__(self, latent_dim):
  10. super(Generator, self).__init__()
  11. self.model = nn.Sequential(
  12. nn.Linear(latent_dim, 256),
  13. nn.LeakyReLU(0.2),
  14. nn.Linear(256, 512),
  15. nn.LeakyReLU(0.2),
  16. nn.Linear(512, 1024),
  17. nn.LeakyReLU(0.2),
  18. nn.Linear(1024, 784),
  19. nn.Tanh()
  20. )
  21. def forward(self, z):
  22. img = self.model(z)
  23. img = img.view(img.size(0), 1, 28, 28)
  24. return img
  25. class Discriminator(nn.Module): # 判别器
  26. def __init__(self):
  27. super(Discriminator, self).__init__()
  28. self.model = nn.Sequential(
  29. nn.Linear(784, 512),
  30. nn.LeakyReLU(0.2),
  31. nn.Linear(512, 256),
  32. nn.LeakyReLU(0.2),
  33. nn.Linear(256, 1),
  34. nn.Sigmoid()
  35. )
  36. def forward(self, img):
  37. img = img.view(img.size(0), -1)
  38. validity = self.model(img)
  39. return validity
  40. def gen_img_plot(model, test_input):
  41. pred = np.squeeze(model(test_input).detach().cpu().numpy())
  42. fig = plt.figure(figsize=(4, 4))
  43. for i in range(16):
  44. plt.subplot(4, 4, i + 1)
  45. plt.imshow((pred[i] + 1) / 2)
  46. plt.axis('off')
  47. plt.show(block=False)
  48. plt.pause(3) # 停留0.5s
  49. plt.close()
  50. # 调用GPU
  51. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  52. # 超参数设置
  53. lr = 0.0001
  54. batch_size = 128
  55. latent_dim = 100
  56. epochs = 1000
  57. # 数据集载入和数据变换
  58. transform = transforms.Compose([
  59. transforms.ToTensor(),
  60. transforms.Normalize((0.5,), (0.5,))
  61. ])
  62. # 训练数据
  63. train_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=False)
  64. train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
  65. # 测试数据 torch.randn()函数的作用是生成一组均值为0,方差为1(即标准正态分布)的随机数
  66. # test_data = torch.randn(batch_size, latent_dim).to(device)
  67. test_data = torch.FloatTensor(batch_size, latent_dim).to(device)
  68. # 实例化生成器和判别器,并定义损失函数和优化器
  69. generator = Generator(latent_dim).to(device)
  70. discriminator = Discriminator().to(device)
  71. adversarial_loss = nn.BCELoss()
  72. optimizer_G = optim.Adam(generator.parameters(), lr=lr)
  73. optimizer_D = optim.Adam(discriminator.parameters(), lr=lr)
  74. # 开始训练模型
  75. for epoch in range(epochs):
  76. for i, (imgs, _) in enumerate(train_loader):
  77. batch_size = imgs.shape[0]
  78. real_imgs = imgs.to(device)
  79. # 训练判别器
  80. z = torch.FloatTensor(batch_size, latent_dim).to(device)
  81. z.data.normal_(0, 1)
  82. fake_imgs = generator(z) # 生成器生成假的图片
  83. real_labels = torch.full((batch_size, 1), 1.0).to(device)
  84. fake_labels = torch.full((batch_size, 1), 0.0).to(device)
  85. real_loss = adversarial_loss(discriminator(real_imgs), real_labels)
  86. fake_loss = adversarial_loss(discriminator(fake_imgs.detach()), fake_labels)
  87. d_loss = (real_loss + fake_loss) / 2
  88. optimizer_D.zero_grad()
  89. d_loss.backward()
  90. optimizer_D.step()
  91. # 训练生成器
  92. z.data.normal_(0, 1)
  93. fake_imgs = generator(z)
  94. g_loss = adversarial_loss(discriminator(fake_imgs), real_labels)
  95. optimizer_G.zero_grad()
  96. g_loss.backward()
  97. optimizer_G.step()
  98. torch.save(generator.state_dict(), "Generator_mnist.pth")
  99. print(f"Epoch [{epoch}/{epochs}] Loss_D: {d_loss.item():.4f} Loss_G: {g_loss.item():.4f}")
  100. # gen_img_plot(Generator, test_data)
  101. gen_img_plot(generator, test_data)

测试代码:

  1. import torch
  2. import torch.nn as nn
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import random
  6. device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
  7. class Generator(nn.Module): # 生成器
  8. def __init__(self, latent_dim):
  9. super(Generator, self).__init__()
  10. self.model = nn.Sequential(
  11. nn.Linear(latent_dim, 256),
  12. nn.LeakyReLU(0.2),
  13. nn.Linear(256, 512),
  14. nn.LeakyReLU(0.2),
  15. nn.Linear(512, 1024),
  16. nn.LeakyReLU(0.2),
  17. nn.Linear(1024, 784),
  18. nn.Tanh()
  19. )
  20. def forward(self, z):
  21. img = self.model(z)
  22. img = img.view(img.size(0), 1, 28, 28)
  23. return img
  24. # test_data = torch.FloatTensor(128, 100).to(device)
  25. test_data = torch.randn(128, 100).to(device) # 随机噪声
  26. model = Generator(100).to(device)
  27. model.load_state_dict(torch.load('Generator_mnist.pth'))
  28. model.eval()
  29. pred = np.squeeze(model(test_data).detach().cpu().numpy())
  30. for i in range(64):
  31. plt.subplot(8, 8, i + 1)
  32. plt.imshow((pred[i] + 1) / 2)
  33. plt.axis('off')
  34. plt.savefig(fname='image.png', figsize=[5, 5])
  35. plt.show()

三、结果

       在超参数设置 epoch=1000,batch_size=128,lr=0.0001,latent_dim = 100 时,gan生成的权重测的结果如图所示

四,GAN的损失函数曲线

                一开始训练时,我的gan的损失函数的曲线是类似这样的,就是知乎这文章里一样,生成器损失函数的曲线一直发散。首先,这个loss的曲线一看就是网络崩了,一般正常的情况,d_loss的值会一直下降然后收敛,而g_loss的曲线会先增大后减少,最后同样也会收敛。其次,网络拿到手以后先不要训练太多次,容易出现过拟合的情况。

生成对抗网络的损失函数图像如下合理吗? - 知乎

这是训练了10轮的生成器和鉴别器的损失函数值变化吧:

效果如图所示: 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/537849
推荐阅读
  

闽ICP备14008679号