A GAN generator is the data-producing half of a generative adversarial network. It takes randomized latent-space inputs with noise and turns them into synthetic samples, such as images. Its counterpart, the discriminator, judges whether samples appear real or fake; the generator improves by using that feedback to make outputs that better fool the discriminator. section 9.1.1 section 9.6
For image generation, a practical generator can resemble an autoencoder decoder. A latent vector first feeds a dense layer, is reshaped into a small spatial feature map, then passes through upsampling and convolution layers until it reaches the target image channels and size. Batch normalization and ReLU activations appear between convolution stages, with tanh applied at the output. section 9.1.2
Listing 9.3 EDL_9_1_GAN.ipynb: Building the generator
def build_generator():
model = Sequential()
model.add(Dense(128 * cs * cs, activation="relu",
input_dim=latent_dim)) #1
model.add(Reshape((cs, cs, 128))) #2
model.add(UpSampling2D()) #3
model.add(Conv2D(128, kernel_size=3, padding="same"))
model.add(BatchNormalization(momentum=0.8))
model.add(Activation("relu"))
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=3, padding="same"))
model.add(BatchNormalization(momentum=0.8))
model.add(Activation("relu"))
model.add(Conv2D(channels, kernel_size=3,
padding="same")) #4
model.add(Activation("tanh"))
model.summary()
noise = Input(shape=(latent_dim,)) #5
img = model(noise)
return Model(noise, img)
The listing builds a convolutional image generator: expand a latent vector, reshape it, repeatedly upsample and convolve, then expose noise as the model input. section 9.1.2
Train the generator as part of the adversarial loop:
- Sample noise vectors from the latent space and use the generator to predict fake images. section 9.1.2
- Train the discriminator on real images labeled valid and generated images labeled fake. section 9.1.2
- Freeze the discriminator in the combined GAN model, then train on noise labeled valid so generator updates are rewarded when generated images are judged real. section 9.1.2
The generator must improve at roughly the same pace as the discriminator. If the discriminator becomes too effective, too little useful loss reaches the generator and gradients can vanish. If the generator overwhelms the discriminator, the discriminator can collapse into near-random real/fake guesses. A generator may also mode-collapse, repeatedly producing little-varied output instead of diverse samples. section 9.2.1 section 9.2
Generating one digit class
To reduce training time, train on images from a single digit class, such as digit 5. Set a latent dimension of 100, generate image batches from normally sampled noise, and alternate discriminator updates with a combined-model update for the generator. Inspect generated outputs and losses over epochs rather than assuming training is stable. section 9.1.2 section 9.2.2











