Two Ways to Ask for a Better Fake

Adversarial GenerationHard+30 XP+30

Two Ways to Ask for a Better Fake

The generator never sees a real example. Everything it learns arrives as a derivative handed back through the discriminator — move your output this way and I would have been more convinced. So the size of that derivative is the size of the lesson.

Take the smallest possible judge. The generator emits one number xxx per fake; the discriminator's head turns it into a logit and squashes it:

z=wx+b,s=σ(z)=11+e−zz = w x + b, \qquad s = \sigma(z) = \frac{1}{1 + e^{-z}}z=wx+b,s=σ(z)=1+e−z1​

sss near 111 means "real", near 000 means "fake". There are two ways to write down what the generator wants, and they are not the same function:

formwhat the generator minimises
originallog⁡(1−s)\log(1 - s)log(1−s)
non-saturating−log⁡s-\log s−logs

Both are driven down by fooling the judge. What differs is the slope on the way there.

Task: write generator_feedback(outputs, weight, bias), where outputs is the list of numbers the generator produced for a batch of fakes and weight, bias are the head's www and bbb. Return four numbers, each rounded to 4 decimal places:

  1. the mean value of the original objective across the batch,
  2. the mean value of the non-saturating objective,
  3. the mean magnitude of the derivative of the original objective with respect to xxx,
  4. the mean magnitude of the derivative of the non-saturating objective with respect to xxx.
  • Differentiate with respect to xxx — the number the generator actually controls — not with respect to sss or zzz.
  • "Magnitude" means absolute value, taken per sample and then averaged.
  • Logarithms are natural. outputs is never empty, and weight may be negative.

A large loss and a usable gradient are not the same thing. One of these two forms can report a batch as almost nothing to fix at the precise moment the discriminator is rejecting every single sample in it.

Examples

outputs = [-0.3, 0.2, -0.8, 0.4]
weight = 1.0
bias = -0.5
returns [-0.4527, 1.0777, 0.3562, 0.6438]
outputs = [-0.3, 0.2, -0.8, 0.4]
weight = 4.0
bias = -2.0
returns [-0.2054, 2.7054, 0.6774, 3.3226]

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