Problems
Loading...
Step 4 of 6
Training Loop
Hard

Implement one gradient-descent update of a linear GAN discriminator while holding the real and fake samples fixed. The discriminator probabilities are

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

The discriminator loss and its weight gradient are

LD=−1N∑i=1N[log⁡pireal+log⁡(1−pifake)]L_D = -\frac{1}{N}\sum_{i=1}^{N}\left[\log p_i^{\mathrm{real}} + \log\left(1-p_i^{\mathrm{fake}}\right)\right] ∇WDLD=(Xreal)T(preal−1)+(Xfake)TpfakeN\nabla_{W_D}L_D = \frac{(X^{\mathrm{real}})^{\mathsf T}(p^{\mathrm{real}}-1) + (X^{\mathrm{fake}})^{\mathsf T}p^{\mathrm{fake}}}{N}

Update the weights with WD′=WD−η∇WDLDW_D' = W_D - \eta\nabla_{W_D}L_D. Here, NN is the shared batch size and η\eta is learning_rate. Clip probabilities only when evaluating logarithms. Return a dictionary containing new_discriminator_weights as a float64 NumPy array and discriminator_loss as a Python float computed before the update.

Loading visualization...

Examples

Input: real_data = [[1,0.5,-0.3],[0.8,-0.2,0.6]], fake_data = [[-0.5,0.3,0.1],[0.2,-0.8,0.4]], D_W = [[0.5],[-0.3],[0.2]], learning_rate = 0.1

Output: {"new_discriminator_weights":[[0.54024],[-0.27506],[0.190175]],"discriminator_loss":1.237087}

Explanation: The real examples contribute probability minus one to the gradient, while fake examples contribute their predicted probability.

Input: real_data = [[2,1],[1.5,0.8]], fake_data = [[-2,-1],[-1.5,-0.8]], D_W = [[1],[0.5]], learning_rate = 0.05

Output: {"new_discriminator_weights":[[1.017344],[0.508997]],"discriminator_loss":0.218276}

Input: real_data = [[0.3,-0.7]], fake_data = [[0.5,0.2]], D_W = [[0.4],[-0.6]], learning_rate = 0.2

Output: {"new_discriminator_weights":[[0.370092],[-0.672346]],"discriminator_loss":1.19311}

Hint 1

Compute the real and fake residual columns before multiplying by the transposed data matrices.

Hint 2

Divide the combined gradient by the shared batch size before the update.

Requirements

  • Use NumPy.
  • Compute real and fake probabilities using the supplied discriminator weights.
  • Compute the analytical discriminator gradient over both batches.
  • Apply one gradient-descent update.
  • Return exactly the documented updated weights and pre-update loss.

Constraints

  • real_data and fake_data both have shape (N,D)(N, D).
  • D_W has shape (D,1)(D, 1).
  • 1≤N≤61 \le N \le 6 and 1≤D≤51 \le D \le 5.
  • learning_rate is a positive finite float.
  • All arrays contain finite real values.