9951 explained code solutions for 126 technologies


python-pytorchHow can I use Python and PyTorch to develop a Generative Adversarial Network (GAN)?


A Generative Adversarial Network (GAN) is a type of deep learning architecture used for unsupervised learning. It consists of two neural networks, a generator and a discriminator, which compete with each other in a zero-sum game. The generator tries to generate realistic data from a given input while the discriminator tries to distinguish between real data and generated data.

To create a GAN using Python and PyTorch, we will need to create two neural networks, one for the generator and one for the discriminator. We will also need to define a loss function and an optimizer.

import torch
import torch.nn as nn
import torch.optim as optim

# define generator and discriminator
class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        self.fc1 = nn.Linear(in_features=100, out_features=256)
        self.fc2 = nn.Linear(in_features=256, out_features=512)
        self.fc3 = nn.Linear(in_features=512, out_features=784)

    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()
        self.fc1 = nn.Linear(in_features=784, out_features=512)
        self.fc2 = nn.Linear(in_features=512, out_features=256)
        self.fc3 = nn.Linear(in_features=256, out_features=1)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        x = self.sigmoid(x)
        return x

# define loss function and optimizer
criterion = nn.BCELoss()
optimizer_G = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999))
optimizer_D = optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999))

We can now train the GAN by running the generator and discriminator networks in an alternating fashion. At each iteration, we will pass a batch of data through the generator, and then pass the generated data through the discriminator. We will then calculate the loss and update the weights of the networks accordingly.

  • import torch: Imports the PyTorch library.
  • class Generator: Defines the generator neural network.
  • class Discriminator: Defines the discriminator neural network.
  • criterion: Defines the loss function.
  • optimizer_G: Defines the optimizer for the generator.
  • optimizer_D: Defines the optimizer for the discriminator.

Helpful links

Edit this code on GitHub