PIXELBANKv9.1.0
Menu

Compute the discriminator loss for a Generative Adversarial Network.

The discriminator tries to maximize: LD=−1m∑i=1m[log⁡D(xi)+log⁡(1−D(G(zi)))]L_D = -\frac{1}{m}\sum_{i=1}^{m}[\log D(x_i) + \log(1 - D(G(z_i)))]

Given the discriminator's output for real data D(x)D(x) and for fake data D(G(z))D(G(z)) (both are probabilities in (0,1)(0, 1)), compute the discriminator loss.

Clip probabilities to [ϵ,1−ϵ][\epsilon, 1-\epsilon] where ϵ=10−7\epsilon = 10^{-7} to avoid log(0).

Return the loss rounded to 4 decimal places. Note: the loss is a positive number (we negate the objective).

Example:

Input:
d_real = [0.9, 0.8]
d_fake = [0.1, 0.2]
Output:
0.3285
Reasoning:
  • First, we clip the discriminator's output for real and fake data to [ϵ,1−ϵ][\epsilon, 1-\epsilon] where ϵ=10−7\epsilon = 10^{-7}: d_real=[0.9,0.8]d\_real = [0.9, 0.8] and d_fake=[0.1,0.2]d\_fake = [0.1, 0.2] remain the same since they are already within the range.
  • Then, we calculate the log of the clipped probabilities: log⁡D(x)=[log⁡0.9,log⁡0.8]\log D(x) = [\log 0.9, \log 0.8] and log⁡(1−D(G(z)))=[log⁡(1−0.1),log⁡(1−0.2)]=[log⁡0.9,log⁡0.8]\log (1 - D(G(z))) = [\log (1 - 0.1), \log (1 - 0.2)] = [\log 0.9, \log 0.8].
  • Next, we compute the discriminator loss using the formula: LD=−1m∑i=1m[log⁡D(xi)+log⁡(1−D(G(zi)))]=−12[log⁡0.9+log⁡0.9+log⁡0.8+log⁡0.8]L_D = -\frac{1}{m}\sum_{i=1}^{m}[\log D(x_i) + \log(1 - D(G(z_i)))] = -\frac{1}{2}[\log 0.9 + \log 0.9 + \log 0.8 + \log 0.8].
  • The final output is the negation of the calculated loss, rounded to 4 decimal places: LD≈−12[−0.1054−0.1054−0.2231−0.2231]≈0.3285L_D \approx -\frac{1}{2}[-0.1054 - 0.1054 - 0.2231 - 0.2231] \approx 0.3285.

Constraints:

  • d_real: list of discriminator outputs for real data (probabilities)
  • d_fake: list of discriminator outputs for fake data (probabilities)
  • Clip to [1e-7, 1-1e-7] before taking log
  • Return the discriminator loss (positive) rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.