Generator
A GAN generator takes a random noise vector as input and produces a generated image. To make its architecture more reusable, you will pass both input and output shapes as parameters to the model. This way, you can use the same model with different sizes of input noise and images of varying shapes.
You will find torch.nn
imported already imported for you as nn
. You can also access a custom gen_block()
function which returns a block of: linear layer, batch norm, and ReLU activation. You will use it as a building block for the generator.
def gen_block(in_dim, out_dim):
return nn.Sequential(
nn.Linear(in_dim, out_dim),
nn.BatchNorm1d(out_dim),
nn.ReLU(inplace=True)
)
This exercise is part of the course
Deep Learning for Images with PyTorch
Exercise instructions
- Define
self.generator
as a sequential model. - After the last
gen_block
, add a linear layer with the appropriate input size and the output size ofout_dim
. - Add a sigmoid activation after the linear layer.
- In the
forward()
method, pass the model's input throughself.generator
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class Generator(nn.Module):
def __init__(self, in_dim, out_dim):
super(Generator, self).__init__()
# Define generator block
self.generator = ____(
gen_block(in_dim, 256),
gen_block(256, 512),
gen_block(512, 1024),
# Add linear layer
____
# Add activation
____
)
def forward(self, x):
# Pass input through generator
return ____