In models with a generator and discriminator, like GANs, the main goal is to make the generator create data that looks real, and the discriminator to tell real from fake. The key metric is loss for both parts. The generator's loss shows how well it fools the discriminator. The discriminator's loss shows how well it spots fakes. We also look at Inception Score or FID to measure how real the generated data looks overall.
Generator and discriminator in PyTorch - Model Metrics & Evaluation
| Predicted Real | Predicted Fake |
---------------------------------------
Real Data | TP | FN |
Fake Data | FP | TN |
TP = Discriminator correctly says real data is real
TN = Discriminator correctly says fake data is fake
FP = Discriminator wrongly says fake data is real
FN = Discriminator wrongly says real data is fake
Precision = TP / (TP + FP) measures how many predicted real are truly real.
Recall = TP / (TP + FN) measures how many real data are correctly found.
These help understand discriminator quality.
For the discriminator, high precision means it rarely mistakes fake data as real. High recall means it finds most real data correctly. If precision is too high but recall low, it may miss some real data. If recall is high but precision low, it may accept many fakes as real.
For the generator, the goal is to reduce discriminator's ability to tell fake from real, so its loss improves when discriminator's precision and recall drop.
Example: In art generation, a generator with low recall on real art means it misses many styles. A discriminator with low precision may accept poor fakes as real.
- Good Discriminator: Balanced precision and recall around 0.8-0.9, showing it can spot fakes and recognize real data well.
- Bad Discriminator: Precision or recall below 0.5 means it guesses poorly, hurting training.
- Good Generator: Generator loss decreases steadily, and FID score lowers (closer to 0), meaning generated data looks more real.
- Bad Generator: Generator loss stays high or oscillates, FID score high, meaning poor quality fake data.
- Mode Collapse: Generator produces limited variety, fooling discriminator but failing diversity. Metrics may look good but output is poor.
- Overfitting Discriminator: Discriminator becomes too strong, generator can't learn. Losses become unstable.
- Ignoring Diversity Metrics: Only tracking loss misses if generator creates varied outputs.
- Data Leakage: Using test data in training can inflate metrics falsely.
Your GAN model has a discriminator accuracy of 98% but the generator's FID score is very high, and generated images look very similar. Is this good?
Answer: No. The discriminator is too strong and the generator is not learning well. The high discriminator accuracy means it easily spots fakes. The high FID and similar images show mode collapse. You need to balance training and monitor diversity.
Practice
generator in a GAN (Generative Adversarial Network)?Solution
Step 1: Understand the generator's purpose
The generator's job is to produce new data that looks like the real data by starting from random noise.Step 2: Differentiate from discriminator's role
The discriminator's role is to tell real data from fake, not to create data.Final Answer:
To create fake data from random noise -> Option CQuick Check:
Generator creates fake data = B [OK]
- Confusing generator with discriminator
- Thinking generator classifies data
- Assuming generator preprocesses data
Solution
Step 1: Check PyTorch model class structure
A PyTorch model must inherit from nn.Module and call super().__init__() in __init__.Step 2: Verify forward method and layers
class Discriminator(nn.Module): def __init__(self): super().__init__() self.fc = nn.Linear(784, 1) def forward(self, x): return torch.sigmoid(self.fc(x)) correctly defines the class with inheritance, super call, layer, and forward method.Final Answer:
Correct PyTorch discriminator class with inheritance and super call -> Option DQuick Check:
Proper nn.Module subclass = A [OK]
- Forgetting to inherit nn.Module
- Missing super().__init__() call
- Defining forward outside class
fake_output?
noise = torch.randn(16, 100) generator = Generator() fake_data = generator(noise) discriminator = Discriminator() fake_output = discriminator(fake_data)Assume generator outputs shape (16, 784) and discriminator outputs shape (batch_size, 1).
Solution
Step 1: Understand generator output shape
The generator outputs fake data with shape (16, 784), meaning batch size 16 and 784 features.Step 2: Understand discriminator input and output shape
The discriminator takes input of shape (16, 784) and outputs shape (16, 1), one score per sample.Final Answer:
torch.Size([16, 1]) -> Option AQuick Check:
Discriminator output shape = (batch_size, 1) = A [OK]
- Confusing input and output shapes
- Mixing batch size and feature dimensions
- Assuming output shape matches noise size
optimizer.zero_grad() output = discriminator(fake_data) loss = criterion(output, real_labels) loss.backward() optimizer.step()Assuming
fake_data is generated by the generator and real_labels are labels for real data.Solution
Step 1: Check input and labels for discriminator
The discriminator should compare fake_data with fake labels, not real_labels.Step 2: Identify label mismatch in loss calculation
Using real_labels with fake_data causes wrong loss and training signals.Final Answer:
Using real_labels for fake_data loss calculation -> Option AQuick Check:
Fake data needs fake labels = C [OK]
- Mixing real and fake labels
- Skipping zero_grad() call
- Forgetting loss.backward()
Solution
Step 1: Understand mode collapse and training stability
Mode collapse happens when generator produces limited outputs; discriminator tricks help prevent it.Step 2: Identify label smoothing effect
Label smoothing softens discriminator targets, preventing it from becoming too confident and stabilizing training.Final Answer:
Use label smoothing on discriminator targets -> Option BQuick Check:
Label smoothing stabilizes discriminator = D [OK]
- Ignoring discriminator role in stability
- Changing only generator learning rate
- Removing discriminator breaks GAN
Ready to prove it?
Pass the test and this pattern moves unknown
