Get startedGet started for free

Building a generator and discriminator

At PyBooks, you're tasked with working on an automatic text generator to help writers overcome writer's block. By using GANs, or Generative Adversarial Networks, you believe you can create a system where one network, the generator, creates new text while the other network, the discriminator, evaluates its authenticity. To do this, you need to initialize both a generator and discriminator network. These networks will then be trained against each other to create new, believable text.

The following has been imported for you: torch, torch.nn as nn.

This exercise is part of the course

Deep Learning for Text with PyTorch

View Course

Exercise instructions

  • Define the Generator class with a linear layer for sequential data and a sigmoid activation function.
  • Pass the input through the defined model in the forward() method of the Generator class.
  • Define a Discriminator class with the same layers and activation function, taking care when defining the dimensions.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define the generator class
class Generator(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.____(nn.____(____), nn.____())
    def forward(self, x):
        return self.____(x)

# Define the discriminator networks
class Discriminator(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.____(nn.____(____), nn.____())
    def forward(self, x):
        return self.model(x)
Edit and Run Code