Quest 2 • Lesson 2
🎨 Generative AI (GANs)
Learn how Generative Adversarial Networks create new data – from images to music – by pitting two neural networks against each other.
Generative Adversarial Networks (GANs) consist of two neural networks: a Generator that creates fake data, and a Discriminator that tries to tell real from fake. They compete, improving each other over time.
🧠 How GANs Work
- Generator – takes random noise (latent vector) and generates fake samples.
- Discriminator – classifies samples as real or fake.
- Adversarial training – generator tries to fool discriminator, discriminator tries to catch fakes.
- Loss functions – generator minimizes discriminator's ability to detect fakes; discriminator minimizes classification error.
- Applications – image generation, style transfer, text-to-image, super-resolution.
🚀 Live Demo: Train a Simple GAN
This GAN learns to generate circles from random noise. Click "Train GAN" to start. Watch the fake images become more realistic.
Real (target)
Generated (fake)
📘 How the demo works
generator = Sequential([
Dense(128, activation='relu', input_shape=[10]),
Dense(100, activation='sigmoid'),
])
# Discriminator: image → real/fake
discriminator = Sequential([
Dense(64, activation='relu', input_shape=[100]),
Dense(1, activation='sigmoid'),
])
# Adversarial training loop
for epoch in range(num_epochs):
noise = random.normal(0, 1, (batch_size, 10))
fake_images = generator.predict(noise)
real_images = get_real_images()
disc_loss = discriminator.train_on_batch(real_images, ones) + discriminator.train_on_batch(fake_images, zeros)
gen_loss = gan.train_on_batch(noise, ones)
✨ Challenge: Improve the GAN
Try modifying the GAN architecture:
- Increase the latent dimension (e.g., 20 instead of 10).
- Add more layers to generator/discriminator.
- Train for more epochs.
const generator = tf.sequential();
generator.add(tf.layers.dense({units: 64, activation: 'relu', inputShape: [20]}));
generator.add(tf.layers.dense({units: 128, activation: 'relu'}));
generator.add(tf.layers.dense({units: 100, activation: 'sigmoid'}));
➡️ Next Lesson
Lesson 2.3: AI in Production – deploy your AI models.
Continue to Lesson 2.3 →(Coming soon – check back or buy Pro Pack for early access)
❤️ Support Free Education
This course is 100% free. If it helps you, consider buying me a coffee.
☕ Buy Me a Coffee