Convolutional Discriminator
With the DCGAN's generator ready, the last step before you can proceed to training it is to define the convolutional discriminator.
torch.nn
is imported for you under its usual alias. To build the convolutional discriminator, you will use a custom gc_disc_block()
function which returns a block of a convolution followed by a batch norm and the leaky ReLU activation. You can inspect dc_disc_block()
's definition below.
def dc_disc_block(in_dim, out_dim, kernel_size, stride):
return nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size, stride=stride),
nn.BatchNorm2d(out_dim),
nn.LeakyReLU(0.2),
)
This exercise is part of the course
Deep Learning for Images with PyTorch
Exercise instructions
- Add the first discriminator block using the custom
dc_disc_block()
function with3
input feature maps and512
output feature maps. - Add the convolutional layer with the output size of
1
. - In the
forward()
method, pass the input through the sequential block you defined in__init__()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class DCDiscriminator(nn.Module):
def __init__(self, kernel_size=4, stride=2):
super(DCDiscriminator, self).__init__()
self.disc = nn.Sequential(
# Add first discriminator block
dc_disc_block(3, 512, kernel_size, stride),
dc_disc_block(512, 1024, kernel_size, stride),
# Add a convolution
nn.Conv2d(1024, 1, kernel_size, stride=stride),
)
def forward(self, x):
# Pass input through sequential block
x = ____
return x.view(len(x), -1)