Implement the discriminator and non-saturating generator losses from a GAN forward pass.
LD=−N1i=1∑N[logpireal+log(1−pifake)] LG=−N1i=1∑NlogpifakeHere, N is the batch size, pireal is the discriminator probability for real sample i, and pifake is its probability for fake sample i. Clip probabilities to [10−8,1−10−8] before taking logarithms. Return a dictionary containing discriminator_loss and generator_loss as Python floats.
Examples
Input: real_probs = [0.9,0.95], fake_probs = [0.1,0.05]
Output: {"discriminator_loss":0.156654,"generator_loss":2.649159}
Explanation: The discriminator loss rewards high real probabilities and low fake probabilities, while the non-saturating generator loss rewards high fake probabilities.
Input: real_probs = [0.5,0.5], fake_probs = [0.5,0.5]
Output: {"discriminator_loss":1.386294,"generator_loss":0.693147}
Input: real_probs = [0.8,0.6,0.9], fake_probs = [0.2,0.4,0.1]
Output: {"discriminator_loss":0.559553,"generator_loss":1.609438}
Hint 1
Use NumPy clipping before applying the logarithm.
Hint 2
Average each loss over the batch dimension.
Requirements
- Use NumPy.
- Compute the discriminator binary cross-entropy over real and fake probabilities.
- Compute the non-saturating generator loss from fake probabilities.
- Clip probabilities with ϵ=10−8 before logarithms.
- Return exactly the two documented float values.
Constraints
- Both probability arrays have shape (N) and the same nonzero length.
- Every probability lies in [0,1].
- 1≤N≤8.
All steps in GAN
- Generator
- Discriminator
- GAN Loss Functions
- Training Loop
- Mode Collapse
- Complete GAN
Related practice
Accepts: array
Accepts: array
GAN Loss Functions
Implement the discriminator and non-saturating generator losses from a GAN forward pass.
LD=−N1i=1∑N[logpireal+log(1−pifake)] LG=−N1i=1∑NlogpifakeHere, N is the batch size, pireal is the discriminator probability for real sample i, and pifake is its probability for fake sample i. Clip probabilities to [10−8,1−10−8] before taking logarithms. Return a dictionary containing discriminator_loss and generator_loss as Python floats.
Examples
Input: real_probs = [0.9,0.95], fake_probs = [0.1,0.05]
Output: {"discriminator_loss":0.156654,"generator_loss":2.649159}
Explanation: The discriminator loss rewards high real probabilities and low fake probabilities, while the non-saturating generator loss rewards high fake probabilities.
Input: real_probs = [0.5,0.5], fake_probs = [0.5,0.5]
Output: {"discriminator_loss":1.386294,"generator_loss":0.693147}
Input: real_probs = [0.8,0.6,0.9], fake_probs = [0.2,0.4,0.1]
Output: {"discriminator_loss":0.559553,"generator_loss":1.609438}
Hint 1
Use NumPy clipping before applying the logarithm.
Hint 2
Average each loss over the batch dimension.
Requirements
- Use NumPy.
- Compute the discriminator binary cross-entropy over real and fake probabilities.
- Compute the non-saturating generator loss from fake probabilities.
- Clip probabilities with ϵ=10−8 before logarithms.
- Return exactly the two documented float values.
Constraints
- Both probability arrays have shape (N) and the same nonzero length.
- Every probability lies in [0,1].
- 1≤N≤8.
All steps in GAN
- Generator
- Discriminator
- GAN Loss Functions
- Training Loop
- Mode Collapse
- Complete GAN
Related practice
Accepts: array
Accepts: array