LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Deep Learning for Text with PyTorch

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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)
Code bearbeiten und ausführen