Implement one gradient-descent update of a linear GAN discriminator while holding the real and fake samples fixed. The discriminator probabilities are
preal=σ(XrealWD) pfake=σ(XfakeWD)The discriminator loss and its weight gradient are
LD=−N1i=1∑N[logpireal+log(1−pifake)] ∇WDLD=N(Xreal)T(preal−1)+(Xfake)TpfakeUpdate the weights with WD′=WD−η∇WDLD. Here, N is the shared batch size and η 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.
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).
- D_W has shape (D,1).
- 1≤N≤6 and 1≤D≤5.
- learning_rate is a positive finite float.
- All arrays contain finite real values.
All steps in GAN
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Training Loop
Implement one gradient-descent update of a linear GAN discriminator while holding the real and fake samples fixed. The discriminator probabilities are
preal=σ(XrealWD) pfake=σ(XfakeWD)The discriminator loss and its weight gradient are
LD=−N1i=1∑N[logpireal+log(1−pifake)] ∇WDLD=N(Xreal)T(preal−1)+(Xfake)TpfakeUpdate the weights with WD′=WD−η∇WDLD. Here, N is the shared batch size and η 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.
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).
- D_W has shape (D,1).
- 1≤N≤6 and 1≤D≤5.
- learning_rate is a positive finite float.
- All arrays contain finite real values.
All steps in GAN
Accepts: array
Accepts: array
Accepts: array
Accepts: number