Get startedGet started for free

Discriminator

With the generator defined, the next step in building a GAN is to construct the discriminator. It takes the generator's output as input, and produces a binary prediction: is the input generated or real?

You will find torch.nn imported already imported for you as nn. You can also access a custom disc_block() function which returns a block of a linear layer followed by a LeakyReLU activation. You will use it as a building block for the discriminator.

def disc_block(in_dim, out_dim):
    return nn.Sequential(
        nn.Linear(in_dim, out_dim),
        nn.LeakyReLU(0.2)
    )

This exercise is part of the course

Deep Learning for Images with PyTorch

View Course

Exercise instructions

  • Add the last discriminator block to the model, with the appropriate input size and the output of 256.
  • After the last discriminator block, add a linear layer to map the output to the size of 1.
  • Define the forward() method to pass the input image through the sequential block defined in __init__().

Hands-on interactive exercise

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

class Discriminator(nn.Module):
    def __init__(self, im_dim):
        super(Discriminator, self).__init__()
        self.disc = nn.Sequential(
            disc_block(im_dim, 1024),
            disc_block(1024, 512),
            # Define last discriminator block
            ____,
            # Add a linear layer
            ____,
        )

    def forward(self, x):
        # Define the forward method
        ____
Edit and Run Code