Bird
Raised Fist0
TensorFlowml~20 mins

GAN concept (generator and discriminator) in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GAN Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Role of Generator in a GAN
In a Generative Adversarial Network (GAN), what is the primary role of the generator?
ATo create fake data samples that resemble the real data
BTo classify data samples as real or fake
CTo optimize the discriminator's loss function
DTo preprocess the input data before training
Attempts:
2 left
💡 Hint
Think about which part of the GAN tries to fool the other part.
🧠 Conceptual
intermediate
1:30remaining
Role of Discriminator in a GAN
What is the main function of the discriminator in a GAN?
ATo generate new data samples
BTo distinguish between real and fake data samples
CTo reduce the generator's learning rate
DTo create noise input for the generator
Attempts:
2 left
💡 Hint
This part acts like a judge deciding if data is real or fake.
❓ Predict Output
advanced
2:00remaining
Output Shape of Generator Model
Given the following TensorFlow generator model code, what is the shape of the output tensor?
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers

def make_generator_model():
    model = tf.keras.Sequential()
    model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Reshape((7, 7, 256)))
    model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))

    return model

generator = make_generator_model()
output = generator(tf.random.normal([1, 100]))
output_shape = output.shape
A(100, 28, 28, 1)
B(1, 14, 14, 1)
C(1, 7, 7, 256)
D(1, 28, 28, 1)
Attempts:
2 left
💡 Hint
Check the effect of Conv2DTranspose layers with strides on spatial dimensions.
❓ Metrics
advanced
1:30remaining
Interpreting GAN Loss Values
During GAN training, the discriminator loss is very low (close to 0), and the generator loss is very high. What does this indicate about the training?
AThe discriminator is too strong and easily detects fake samples
BThe generator is producing very realistic samples
CBoth models are balanced and training well
DThe generator is overfitting the training data
Attempts:
2 left
💡 Hint
Think about what a low discriminator loss means in terms of classification accuracy.
🔧 Debug
expert
2:30remaining
Identifying Error in GAN Training Loop
Consider this simplified GAN training loop snippet in TensorFlow. What error will occur when running this code?
TensorFlow
import tensorflow as tf

def train_step(generator, discriminator, real_images, batch_size, noise_dim):
    noise = tf.random.normal([batch_size, noise_dim])
    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        generated_images = generator(noise, training=True)

        real_output = discriminator(real_images)
        fake_output = discriminator(generated_images)

        gen_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(fake_output), fake_output)
        disc_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(real_output), real_output) + \
                    tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.zeros_like(fake_output), fake_output)

    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    generator.optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator.optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

batch_size = 32
noise_dim = 100
real_images = tf.random.normal([batch_size, 28, 28, 1])

# Assume generator and discriminator are defined and compiled with optimizers
train_step(generator, discriminator, real_images, batch_size, noise_dim)
AValueError due to shape mismatch in loss calculation
BTypeError because real_images has wrong dtype
CAttributeError because generator.optimizer is None
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check if the generator and discriminator have optimizers assigned.

Practice

(1/5)
1.

What is the main role of the generator in a GAN?

easy
A. To classify data as real or fake
B. To create fake data that looks real
C. To label data for supervised learning
D. To reduce the size of the dataset

Solution

  1. Step 1: Understand the generator's purpose

    The generator's job is to produce new data that mimics real data.
  2. Step 2: Differentiate from discriminator

    The discriminator checks data authenticity, but the generator creates fake data.
  3. Final Answer:

    To create fake data that looks real -> Option B
  4. Quick Check:

    Generator = create fake data [OK]
Hint: Generator makes fake data, discriminator checks it [OK]
Common Mistakes:
  • Confusing generator with discriminator
  • Thinking generator labels data
  • Assuming generator reduces data size
2.

Which of the following is the correct way to define a simple generator model in TensorFlow Keras?

def make_generator_model():
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(7*7*256, use_bias=False, input_shape=(100,)),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.LeakyReLU(),
        tf.keras.layers.Reshape((7, 7, 256)),
        tf.keras.layers.Conv2DTranspose(128, 5, strides=1, padding='same', use_bias=False),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.LeakyReLU(),
        tf.keras.layers.Conv2DTranspose(1, 5, strides=2, padding='same', activation='tanh')
    ])
    return model
