Problems
Loading...
Step 6 of 6
Complete GAN
Hard

Implement a deterministic GAN forward pass that connects the generator, discriminator, and adversarial losses. First generate samples with

X^=tanh⁡(ZWG+bG)\widehat{X} = \tanh(ZW_G+b_G)

Then compute discriminator probabilities for real and generated samples:

preal=σ(XrealWD)p^{\mathrm{real}} = \sigma(X^{\mathrm{real}}W_D) pfake=σ(X^WD)p^{\mathrm{fake}} = \sigma(\widehat{X}W_D)

Use these probabilities to compute the discriminator and non-saturating generator losses defined in the GAN loss problem, clipping only before logarithms. Here, ZZ contains noise vectors, WGW_G and bGb_G are generator parameters, WDW_D contains discriminator weights, and both batches contain BB samples. Return a dictionary containing generated_samples, real_probabilities, fake_probabilities, discriminator_loss, and generator_loss. All returned arrays must be float64.

Loading visualization...

Examples

Input: z = [[1,-0.5],[0.3,0.8]], real_data = [[0.7,-0.2,0.4],[0.1,0.9,-0.3]], G_W = [[0.3,-0.1,0.5],[0.2,0.4,-0.3]], G_b = [0.1,0,-0.1], D_W = [[0.5],[-0.2],[0.3]]

Output: {"generated_samples":[[0.291313,-0.291313,0.50052],[0.336376,0.282135,-0.187746]],"real_probabilities":[[0.624806],[0.445221]],"fake_probabilities":[[0.587605],[0.513856]],"discriminator_loss":1.443261,"generator_loss":0.598756}

Explanation: The generator produces a fake batch, the shared discriminator scores both batches, and the two losses use those exact scores.

Input: z = [[-0.2,0.6,0.1]], real_data = [[0.4,-0.5]], G_W = [[0.2,-0.4],[0.5,0.3],[-0.1,0.6]], G_b = [0.05,-0.05], D_W = [[0.7],[-0.2]]

Output: {"generated_samples":[[0.291313,0.263625]],"real_probabilities":[[0.593873]],"fake_probabilities":[[0.537727]],"discriminator_loss":1.292688,"generator_loss":0.620405}

Input: z = [[0,0],[0,0]], real_data = [[1,-1],[-1,1]], G_W = [[0,0],[0,0]], G_b = [0,0], D_W = [[0.4],[-0.4]]

Output: {"generated_samples":[[0,0],[0,0]],"real_probabilities":[[0.689974],[0.310026]],"fake_probabilities":[[0.5],[0.5]],"discriminator_loss":1.464248,"generator_loss":0.693147}

Hint 1

Reuse the generated batch when computing fake probabilities and losses.

Hint 2

Keep the probability arrays two-dimensional with shape (B, 1).

Hint 3

Clip probabilities only for the logarithms.

Requirements

  • Use NumPy.
  • Apply the generator affine transformation and tanh activation.
  • Evaluate the same discriminator on real and generated samples.
  • Compute both adversarial losses from those probabilities.
  • Return exactly the five documented outputs.

Constraints

  • z has shape (B,N)(B, N) and real_data has shape (B,D)(B, D).
  • G_W has shape (N,D)(N, D) and G_b has shape (D)(D).
  • D_W has shape (D,1)(D, 1).
  • 1≤B≤41 \le B \le 4, 1≤N≤41 \le N \le 4, and 1≤D≤51 \le D \le 5.
  • All inputs contain finite real values.