Generative Adversarial Networks (GANs) Explained from Scratch: A Complete Beginner’s Guide
Imagine asking an artist to create a realistic painting without ever showing them how to paint. Instead of giving direct instructions, another person simply reviews every painting and says, “This looks real” or “This doesn’t.” After receiving this feedback thousands of times, the artist gradually learns to create paintings that are almost impossible to distinguish from real ones.
This simple idea forms the foundation of Generative Adversarial Networks (GANs), one of the most fascinating breakthroughs in deep learning. Unlike traditional neural networks that classify or predict existing data, GANs learn to generate entirely new data that closely resembles the training data.
From generating realistic human faces and creating artwork to restoring damaged photographs and producing synthetic medical images, GANs have transformed the field of generative artificial intelligence.
In this article, we’ll build a strong theoretical understanding of GANs from scratch. We’ll explore how they work, understand the roles of the Generator and the Discriminator, and see how these two neural networks compete to produce increasingly realistic images.
Why Do We Need GANs?
Before understanding what a GAN is, let’s first understand why it was introduced.
Most traditional deep learning models are discriminative models. Their primary objective is to analyze existing data and make predictions.
For example, a CNN can:
- Classify whether an image contains a cat or a dog.
- Detect objects in an image.
- Recognize handwritten digits.
- Identify diseases from medical scans.
Notice one common characteristic.
These models always work with existing data. They analyze what already exists but cannot create entirely new samples.
Suppose we ask a CNN:
“Generate a completely new human face.”
It simply cannot perform this task because it was never designed for data generation.
This limitation led researchers to develop generative models, whose objective is fundamentally different.
Instead of predicting labels, generative models learn the underlying patterns and distribution of the training data so they can create new samples that resemble real ones. This is exactly where GANs come into the picture.
What is a Generative Adversarial Network (GAN)?
A Generative Adversarial Network (GAN) is a deep learning model consisting of two neural networks that learn by competing against each other.
These networks are:
- Generator (G)
- Discriminator (D)
Unlike conventional neural networks that optimize a single objective, GANs rely on a competitive learning process.
The word Generative means the model can generate new data.
The word Adversarial means the two networks have opposite objectives and continuously challenge one another during training.
Their roles are simple:
- The Generator tries to create realistic fake images.
- The Discriminator tries to determine whether an image is real or fake.
Because both networks continuously improve to outperform each other, the quality of the generated images gradually becomes better over time.
This competition is the key idea behind GANs.
Understanding the Adversarial Concept
The easiest way to understand GANs is to think of a counterfeiter and a detective.
Imagine a person whose job is to create counterfeit currency.
Initially, the fake notes are poorly made, making them easy to identify.
Now imagine another person whose job is to inspect every note and determine whether it is genuine or counterfeit.
Every time the counterfeiter fails, they learn from their mistakes and improve the quality of the fake notes.
At the same time, the inspector also becomes better at identifying increasingly convincing counterfeits.
As this competition continues:
- The counterfeiter improves.
- The inspector improves.
- Both become more skilled over time.
GANs follow exactly the same learning strategy.
The Generator acts like the counterfeiter, while the Discriminator acts like the inspector. Neither network is perfect at the beginning, but continuous competition allows both to learn and improve.
The Two Brains Behind a GAN
Every GAN consists of two neural networks working together.
Although they share the same objective of improving the overall system, their individual goals are completely opposite.
One network creates.
The other evaluates.
One tries to deceive.
The other tries to detect deception.
This constant interaction is what makes GANs fundamentally different from most deep learning models.
Let’s understand each network individually before exploring how they work together.
In the next section, we’ll dive deep into the Generator, understand how it transforms random noise into realistic images, and then explore how the Discriminator learns to identify whether an image is real or fake.
Understanding the Generator
Now that we know a GAN consists of two competing neural networks, let’s begin with the first one — the Generator.
The Generator is the creative part of a GAN. Its responsibility is to create entirely new images that resemble the real images in the training dataset.
However, an important question arises:
If the Generator creates images, what does it use as input?
Unlike CNNs or other image-based neural networks, the Generator does not receive an image as input. Instead, it starts with a vector of random numbers known as the latent vector or noise vector.
A simplified view is shown below:
Random Noise (Latent Vector)
│
▼
Generator
│
▼
Generated ImageInitially, this random vector contains no meaningful visual information. It is simply a collection of randomly generated numbers.
For example:
[0.42, -1.15, 0.83, 0.19, ...]To us, these numbers appear meaningless. However, during training, the Generator gradually learns how to transform these random values into realistic images.
Why Does the Generator Use Random Noise?
A common misconception is that the Generator starts with an incomplete image and improves it.
This is not how GANs work.Instead, every generated image begins with completely random noise.
Why?
Because the Generator’s objective is not to modify existing images. Its objective is to learn the underlying distribution of the training dataset so it can create entirely new samples.
Think of the random noise as a blank canvas.
Just as an artist begins with an empty canvas before creating a painting, the Generator begins with random numbers before learning how to generate an image.
Each different noise vector produces a different output image, allowing the Generator to generate an unlimited variety of images after training.
How Does the Generator Learn?
At the beginning of training, the Generator knows absolutely nothing.
When it receives random noise as input, its output looks like random pixels rather than a meaningful image.
For example:
Random Noise
│
▼
Generator
│
▼
Random PixelsSince the Generator has not learned any patterns yet, the generated image looks nothing like the images in the training dataset.
The Discriminator immediately recognizes it as fake.
However, this failure is actually part of the learning process.
After every training iteration, the Generator receives feedback from the Discriminator.
Using this feedback, it updates its internal weights through backpropagation and gradually learns important visual features such as:
- Shapes
- Edges
- Colors
- Textures
- Object structures
- Fine details
After many iterations, the Generator starts producing images that look increasingly realistic.
Its ultimate goal is simple:
Generate images that are so realistic that the Discriminator believes they are real.
Understanding the Generator Architecture
The Generator is responsible for transforming a random noise vector into a realistic image. It does not create the image in a single step. Instead, the image is generated gradually through multiple layers, where each layer adds more details and increases the image size.
The complete Generator architecture :
Let’s understand the purpose of each step.
Step 1: Random Noise (Latent Vector)
The Generator begins with a vector of random numbers called the latent vector or noise vector.
For example:
[0.35, -1.21, 0.87, 0.14, ...]This vector contains no meaningful visual information. It simply acts as the starting point from which the Generator learns to create an image.
Every different noise vector produces a different generated image.
Step 2: Fully Connected Layer
The random noise is first passed through a Fully Connected (Dense) Layer.
Its purpose is to transform the one-dimensional noise vector into a larger set of learned features.
At this stage, the output is still not an image. Instead, it is a collection of numerical features that will later be converted into image feature maps.
Step 3: Reshaping
The output from the Fully Connected Layer is still a one-dimensional vector.
Since convolution operations require multi-dimensional data, this vector is reshaped into a small three-dimensional feature map.
For example:
1D Vector
│
▼
4 × 4 × 1024 Feature MapThis small feature map becomes the starting point for image generation.
Step 4: ConvTranspose2D
The feature maps are then passed through Transposed Convolution (ConvTranspose2D) layers.
Unlike a standard convolution that reduces image size, ConvTranspose2D increases the spatial dimensions.
For example:
4 × 4
│
▼
8 × 8
│
▼
16 × 16
│
▼
32 × 32
│
▼
64 × 64With every ConvTranspose2D layer, the Generator gradually builds a larger and more detailed image.
Step 5: Batch Normalization
After each ConvTranspose2D layer, Batch Normalization is applied.
Its purpose is to normalize the feature values, making training more stable and helping the Generator learn faster.
It also reduces sudden changes in feature distributions during training.
Step 6: ReLU Activation
Next, a ReLU (Rectified Linear Unit) activation function is applied.
ReLU introduces non-linearity into the network, allowing the Generator to learn complex image patterns instead of simple linear relationships.
Without activation functions, the Generator would not be able to generate realistic images.
Step 7: Repeat the Process
The sequence:
- ConvTranspose2D
- Batch Normalization
- ReLU
is repeated several times.
With every repetition:
- The image becomes larger.
- More visual features are learned.
- Shapes become clearer.
- Textures become richer.
- Fine details gradually appear.
The Generator slowly transforms random noise into a realistic image.
Step 8: Output Layer
After the repeated upsampling stages, the final ConvTranspose2D layer produces an image with the required dimensions.
For example:
64 × 64 × 3where:
- 64 × 64 represents the image size.
- 3 represents the RGB color channels.
Step 9: Tanh Activation
Finally, a Tanh activation function is applied to the output image.
Tanh converts every pixel value into the range:
-1 to +1Most GAN datasets are normalized to this range before training.
Using Tanh ensures that the generated image has the same value range as the real training images, resulting in more stable learning.
At this point, the Generator produces its final output, the generated image.
Understanding the Discriminator
While the Generator creates images, the Discriminator evaluates them.
The Discriminator acts as a binary classifier, meaning it predicts whether an image belongs to one of two possible classes:
- Real
- Fake
Unlike the Generator, the Discriminator does not generate any images.
Instead, it receives an image as input and predicts the probability that the image is real.
A simplified workflow is:
Input Image
│
▼
Discriminator
│
▼
ProbabilityThe output is always a value between 0 and 1.
For example:
- 0.98 → The image is almost certainly real.
- 0.85 → Probably real.
- 0.60 → Slightly real.
- 0.35 → Probably fake.
- 0.08 → Almost certainly fake.
The closer the prediction is to 1, the more confident the Discriminator is that the image is real.
The closer it is to 0, the more confident it is that the image is fake.
How Does the Discriminator Learn?
The Discriminator is trained using two different types of images.
Real Images
These images come directly from the training dataset.
Since they are genuine images, their correct label is:
Real = 1Fake Images
These images are generated by the Generator.
Their correct label is:
Fake = 0During training, the Discriminator compares its predictions with the correct labels.
Whenever it makes a mistake, backpropagation updates its weights so that it can make more accurate predictions in future iterations.
As training continues, the Discriminator becomes increasingly skilled at distinguishing real images from fake ones.
Understanding the Discriminator Architecture
The Discriminator performs the opposite task.
Get Muhammadsufian’s stories in your inbox
Join Medium for free to get updates from this writer.
Instead of generating an image, it receives an image and determines whether it is real or fake.
Its architecture gradually extracts important image features while reducing the spatial dimensions.
The complete Discriminator architecture follows this pipeline:
Let’s understand each step.
Step 1: Input Image
The Discriminator receives 2 images as input.
The 2 images are:
- A real image from the training dataset.
- A fake image generated by the Generator.
Its task is to determine which one it is.
Step 2: Conv2D
The input image passes through Convolution (Conv2D) layers.
These layers extract important visual features from the image.
Initially, they learn:
- Edges
- Lines
- Corners
- Basic shapes
As the network becomes deeper, it learns more complex visual structures.
Step 3: Leaky ReLU
After every convolution layer, Leaky ReLU is applied.
Like ReLU, it introduces non-linearity into the network.
However, unlike ReLU, Leaky ReLU allows a small amount of information to pass even for negative values.
This helps prevent neurons from becoming inactive during training and improves gradient flow.
Step 4: Batch Normalization
Next, Batch Normalization is applied.
It stabilizes the learning process by keeping feature distributions consistent across different training batches.
This enables faster and more reliable training.
Step 5: Repeat the Process
The sequence:
- Conv2D
- Leaky ReLU
- Batch Normalization
is repeated several times.
With each repetition:
- The image becomes smaller.
- Feature extraction becomes stronger.
- The network focuses on increasingly meaningful visual patterns.
Step 6: Flatten
After all convolution operations, the extracted feature maps are converted into a one-dimensional vector using the Flatten operation.
This prepares the data for the final classification stage.
Step 7: Fully Connected Layer
The flattened feature vector is passed to a Fully Connected Layer.
This layer combines all extracted features and prepares a final prediction.
Step 8: Sigmoid Activation
Finally, the Sigmoid activation function converts the output into a probability between 0 and 1.
For example:
- 0.97 → Real image.
- 0.81 → Probably real.
- 0.42 → Probably fake.
- 0.06 → Fake image.
This probability becomes the Discriminator’s final decision.
Generator vs. Discriminator
Although both are neural networks, their objectives are completely opposite.
In simple terms:
- Generator = Image Creator
- Discriminator = Image Judge
One creates, The other evaluates.
Neither network can accomplish the complete task alone.
Only by working together do they gradually learn to generate highly realistic images.
How Both Networks Compete
The learning process in a GAN is often described as a game between two players.
The Generator’s objective is to fool the Discriminator by creating realistic fake images.
At the same time, the Discriminator’s objective is to correctly identify whether an image is real or fake.
Initially:
Generator
│
Creates Poor Image
│
▼
Discriminator
│
Predicts "Fake"Since the Discriminator easily identifies the fake image, the Generator receives a large error and updates its weights.
After many iterations, the Generator improves.
Now the process looks like this:
Generator
│
Creates Better Image
│
▼
Discriminator
│
Not SureEventually:
Generator
│
Creates Realistic Image
│
▼
Discriminator
│
Predicts "Real"At this stage, the Generator has successfully fooled the Discriminator.
However, the competition doesn’t stop here.
The Discriminator now learns from its mistake and becomes better at detecting fake images. This forces the Generator to improve even further.
This continuous competition creates a powerful learning cycle:
Better Generator
│
▼
Stronger Discriminator
│
▼
Better Generator
│
▼
Stronger Discriminator
│
▼
Both networks continuously challenge each other, and as a result, both become increasingly effective over time.
This adversarial learning process is the core idea behind Generative Adversarial Networks and the primary reason they are capable of generating highly realistic images.
Loss Function and Training Process in GANs
Now that we understand the Generator and Discriminator architectures, the next question is:
How do these two networks learn and improve during training?
The answer lies in the loss function.
A loss function measures how well a neural network is performing by comparing its prediction with the correct target.
- Small Loss : The prediction is close to the target.
- Large Loss : The prediction is far from the target.
During GAN training, both the Generator and the Discriminator have their own objectives, so they also have their own losses.
However, before calculating these losses, the Discriminator is always trained first, followed by the Generator.
Why is the Discriminator Updated First?
The Generator learns by receiving feedback from the Discriminator.
If the Discriminator is not trained first, it cannot provide meaningful feedback because it has not yet learned how to distinguish between real and fake images.
Therefore, every GAN training iteration follows this order:
Step 1 → Train the Discriminator
Step 2 → Train the GeneratorThis process repeats throughout training.
Step 1: Updating the Discriminator
The first network to be trained is the Discriminator.
During this step, the Discriminator receives two different types of images:
- Real images from the training dataset.
- Fake images generated by the Generator.
Its objective is straightforward:
- Predict 1 for real images.
- Predict 0 for fake images.
A simplified workflow is shown below.
The Discriminator makes predictions for both real and fake images.
It then compares these predictions with the correct labels to calculate its loss.
Binary Cross Entropy (BCE) Loss
Since the Discriminator performs binary classification, there are only two possible classes:
- Real = 1
- Fake = 0
Because there are only two classes, GANs commonly use Binary Cross Entropy (BCE) Loss.
The BCE loss is calculated using the following formula:
Where:
- (y) = Actual (target) label.
- (p) = Predicted probability from the Discriminator.
The BCE loss measures how close the predicted probability is to the correct target.
Calculating the Discriminator Loss
The Discriminator calculates two separate losses.
1. Loss for Real Images
Suppose:
- Target = 1
- Prediction = 0.95
The prediction is very close to the target.
Therefore:
- Loss is small.
2. Loss for Fake Images
Suppose:
- Target = 0
- Prediction = 0.08
Again, the prediction is close to the target.
Therefore:
- Loss is small.
The total Discriminator loss is simply the sum of these two losses.
Discriminator Loss
│
├── Real Image Loss
│
└── Fake Image Loss
Total Loss = Real Loss + Fake LossA small total loss indicates that the Discriminator is successfully distinguishing between real and fake images.
Backpropagation for the Discriminator
Once the Discriminator loss has been calculated, the network learns through backpropagation.
The process is straightforward:
Prediction
│
▼
Calculate Loss
│
▼
Backpropagation
│
▼
Update WeightsBackpropagation computes how much each weight contributed to the error.
Using this information, the optimizer updates the weights so that the Discriminator makes better predictions during the next training iteration.
Once the Discriminator has been updated, we move to the Generator.
Step 2: Updating the Generator
After training the Discriminator, the Generator is trained.
Unlike the Discriminator, the Generator never sees real images directly.
Instead, it receives:
- Random noise as input.
The Generator then produces a fake image and sends it to the Discriminator.
Now comes the interesting part.
Although the image is fake, the Generator does not want the Discriminator to predict 0.
Instead, it wants the Discriminator to believe that the fake image is real.Therefore, during Generator training:
Target = 1even though the generated image is fake.
Calculating the Generator Loss
Suppose the Discriminator predicts:
Prediction = 0.08This means the Discriminator is almost certain that the generated image is fake.
Since the Generator wanted a prediction close to 1, this produces a large loss.
For example:
- Target = 1
- Prediction = 0.08
Result:
- Large Loss
A large loss tells the Generator:
“Your generated image is not realistic enough. Improve it.”
After updating its weights, the Generator gradually improves.
The predictions begin to move closer to the desired target.
0.08
│
▼
0.24
│
▼
0.47
│
▼
0.71
│
▼
0.89
│
▼
0.97As the prediction approaches 1, the Generator becomes increasingly successful at fooling the Discriminator.
Backpropagation for the Generator
Once the Generator loss has been calculated, backpropagation is performed again.
This time, however, only the Generator’s weights are updated.
The Discriminator is not updated during this step.
The process is:
Random Noise
│
▼
Generator
│
▼
Fake Image
│
▼
Discriminator
│
▼
Calculate Generator Loss
│
▼
Backpropagation
│
▼
Update Generator WeightsThe Generator gradually learns how to produce images that appear increasingly realistic.
Complete Training Cycle
A single training iteration of a GAN can be summarized as follows:
This process is repeated for thousands of iterations.
Initially, the Generator produces poor-quality images, making them easy for the Discriminator to identify.
As training progresses:
- The Generator becomes better at creating realistic images.
- The Discriminator becomes better at identifying fake images.
This continuous competition forces both networks to improve together, eventually enabling the Generator to produce images that are remarkably similar to real data.
Conclusion
Generative Adversarial Networks (GANs) introduced a completely new way of training deep learning models. Instead of relying on a single neural network, GANs use two networks — the Generator and the Discriminator — that compete with each other in an adversarial learning process. This continuous competition enables the Generator to produce increasingly realistic images while the Discriminator becomes better at distinguishing real images from generated ones.
In this article, we built a strong theoretical foundation by exploring every core concept behind GANs. We began by understanding why GANs were introduced and how they differ from traditional deep learning models. We then examined the roles of the Generator and Discriminator, their architectures, the concept of latent space, the complete training workflow, Binary Cross Entropy (BCE) loss, and how backpropagation allows both networks to improve through iterative learning.
One of the most important ideas to remember is that GANs do not learn by being explicitly told how to generate images. Instead, the Generator gradually learns from the feedback provided by the Discriminator. Over thousands of training iterations, this adversarial process allows the Generator to transform random noise into highly realistic images that closely resemble the training data.
Although the underlying mathematics and training procedure may initially seem challenging, the overall concept is surprisingly intuitive once you understand the interaction between these two networks. The Generator continuously tries to fool the Discriminator, while the Discriminator continuously learns to detect fake images. This dynamic competition is the key reason why GANs have become one of the most powerful generative models in modern artificial intelligence.
Thank you for reading! I hope this article helped you build a solid theoretical understanding of Generative Adversarial Networks (GANs). If you found it helpful, consider sharing it with others who are learning deep learning and generative AI. In the next article, we’ll move from theory to practice by implementing a GAN in PyTorch, where we’ll explore every line of code step by step.









