Book a free call

What Is a Generative Adversarial Network (GAN)?

A generative adversarial network trains two neural networks against each other: one makes fakes, the other spots them. Here is how GANs work and where they are used.

Hertiv engineering teamReviewed by a senior engineer
7 min read
What Is a Generative Adversarial Network (GAN)?
On this page

Short answer

A generative adversarial network, or GAN, is a machine learning framework with two neural networks trained against each other: a generator that turns random noise into new data, and a discriminator that tries to tell generated data from real data. The two improve together until the generator's output is hard to tell from the training data. Ian Goodfellow and colleagues introduced GANs in a 2014 paper.

What is a generative adversarial network?

A generative adversarial network, or GAN, is a machine learning framework in which two neural networks are trained against each other. The generator turns random noise into new data, such as an image, while the discriminator tries to tell that generated data apart from real examples from the training set. The two networks improve together: as the discriminator gets better at spotting fakes, the generator gets better at making them.

The idea is a contest. A counterfeiter who only ever sees real banknotes cannot learn to copy them, and a detective who only ever sees real banknotes cannot learn to spot fakes. A GAN trains both at once, so each one pushes the other to improve.

Ian Goodfellow and colleagues at the University of Montreal introduced the framework in the 2014 paper "Generative Adversarial Nets", and it led image generation until diffusion models took over around 2021. For the wider family of models, see what generative AI is.

A GAN is a training framework, not a single fixed network. The generator and discriminator can be built from many kinds of layers, and the same contest idea works for images, audio, video and tabular data. What stays the same is the game: one network generates, the other judges, and both learn from the result.

How do the generator and discriminator work?

The two networks have opposite jobs, and the table below sets them out side by side.

Network Input Output Goal
Generator Random noise, called a latent vector A new sample, such as an image Fool the discriminator
Discriminator A real sample or a generated one A score: how likely it is real Spot the fakes

The generator never sees real data. It starts from random noise, and its only feedback is the discriminator's score: a high score means the fake passed, a low score means it was caught.

The discriminator sees both real samples from the training set and fakes from the generator, and it learns the boundary between them. At the ideal end of training, the generator's output matches the real data so well that the discriminator is right only half the time, which is no better than guessing.

The latent vector is the generator's raw material. Each random vector maps to a different output, so sampling a new vector produces a new image. Moving the vector in small steps changes the output in small steps, which is how a GAN can slide a face from one expression to another or change the style of a generated room.

How is a GAN trained, step by step?

Training a GAN is a loop, and the two networks take turns. The steps below describe one training round, repeated thousands of times.

  1. Show the discriminator real samples. Take a batch of real images from the training set and teach the discriminator to score them as real.
  2. Show it generated samples. Take a batch of fakes from the generator and teach the discriminator to score them as fake.
  3. Update the discriminator. Adjust its weights so it gets better at the real-versus-fake decision.
  4. Ask the generator for new fakes. Feed fresh random noise through the generator and ask the discriminator to score the results.
  5. Update the generator. Adjust the generator's weights so its fakes score higher, using the discriminator's feedback as the training signal.
  6. Repeat. Keep alternating until the discriminator can barely tell real from generated, or until the scores stop improving.

Early training has a known trap: when the fakes are easy to spot, the generator gets almost no useful feedback from the original objective, because the discriminator is so confident that its gradient nearly vanishes. Goodfellow's paper proposed a fix that is still widely used: train the generator to maximise the probability that the discriminator calls its samples real, which keeps the learning signal alive when the fakes are poor.

What are the main types of GAN?

The original GAN was a framework, and researchers built many variants on top of it. The table below lists the ones a reader is most likely to meet.

Variant What it adds Known for
DCGAN Convolutional layers in both networks Stable image generation, a common starting point
Conditional GAN A label or image fed to both networks Generating a chosen class, such as a digit
StyleGAN Style controls at each layer of the generator Realistic faces with control over features
CycleGAN Two generators that translate both ways Turning photos into paintings without paired data
Wasserstein GAN A different loss that measures distance between distributions More stable training and fewer collapses

Each variant fixes a weakness of the original. Conditional GANs add control, so the user can ask for a specific kind of output. StyleGAN adds fine control over the features of a generated face. Wasserstein GAN changes the training objective so the learning signal stays useful for longer, which makes training less likely to stall. The right choice depends on the data and on what the output must look like.

What are GANs used for?