easy
A. The model is missing an input layer
B. The activation function 'tanh' is incorrect for generator output
C. The code correctly defines a generator model
D. The Dense layer should have bias enabled

Solution

  1. Step 1: Check model layers and input

    The model starts with a Dense layer with input_shape=(100,), which is correct for noise input.
  2. Step 2: Verify output activation and structure

    The last Conv2DTranspose uses 'tanh' activation, common for generator output scaled between -1 and 1.
  3. Final Answer:

    The code correctly defines a generator model -> Option C
  4. Quick Check:

    Generator model code is correct [OK]
Hint: Generator output often uses tanh activation [OK]
Common Mistakes:
  • Forgetting input_shape in first layer
  • Using wrong activation for output layer
  • Enabling bias unnecessarily in Dense layer
3.

Given the following code snippet for a discriminator model, what will be the output shape after the last Conv2D layer?

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(64, 5, strides=2, padding='same', input_shape=(28, 28, 1)),
    tf.keras.layers.LeakyReLU(),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Conv2D(128, 5, strides=2, padding='same'),
    tf.keras.layers.LeakyReLU(),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1)
])
medium
A. (7, 7, 128)
B. (14, 14, 128)
C. (28, 28, 64)
D. (1, 1, 1)

Solution

  1. Step 1: Calculate output after first Conv2D

    Input is (28,28,1), stride=2, padding='same' halves spatial dims: (14,14,64).
  2. Step 2: Calculate output after second Conv2D

    Again stride=2, padding='same' halves spatial dims: (7,7,128).
  3. Final Answer:

    (7, 7, 128) -> Option A
  4. Quick Check:

    Conv2D output shape = (7,7,128) [OK]
Hint: Stride 2 halves spatial dimensions with 'same' padding [OK]
Common Mistakes:
  • Forgetting padding='same' keeps output size rounded up
  • Misapplying stride effect on dimensions
  • Confusing number of filters with spatial size
4.

Identify the error in this GAN training step code snippet:

noise = tf.random.normal([batch_size, noise_dim])
generated_images = generator(noise, training=True)
real_output = discriminator(real_images, training=True)
fake_output = discriminator(generated_images, training=False)

loss = generator_loss(fake_output) + discriminator_loss(real_output, fake_output)
medium
A. Discriminator should be in training mode for fake images
B. Generator should not use training=True during generation
C. Noise shape is incorrect for generator input
D. Loss calculation should not combine generator and discriminator losses

Solution

  1. Step 1: Check discriminator training mode

    Discriminator must be in training mode for both real and fake images to update weights properly.
  2. Step 2: Identify incorrect training flag

    Here, discriminator(fake images) uses training=False, which disables training behavior like dropout.
  3. Final Answer:

    Discriminator should be in training mode for fake images -> Option A
  4. Quick Check:

    Discriminator training mode = True for all inputs [OK]
Hint: Discriminator trains on both real and fake data [OK]
Common Mistakes:
  • Setting discriminator training=False on fake data
  • Confusing generator training flag
  • Incorrect noise shape assumptions
5.

You want to improve a GAN to generate clearer images. Which combined approach is best?

  1. Increase generator depth and add batch normalization
  2. Use a weaker discriminator to avoid overpowering generator
  3. Train discriminator more times per generator update
  4. Remove dropout layers from discriminator
hard
A. Options 2 and 4 together are best
B. Options 1 and 2 together are best
C. Only option 1 is best
D. Options 1 and 3 together are best

Solution

  1. Step 1: Evaluate option 1

    Increasing generator depth and adding batch normalization helps create clearer, stable images.
  2. Step 2: Evaluate option 3

    Training discriminator more per generator update helps discriminator learn better, pushing generator to improve.
  3. Step 3: Assess options 2 and 4

    Weaker discriminator or removing dropout can reduce training quality and cause poor results.
  4. Final Answer:

    Options 1 and 3 together are best -> Option D
  5. Quick Check:

    Improve generator + stronger discriminator training = better images [OK]
Hint: Stronger generator + more discriminator training = clearer images [OK]
Common Mistakes:
  • Weakening discriminator hurts GAN balance
  • Removing dropout may cause overfitting
  • Ignoring batch normalization benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard