One Round, Both Players

Adversarial GenerationHard+30 XP+30

One Round, Both Players

A GAN training step has two halves. First the discriminator is updated on a batch of real examples and a batch of fakes. Then the generator is scored against the discriminator as it stands after that update. Here you will write both losses and the discriminator's update, using the smallest discriminator there is.

The discriminator. The data are single numbers. The discriminator has two parameters, www and bbb. It gives an example xxx a raw score (a logit), then squashes that with a sigmoid into its probability that xxx is real:

s=wx+b,D(x)=σ(s)=11+e−ss = wx + b, \qquad D(x) = \sigma(s) = \frac{1}{1 + e^{-s}}s=wx+b,D(x)=σ(s)=1+e−s1​

Discriminator loss. Binary cross-entropy, with label 111 for every real example and label 000 for every fake. Each batch is averaged on its own, and the two averages are added:

LD=1nreal∑real x−log⁡D(x)  +  1nfake∑fake x−log⁡(1−D(x))L_D = \frac{1}{n_{\text{real}}}\sum_{\text{real } x} -\log D(x) \;+\; \frac{1}{n_{\text{fake}}}\sum_{\text{fake } x} -\log\big(1 - D(x)\big)LD​=nreal​1​real x∑​−logD(x)+nfake​1​fake x∑​−log(1−D(x))

Discriminator update. One step of gradient descent on www and bbb, with learning rate η\etaη. For a single example with score sss and label yyy, the slope of its cross-entropy with respect to sss is σ(s)−y\sigma(s) - yσ(s)−y. Because s=wx+bs = wx + bs=wx+b, that example's slope with respect to www is (σ(s)−y) x(\sigma(s) - y)\,x(σ(s)−y)x, and with respect to bbb it is σ(s)−y\sigma(s) - yσ(s)−y. The gradient of LDL_DLD​ combines these per-example slopes in exactly the way LDL_DLD​ combines the per-example losses.

Generator loss. The generator wants its fakes to be called real. It does not minimise log⁡(1−D)\log(1 - D)log(1−D), which goes flat exactly when the discriminator confidently rejects a fake. It minimises this instead:

LG=1nfake∑fake x−log⁡D(x)L_G = \frac{1}{n_{\text{fake}}}\sum_{\text{fake } x} -\log D(x)LG​=nfake​1​fake x∑​−logD(x)

LGL_GLG​ is measured with the updated www and bbb. The generator's own update is not part of this task: the fakes are handed to you as numbers.

Task: write gan_step(w, b, real, fake, lr). Return a tuple (d_loss, new_w, new_b, g_loss): LDL_DLD​ at the starting parameters, the two parameters after one update, and LGL_GLG​ at the updated parameters, each rounded to 4 decimal places.

Large scores. A confident discriminator can produce scores like ±50\pm 50±50, and your losses must still come out finite. These two identities never take the logarithm of a probability that has rounded to 0:

−log⁡σ(s)=log⁡(1+e−s),−log⁡(1−σ(s))=log⁡(1+es)-\log \sigma(s) = \log\left(1 + e^{-s}\right), \qquad -\log\big(1 - \sigma(s)\big) = \log\left(1 + e^{s}\right)−logσ(s)=log(1+e−s),−log(1−σ(s))=log(1+es)
DShow the discriminator real examples, labelled real.
DShow it generated examples, labelled fake. Update it.
GGenerate a batch and ask the discriminator to judge it.
GUpdate the generator to make that judgement come out wrong.

Only one network is updated at a time. The other is held still, so each has a fixed opponent for that step.

Examples

w = 1.0
b = 0.0
real = [2.0, 3.0]
fake = [-1.0, 0.5]
lr = 0.5
returns (0.7314, 1.0846, -0.1812, 1.0215)
w = 0.5
b = -0.2
real = [1.5, 2.5, 2.0]
fake = [0.0, 1.0]
lr = 1.0
returns (1.1018, 0.8184, -0.4006, 0.7096)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run