GANs are used wherever new data that looks real is useful. Image generation is the best-known use: faces, rooms, products and art that never existed, made for design, advertising and games. A product team can generate a hundred pack shots in different colours without a photo shoot, and a game studio can fill a world with textures and characters. For the field behind them, see what artificial intelligence is.

Data augmentation is the quieter use. When real training data is scarce, a GAN can generate extra examples to train another model on, such as medical scans or rare defect photos. The generated data must be checked carefully, because a GAN can only repeat patterns in the data it was trained on, and it can also repeat the biases in that data.

Other uses include image-to-image translation, such as turning a sketch into a photo or a summer photo into a winter one, super-resolution that sharpens a low-resolution image, and restoring damaged photos and film. Diffusion models have taken over much of image generation since 2021, but GANs are still used where a single fast forward pass matters, such as real-time generation on a device. For the deep learning behind them, see what deep learning is.

Example (hypothetical): training a GAN on product photos

Example (hypothetical): a furniture shop trains a GAN on 10,000 photos of chairs to generate new product images. In each training round the discriminator sees 10,000 real chair photos and 10,000 generated ones, or 20,000 images in total. Early on it is right on 9,000 of the real photos and 8,000 of the generated ones, which is 17,000 correct out of 20,000, or 85% accuracy.

After weeks of training the generator improves, and the discriminator is right on 5,200 of the real photos and 5,100 of the generated ones, which is 10,300 correct out of 20,000, or about 51.5% accuracy. That is barely better than guessing, which is exactly what the training aims for: the fakes are now so close to the real photos that the discriminator cannot tell them apart.

The shop checks the output by eye before using any of it. A GAN can produce chairs with three legs or floating armrests, and a near-perfect discriminator score does not mean every image is usable. The lesson is that the score measures the contest, not the quality of any single image.

What are the common mistakes?

The most common mistakes with GANs are training without checking the output, letting the generator collapse onto one output, and trusting the training scores instead of looking at the results.

  • Mode collapse. The generator finds one output that fools the discriminator and produces it over and over. Watch the variety of the output, not just the score.
  • Unstable training. The two networks can chase each other in circles, with the loss swinging instead of falling. Smaller learning rates and a steadier variant help.
  • Judging by the score alone. A discriminator near 50% accuracy does not mean the images are good. Look at them, and check them against the real data.
  • Too little data. A GAN trained on a few hundred images will copy them rather than learn the pattern. More data beats a bigger network.
  • Ignoring bias. A GAN trained on skewed data will generate skewed output, and the skew will show up in whatever the output is used for.
  • Using GANs where a simpler tool works. If the task is classification or prediction, a discriminator alone is the right tool, and the generator is wasted work.

Key takeaways

  • A GAN trains a generator against a discriminator until the fakes pass as real
  • The generator never sees real data; it learns only from the discriminator's scores
  • Goodfellow and colleagues introduced GANs in a 2014 paper
  • Variants such as StyleGAN and CycleGAN add control and stability
  • Judge a GAN by looking at its output, not by its training score alone

Frequently asked questions

What is a generative adversarial network?

A generative adversarial network, or GAN, is a machine learning framework with two neural networks trained against each other. The generator turns random noise into new data, and the discriminator tries to tell generated data from real data, until the generator's output is hard to tell from the training set.

Who invented GANs?

Ian Goodfellow and colleagues at the University of Montreal introduced GANs in the 2014 paper Generative Adversarial Nets. The paper framed training as a two-player game, with the generator trying to fool the discriminator and the discriminator trying to catch it.

What is the difference between a GAN and a diffusion model?

A GAN generates a sample in a single forward pass through the generator, which makes it fast. A diffusion model builds a sample step by step from noise, which is slower but usually produces higher-quality images, and diffusion models have led image generation since around 2021.

What is mode collapse in a GAN?

Mode collapse is when the generator finds one output that fools the discriminator and produces it again and again, instead of covering the full variety of the training data. It is spotted by watching the variety of the output, not by watching the training score.

Can GANs generate text or audio?

Yes, the same contest works for other data types, but GANs are hardest to train on text because text is discrete and the generator's feedback is weaker. Images remain the most common GAN output, with audio and tabular data as secondary uses.

Do I need a lot of data to train a GAN?

Yes. A GAN learns the pattern of the training data by competing over it, and a small dataset leads it to copy the examples rather than learn the pattern. A few thousand clean examples is a practical minimum for a simple image task, and more is better.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a generative AI product? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Machine Learning Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call