What is GAN?
- Machine Learning course
- Expert
- Lesson 29/44
- 5 min
GAN is a generative model where a generator and discriminator compete.
What is a GAN?
A generative adversarial network (GAN, 2014) is a game between two networks:
- The generator (G): produces fake samples from random noise z.
- The discriminator (D): says, as a probability, whether a sample is real or fake.
# D maximizes, G minimizes:
# V(D, G) = E[log D(x)] + E[log(1 - D(G(z)))]As D improves, G is forced to produce more convincing samples. At the equilibrium G imitates the real distribution and D says 0.5 to every sample.
Loss functions
In practice both networks are trained with binary cross-entropy. For the generator, minimizing −log D(G(z)) is preferred over minimizing log(1 − D(G(z))) (the non-saturating loss): the gradient doesn't vanish at the start when D easily catches the fakes:
bce = lambda p, y: -(y * np.log(p) + (1 - y) * np.log(1 - p)).mean()
kayip_D = bce(D_gercek, 1) + bce(D_sahte, 0)
kayip_G = bce(D_sahte, 1) # "make D call the fake real"The training loop alternately updates D by one step and G by one step.
Practice GAN losses
Write the bce(p, y) function. For three cases print the D and G losses with round(..., 3) on one line: (1) D is very good (0.9 to real, 0.1 to fake), (2) equilibrium (0.5 to both), (3) G is fooling it (0.6 to real, 0.8 to fake).
Starter code
import numpy as np
durumlar = [(0.9, 0.1), (0.5, 0.5), (0.6, 0.8)]
# buraya yazSample solution
import numpy as np
durumlar = [(0.9, 0.1), (0.5, 0.5), (0.6, 0.8)]
def bce(p, y):
p = np.asarray(p, dtype=float)
return float(-(y * np.log(p) + (1 - y) * np.log(1 - p)).mean())
for g, s in durumlar:
kayip_D = bce([g], 1) + bce([s], 0)
kayip_G = bce([s], 1)
print(round(kayip_D, 3), round(kayip_G, 3))Expected output
0.211 2.303 1.386 0.693 2.12 0.223
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In a GAN that has reached equilibrium, what value does the discriminator give for every sample?
- 0.5 Correct answer
- 1
- 0
- Noise
Explanation: When real and fake are indistinguishable, the best guess is a coin flip.
2 In the generator's loss, evaluate the fakes with the "real" label.
kayip_G = bce(D_sahte, ____)
kayip_G = bce(D_sahte, 1)
Explanation: Non-saturating loss: G wants D to say 1 to the fake.
3 Match.
| Term | Meaning |
|---|---|
G |
Produces samples from noise |
D |
Real/fake probability |
kayip_D |
bce(real,1) + bce(fake,0) |
kayip_G |
bce(fake,1) |