Deep Learning Notes
Deep Learning Notes
Deep Learning is a subset of Machine Learning (ML) that uses artificial neural networks with many
layers to learn patterns from large amounts of data. It is inspired by the structure and function of the
human brain. Deep learning is especially powerful in handling unstructured data like images, audio,
video, and text.
Deep learning models automatically learn features from raw data by passing it through multiple layers
(input → hidden layers → output). Each layer learns different levels of abstraction.
4
The breakthrough came in 2012 when AlexNet, a deep CNN, significantly outperformed traditional
methods in the ImageNet challenge. Since then, deep learning has powered breakthroughs in vision,
language, robotics, and healthcare.
Artificial Neural Networks contain artificial neurons, which are called units. These units are arranged
in a series of layers that together constitute the whole Artificial Neural Network in a system. A layer
can have only a dozen units or millions of units, as this depends on how the complex neural networks
will be required to learn the hidden patterns in the dataset. Commonly, an Artificial Neural Network
has an input layer, an output layer, as well as hidden layers. The input layer receives data from the
outside world, which the neural network needs to analyze or learn about. Then, this data passes
through one or multiple hidden layers that transform the input into data that is valuable for the output
layer. Finally, the output layer provides [Link] the majority of neural networks, units are
interconnected from one layer to another. Each of these connections has weights that determine the
influence of one unit on another unit. As the data transfers from one unit to another, the neural
network learns more and more about the data, which eventually results in an output from the output
layer.
The structures and operations of human neurons serve as the basis for artificial neural networks. It is
also known as neural networks or neural nets. The input layer of an artificial neural network is the
first layer, and it receives input from external sources and releases it to the hidden layer, which is the
second layer. In the hidden layer, each neuron receives input from the previous layer neurons,
computes the weighted sum, and sends it to the neurons in the next layer. These connections are
weighted means effects of the inputs from the previous layer are optimized more or less by assigning
5
different-different weights to each input and it is adjusted during the training process by optimizing
these weights for improved model performance.
The concept of artificial neural networks comes from biological neurons found in animal brains So
they share a lot of similarities in structure and function wise.
Structure: The structure of artificial neural networks is inspired by biological neurons. A
biological neuron has a cell body or soma to process the impulses, dendrites to receive them,
and an axon that transfers them to other neurons. The input nodes of artificial neural networks
receive input signals, the hidden layer nodes compute these input signals, and the output layer
nodes compute the final output by processing the hidden layer's results using activation
functions.
Synapses: Synapses are the links between biological neurons that enable the transmission of
impulses from dendrites to the cell body. Synapses are the weights that join the one-layer nodes to the
next-layer nodes in artificial neurons. The strength of the links is determined by the weight value.
Learning: In biological neurons, learning happens in the cell body nucleus or soma, which has a
nucleus that helps to process the impulses. An action potential is produced and travels through the
axons if the impulses are powerful enough to reach the threshold. This becomes possible by synaptic
plasticity, which represents the ability of synapses to become stronger or weaker over time in reaction
to changes in their activity. In artificial neural networks, backpropagation is a technique used
for learning, which adjusts the weights between nodes according to the error or differences between
predicted and actual outcomes.
Activation: In biological neurons, activation is the firing rate of the neuron which happens when the
impulses are strong enough to reach the threshold. In artificial neural networks, A mathematical
function known as an activation function maps the input to the output, and executes activations.
6
Artificial neural networks are trained using a training set. For example, suppose you want to
teach an ANN to recognize a cat. Then it is shown thousands of different images of cats so that the
network can learn to identify a cat. Once the neural network has been trained enough using images of
cats, then you need to check if it can identify cat images correctly. This is done by making the ANN
classify the images it is provided by deciding whether they are cat images or not. The output obtained
by the ANN is corroborated by a human-provided description of whether the image is a cat image or
not. If the ANN identifies incorrectly then is used to adjust whatever it has learned during training.
Backpropagation is done by fine-tuning the weights of the connections in ANN units based on the
error rate obtained. This process continues until the artificial neural network can correctly recognize a
cat in an image with minimal possible error rates.
For XOR:
No combination of weights and bias will allow a single-layer perceptron to classify the
outputs correctly.
It cannot learn non-linear decision boundaries.
Hence, XOR cannot be solved by a single-layer neural network.
Architecture:
Input layer: 2 neurons (for x₁ and x₂)
Hidden layer: 2 neurons (to learn intermediate features)
7
Output layer: 1 neuron (XOR result)
The hidden layer enables the network to transform the input space into a new space where the
problem becomes linearly separable.
Single-Layer Perceptron:
Structure: It consists of input nodes, an output node, and a single layer of connections (weights)
between them.
Function: It learns a linear decision boundary to classify data points.
Limitations: It cannot solve problems like XOR, which requires non-linear separation.
Example: A single perceptron could be used to classify simple images of cats and dogs based on a few
features.
Function:
It can learn non-linear relationships in data, enabling it to solve more complex problems than a single-
layer perceptron.
Learning:
MLPs typically use backpropagation, an algorithm that adjusts weights to minimize errors.
Example:
An MLP can be used for image recognition, speech recognition, or natural language processing.
8
Feed Forward Neural Network (FFNN)
A Feed Forward Neural Network (FFNN) is the most basic type of Artificial Neural Network (ANN).
It is called “feed-forward” because the data flows in one direction — from the input layer to the
output layer — without any loops or feedback.
1. Input Layer:
o Receives the raw data (e.g., pixels in an image, words in text).
o Each neuron in this layer corresponds to one input feature.
2. Hidden Layers:
o Intermediate layers where the computation happens.
o Each neuron performs a weighted sum of its inputs, adds a bias, and applies an
activation function.
o FFNN can have one or multiple hidden layers.
3. Output Layer:
o Produces the final prediction or classification.
o Number of neurons in this layer depends on the task (e.g., 1 for binary classification,
N for N-class classification).
9
Deep Feed Forward Networks
A Deep Feed Forward Network (DFFN), also known as a Deep Neural Network (DNN), is a type of
neural network that consists of multiple layers of neurons between the input and output layers. These
hidden layers allow the network to learn complex, hierarchical representations of data.
10
Training Deep Networks
Training involves:
Forward propagation: Calculate output layer values
Loss computation: Measure prediction error
Backpropagation: Compute gradients layer by layer from output to input
Gradient descent (e.g., SGD, Adam): Adjust weights to reduce loss
As the number of layers increases, techniques such as batch normalization, dropout, and skip
connections are used to stabilize training and prevent overfitting or vanishing gradients.
Instead of using the entire dataset (like Batch Gradient Descent), SGD updates weights using
a single sample or a mini-batch.
It's faster, introduces randomness, and helps escape local minima.
Common variants: Mini-batch SGD, Momentum, Adam optimizer.
11
Stochastic Gradient Descent (SGD) is a fundamental optimization technique used in training
deep learning models. Its primary goal is to minimize the loss function by adjusting the weights and
biases in a neural network in such a way that the network's predictions become closer to the actual
target values. Unlike Batch Gradient Descent, which computes the gradient of the loss function using
the entire dataset, SGD updates the model parameters using only one training example at a time. This
leads to more frequent updates, making the training process faster and often more suitable for large
datasets.
The term "stochastic" means random. In the context of SGD, this refers to the randomness introduced
by using a single or randomly selected subset of data (called a mini-batch) for each weight update.
Because each update is based on a small, potentially non-representative sample of the dataset, the path
that the weights take toward the minimum loss is noisy or irregular. However, this randomness is
beneficial: it can help the algorithm escape local minima or saddle points, making it more effective for
optimizing non-convex functions like those found in deep neural networks.
One of the major advantages of SGD is its computational efficiency. Since it processes one or a few
samples at a time, it requires less memory and enables the use of online or streaming data, where the
entire dataset is not available at once. This makes it especially useful for training deep learning
models with millions of parameters and massive datasets.
Over time, several enhancements to basic SGD have been proposed to improve convergence speed
and stability. Mini-batch SGD, for instance, updates the weights using small batches of data (e.g., 32
or 64 samples), striking a balance between the stability of batch gradient descent and the speed of
pure SGD. Momentum-based SGD incorporates a fraction of the previous update direction to dampen
oscillations and accelerate learning in relevant directions. More advanced variants like the Adam
(Adaptive Moment Estimation) optimizer combine momentum and adaptive learning rates for each
parameter, leading to faster convergence and better performance in many scenarios.
In modern deep learning frameworks, SGD and its variants are standard tools used to train neural
networks for tasks such as image recognition, natural language processing, and reinforcement
learning. Its scalability and ability to handle large, high-dimensional data make it a core component in
the development of state-of-the-art models across many domains.
Hidden Units
Hidden units are the core computational elements located in the hidden layers of a neural network.
These units, also known as neurons, play a crucial role in enabling the network to learn complex
patterns and hierarchical representations of data. Each hidden unit receives inputs from the previous
layer — whether it's the input layer or another hidden layer — and performs a weighted sum of these
inputs. This sum is then adjusted using a bias term, and the result is passed through a non-linear
activation function. The output from this process becomes the input for the next layer in the network.
The presence of hidden units allows the network to move beyond simple linear mappings. By
introducing non-linearity, hidden units help the model capture intricate relationships and abstract
features within the data. The number of hidden units in each layer, as well as the total number of
hidden layers, determines the capacity of the network. A network with too few hidden units might not
have enough capacity to learn the training data, leading to underfitting. On the other hand, a network
with too many hidden units might memorize the training data instead of generalizing well to unseen
examples, resulting in overfitting.
12
The choice of activation function applied within hidden units significantly affects the learning
dynamics and representational power of the network. Common activation functions include sigmoid,
which maps inputs to a range between 0 and 1; tanh, which maps inputs to a range between -1 and 1;
and ReLU (Rectified Linear Unit), which outputs zero for negative inputs and passes positive inputs
unchanged. ReLU has become the default activation function in many modern deep learning
architectures due to its simplicity and effectiveness in mitigating the vanishing gradient problem.
Overall, hidden units form the intermediate layers of abstraction in a neural network. They enable the
model to break down the learning task into manageable sub-problems, gradually transforming the
input data into a representation suitable for the output layer. Whether the task involves image
recognition, language translation, or speech processing, the hidden units are essential in allowing
neural networks to learn deep, abstract representations that drive intelligent behavior.
Hidden units are the neurons in hidden layers of a neural network. They:
Receive inputs from the previous layer
Apply a weighted sum, bias, and activation function
Pass the output to the next layer
The number and type of hidden units determine the model’s capacity and performance. Too few →
underfitting; too many → overfitting.
Architecture Design
Architecture design in deep learning refers to the process of configuring the structure of a neural
network to best suit a particular task. This involves several important choices, each of which
significantly impacts the network’s learning ability, efficiency, and generalization. One of the most
fundamental decisions is determining the number of layers, commonly referred to as the depth of the
network. Deeper networks can model more complex patterns by composing multiple levels of
abstraction, but they are also more computationally demanding and may be harder to train.
Another critical aspect is the number of neurons in each layer, known as the width of the network.
Wider layers provide more representational capacity and can help the network learn finer details of
13
the data. However, excessive width may lead to overfitting, especially if the training data is limited.
The type of layers used is also a major consideration. For instance, fully connected (dense) layers are
common in basic feedforward networks; convolutional layers are used in CNNs for processing image
data; and recurrent or transformer layers are used for sequential or time-dependent data such as text or
speech.
In addition to layer types, selecting appropriate activation functions is crucial. These functions
introduce non-linearity into the network, enabling it to learn complex mappings. Common choices
include ReLU, sigmoid, and tanh. The architecture must also include a suitable loss function, which
quantifies how far off the model’s predictions are from the actual targets. Along with the loss
function, an optimizer like SGD, Adam, or RMSProp is used to update the network parameters during
training.
In conclusion, architecture design is both an art and a science. It requires a careful combination of
theoretical understanding, practical considerations, and experimental tuning to develop models that
are not only accurate but also efficient and robust in real-world applications.
Examples:
CNNs for images
RNNs for sequences
Transformers for language
Backpropagation
Backpropagation is a fundamental algorithm used for training neural networks. It allows the model to
learn from errors by systematically adjusting the weights of the network to minimize a predefined loss
function. The central idea behind backpropagation is to compute how much each parameter (weight
and bias) in the network contributed to the overall error, and then make corrections to those
parameters in the direction that reduces the error.
The process begins with a forward pass, where input data is passed through the network layer by layer
to compute the final output using the current set of weights and activation functions. The predicted
14
output is then compared with the actual target values to calculate the loss, which quantifies how
incorrect the predictions are. Common loss functions include mean squared error for regression and
cross-entropy for classification.
Once the loss is calculated, the backward pass begins. This is where the actual backpropagation
happens. The algorithm applies the chain rule of calculus to compute the gradient of the loss with
respect to each weight in the network. Starting from the output layer and moving backward toward the
input layer, these gradients measure how sensitive the loss is to changes in each weight.
After computing the gradients, the final step is to update the weights using an optimization algorithm
such as Stochastic Gradient Descent (SGD) or its variants (like Adam). The weights are adjusted in
the direction opposite to the gradient, effectively reducing the loss in the next iteration. The learning
rate, a key hyperparameter, controls the size of these updates.
Backpropagation is crucial for enabling supervised learning in deep networks. Without it, the model
would have no way to adjust itself based on errors made during training. It provides a mathematically
efficient and scalable way to train even large networks with millions of parameters. Over many
iterations, this process allows the network to gradually reduce its prediction errors, improving
accuracy, generalization, and overall performance on the task.
Backpropagation is the learning engine of neural networks. It empowers the model to learn from data,
refine its internal parameters, and improve its predictive ability, forming the backbone of modern
deep learning systems.
Backpropagation is the algorithm used to train neural networks by adjusting weights to minimize the
error.
Steps:
1. Forward pass: Compute output using current weights.
2. Compute loss: Measure prediction error.
3. Backward pass: Calculate gradients of loss w.r.t. each weight using chain rule.
4. Update weights: Using gradient descent.
Backpropagation enables the network to learn from its mistakes and gradually improve accuracy.
15
UNIT-II
Convolutional Neural Networks (CNNs) are a specialized class of deep learning models designed
primarily for processing data with a grid-like topology, such as images. They have revolutionized the
field of computer vision by achieving remarkable accuracy in tasks like image classification, object
detection, segmentation, and facial recognition. CNNs are inspired by the structure of the visual
cortex in animals, where neurons respond to overlapping regions of the visual field. This biological
motivation helps CNNs recognize patterns and features in images effectively.
The key idea behind CNNs is the convolution operation, which replaces traditional matrix
multiplication in at least one of the layers of a neural network. This operation uses small filters or
kernels to scan through the input image and extract local features such as edges, textures, or colors.
These features are then passed through layers of increasing depth, where more abstract patterns are
recognized, such as shapes or entire objects.
One of the main advantages of CNNs is their parameter efficiency. Unlike fully connected networks
that treat all pixels equally and require a large number of weights, CNNs share weights through filters,
significantly reducing the number of parameters. This makes CNNs more computationally efficient
and less prone to overfitting, especially on high-dimensional input like images.
CNNs are structured hierarchically, with early layers capturing low-level features (edges, corners),
and deeper layers recognizing high-level semantic features (like eyes, wheels, or digits). This layered
feature learning process allows CNNs to automatically extract features without the need for manual
feature engineering.
In terms of applications, CNNs have become the backbone of modern computer vision systems. In
image classification, CNNs can distinguish between thousands of categories such as cats, dogs, and
airplanes. In object detection, they not only classify objects but also localize them using bounding
boxes. Semantic segmentation uses CNNs to label each pixel in an image with a class label. In
medical imaging, CNNs are used to identify tumors in X-rays, MRIs, or CT scans. They are also used
in autonomous vehicles to detect pedestrians, lane markings, and traffic signs.
Further applications include facial recognition, optical character recognition (OCR), gesture
recognition, and style transfer. CNNs also play a critical role in content-based image retrieval systems
and are increasingly being used in video analysis, such as action recognition and video classification.
CNNs provide a powerful framework for handling image and video data by leveraging spatial
hierarchies and local feature extraction. Their success in real-world applications continues to grow,
making them a fundamental component of modern artificial intelligence systems.
16
CNN basic architecture
The input layer of a CNN receives raw pixel values from an image. For example, a grayscale image of
size 28×28 is represented as a matrix of pixel intensity values, while a color image has three channels
(Red, Green, Blue), forming a 3D tensor.
The convolutional layer is the core building block of a CNN. It uses small learnable filters (e.g., 3×3
or 5×5) that are convolved over the input to produce feature maps. Each filter is trained to detect a
specific feature such as vertical edges, horizontal edges, or patterns. The convolution operation
preserves the spatial relationship between pixels by learning local patterns, which makes CNNs
particularly powerful in visual tasks.
After the convolution operation, the output is passed through a non-linear activation function like
ReLU (Rectified Linear Unit), which introduces non-linearity into the model, allowing it to learn
complex mappings.
Next comes the pooling layer, which performs downsampling. Pooling reduces the spatial dimensions
(width and height) of the feature maps while retaining the most important information. This not only
reduces computational cost but also provides translation invariance. Common pooling techniques
include max pooling and average pooling.
The flattening layer converts the 2D feature maps into a 1D vector, which is then fed into one or more
fully connected (dense) layers. These layers are similar to those in traditional neural networks and are
used to combine the features learned by the convolutional and pooling layers to make final
predictions.
Finally, the output layer produces the prediction results. For classification tasks, it often uses a
softmax activation function, which converts the output into a probability distribution across different
classes.
A CNN architecture may also include regularization techniques such as dropout to prevent overfitting
and batch normalization to stabilize training. The design of the architecture (number of layers, filter
sizes, stride, padding, etc.) is usually tailored to the specific problem and dataset.
To summarize, the basic architecture of a CNN transforms raw image input through a series of
convolution, activation, and pooling layers into a meaningful output. This architecture allows the
model to learn hierarchical representations of data — from low-level features like edges to high-level
semantics like objects — making CNNs the foundation of modern computer vision solutions.
17
The ConvNet’s job is to compress the images into a format that is easier to process while preserving
elements that are important for obtaining a decent prediction. This is critical for designing an
architecture that is capable of learning features while also being scalable to large datasets.
A convolutional neural network, ConvNets in short has three layers which are its building blocks, let’s
have a look:
Convolutional Layer (CONV): They are the foundation of CNN, and they are in charge of executing
convolution operations. The Kernel/Filter is the component in this layer that performs the convolution
operation (matrix). Until the complete image is scanned, the kernel makes horizontal and vertical
adjustments dependent on the stride rate. The kernel is less in size than a picture, but it has more
depth. This means that if the image has three (RGB) channels, the kernel height and width will be
modest spatially, but the depth will span all three.
Other than convolution, there is another important part of convolutional layers, known as the Non-
linear activation function. The outputs of the linear operations like convolution are passed through a
non-linear activation function. Although smooth nonlinear functions such as the sigmoid or
hyperbolic tangent (tanh) function were formerly utilized because they are mathematical
representations of biological neuron actions. The rectified linear unit (ReLU) is now the most
commonly used non-linear activation function. f(x) = max(0, x)
Pooling Layer (POOL): This layer is in charge of reducing dimensionality. It aids in reducing the
amount of computing power required to process the data. Pooling can be divided into two types:
maximum pooling and average pooling. The maximum value from the area covered by the kernel on
the image is returned by max pooling. The average of all the values in the part of the image covered
by the kernel is returned by average pooling.
18
Fully Connected Layer (FC): The fully connected layer (FC) works with a flattened input, which
means that each input is coupled to every neuron. After that, the flattened vector is sent via a few
additional FC layers, where the mathematical functional operations are normally performed. The
classification procedure gets started at this point. FC layers are frequently found near the end of CNN
architectures if they are present.
Along with the above layers, there are some additional terms that are part of a CNN architecture.
Activation Function: The last fully connected layer’s activation function is frequently distinct from
the others. Each activity necessitates the selection of an appropriate activation function. The softmax
function, which normalizes output real values from the last fully connected layer to target class
probabilities, where each value ranges between 0 and 1 and all values total to 1, is an activation
function used in the multiclass classification problem.
Dropout Layers: The Dropout layer is a mask that nullifies some neurons’ contributions to the
following layer while leaving all others unchanged. A Dropout layer can be applied to the input
19
vector, nullifying some of its properties; however, it can also be applied to a hidden layer, nullifying
some hidden neurons. Dropout layers are critical in CNN training because they prevent the training
data from overfitting. If they aren’t there, the first batch of training data has a disproportionately large
impact on learning. As a result, learning of traits that occur only in later samples or batches would be
prevented.
Activation functions are a critical component of neural networks, including CNNs. They determine
whether a neuron should be activated or not by introducing non-linearity into the model. Without
activation functions, the network would behave like a linear regression model, unable to capture
complex patterns.
1. Introduce Non-linearity: Without non-linearity, a neural network would behave like a linear
model, no matter how deep it is. Activation functions allow the network to learn and represent
complex, non-linear mappings between inputs and outputs.
2. Control Neuron Activation: Activation functions control the firing behavior of neurons.
Depending on the activation function’s output, a neuron might become activated (output a
non-zero value) or remain inactive (output zero).
20
Typically, same activation function is applied to all the hidden layers, while the output layer uses a
different activation function, based on the type of prediction model aims to make.
It maps input values into the range (0, 1), making it useful for binary classification problems.
However, it suffers from the vanishing gradient problem, where gradients become too small for large
or small input values, slowing down or stopping learning.
It maps values to the range (-1, 1), and like sigmoid, it has a smooth gradient, but with a zero-centered
output, which helps in optimization. Still, it suffers from vanishing gradients for large input values.
It has become the most commonly used activation in CNNs due to its simplicity and effectiveness. It
allows the network to converge faster and avoids the vanishing gradient issue. However, ReLU can
suffer from the “dying ReLU” problem, where neurons get stuck during training and output zero for
all inputs.
To address this, Leaky ReLU introduces a small slope for negative input values, defined as:
where α is a small constant like 0.01. This modification allows negative input values to propagate and
prevents neurons from dying.
For classification tasks, especially multi-class, the Softmax function is used in the final layer. It
converts a vector of raw scores into probabilities by emphasizing the highest values and suppressing
21
the others. Each output is in the range (0,1) and sums to 1 across all classes, making it ideal for
deciding the most likely class.
Activation functions are essential for learning complex, non-linear relationships in data. Choosing the
right activation function affects the learning efficiency, convergence speed, and overall performance
of CNN models.
Pooling layers are a fundamental part of Convolutional Neural Networks (CNNs). Their main
function is to progressively reduce the spatial dimensions (width and height) of the feature maps,
which helps in reducing the number of parameters and computations in the network. Pooling also
provides translation invariance, meaning that small shifts in the input image do not significantly
change the output of the model.
22
Pooling operates independently on every depth slice (channel) of the input and resizes it spatially. The
most commonly used types of pooling are max pooling, average pooling, and global pooling.
Max Pooling is the most widely used form of pooling. In this method, a filter (e.g., 2×2) slides over
the input feature map, and for each region, the maximum value is taken. This effectively captures the
most prominent feature (strongest activation) in that region. Max pooling retains sharp and distinct
features and is commonly used after convolution and activation layers.
Average Pooling computes the average of the values in each region covered by the filter. It is less
aggressive than max pooling and provides smoother representations. Average pooling may be useful
in certain applications where subtle patterns and overall trends are more important than the strongest
signals.
23
Max Pooling Process
Since we have got a basic idea what max pooling intends to do , let’s discuss about the operation what
it actually does.
We have discussed about role of kernels in the previous post . They act as filters and when they
convolve over an image, they create an output corresponding to computations of the filter applied .
For each movement of the filter from the n*n pixels block of the image under consideration at that
point, maximum value is captured as the output for the next layer . Then the filter is moved by k
pixels (defined as stride) to perform the same operation [Link] key point to be noted that Max
pooling is applied after a convolutional layer .
Global Pooling is a special case of pooling where the pooling operation is applied over the entire
feature map instead of small regions. In global max pooling, the maximum value from the entire map
is chosen, and in global average pooling, the average of all values is computed. These are often used
before the final output layer to convert feature maps into a single vector, especially in architectures
designed for image classification.
In addition to the type of pooling, the choice of stride (the number of pixels the filter moves) and
whether or not to use padding (adding zeros around the input) also affects the output size and
information retained. A stride of 2 is common and reduces the size of feature maps by half in both
dimensions.
While pooling is extremely useful, it also leads to loss of spatial information. In recent architectures,
alternatives like strided convolutions and attention mechanisms are being explored to retain more
detailed spatial information.
Training a Convolutional Neural Network (CNN) using TensorFlow, an open-source deep learning
framework developed by Google, is one of the most efficient and scalable ways to build and deploy
24
deep learning models. TensorFlow provides a flexible, high-level API called Keras, which simplifies
model development, training, and evaluation.
The training process begins with defining the model architecture. This involves stacking layers such
as Conv2D, MaxPooling2D, Flatten, and Dense using either the Sequential API or Functional API.
Simple CNN model can be defined in TensorFlow Keras as follows:
model = [Link]([
[Link].Conv2D(32, (3,3), activation='relu', input_shape=(64, 64, 3)),
[Link].MaxPooling2D(2, 2),
[Link](),
[Link](128, activation='relu'),
[Link](10, activation='softmax')
])
Once the architecture is defined, the model needs to be compiled using an optimizer, a loss function,
and metrics to monitor during training. For example:
Training a CNN requires preprocessing the input images using data augmentation, normalization,
resizing, and batching. TensorFlow provides the ImageDataGenerator and [Link] APIs for
handling large datasets efficiently.
Furthermore, TensorFlow supports training on GPUs and TPUs, enabling faster computation. It also
allows transfer learning by using pretrained models such as VGG, ResNet, and MobileNet to fine-tune
for custom tasks.
25
[Link]([]); [Link]([]); [Link](False)
[Link](train_images[i])
[Link](class_names[train_labels[i][0]])
[Link]()
Over the years, several powerful CNN architectures have been proposed to improve accuracy and
efficiency in deep learning tasks. Among the most influential are VGG, GoogleNet (Inception), and
ResNet, each contributing innovative design ideas that have shaped modern neural networks.
VGG (Visual Geometry Group Network), developed by the University of Oxford, is known for its
simplicity and uniform architecture. The VGG-16 and VGG-19 models use 3×3 convolution filters
throughout the network, stacked with increasing depth. Max pooling is applied after certain layers to
26
reduce the spatial dimensions. Despite its large number of parameters and memory usage, VGG
demonstrated that depth improves performance, inspiring the development of deeper models. VGG is
particularly well-suited for image classification and transfer learning.
What is VGG-Net?
It is a typical deep Convolutional Neural Network (CNN) design with numerous layers, and the
abbreviation VGG stands for Visual Geometry Group. The term “deep” describes the number of
layers, with VGG-16 or VGG-19 having 16 or 19 convolutional layers, respectively.
Innovative object identification models are built using the VGG architecture. The VGGNet, created as
a deep neural network, outperforms benchmarks on a variety of tasks and datasets outside of
ImageNet. It also remains one of the most often used image recognition architectures today.
VGG-16
The convolutional neural network model called the VGG model, or VGGNet, that supports 16 layers
is also known as VGG16. It was developed by A. Zisserman and K. Simonyan from the University of
Oxford. The research paper titled “Very Deep Convolutional Networks for Large-Scale Image
Recognition” contains the model that these researchers released.
In ImageNet, the VGG16 model achieves top-5 test accuracy of about 92.7 per cent. A dataset called
ImageNet has over 14 million photos that fall into almost 1000 types. It was also among the most
well-liked models submitted at ILSVRC-2014. It significantly outperforms AlexNet by substituting
several 3x3 kernel-sized filters for the huge kernel-sized filters. Nvidia Titan Black GPUs were used
to train the VGG16 model over many weeks.
The VGGNet-16 has 16 layers and can classify photos into 1000 different object categories, including
keyboard, animals, pencil, mouse, etc., as discussed above. The model also accepts images with a
resolution of 224 by 224.7
VGG-Net Architecture
Very tiny convolutional filters are used in the construction of the VGG network. Thirteen
convolutional layers and three fully connected layers make up the VGG-16.
27
Inputs: The VGGNet accepts 224224-pixel images as input. To maintain a consistent input
size for the ImageNet competition, the model’s developers chopped out the central 224224
patches in each image.
Convolutional Layers: VGG’s convolutional layers use the smallest feasible receptive field,
or 33, to record left-to-right and up-to-down movement. Additionally, 11 convolution filters
are used to transform the input linearly. The next component is a ReLU unit, a significant
advancement from AlexNet that shortens training time. Rectified linear unit activation
function, or ReLU, is a piecewise linear function that, if the input is positive, outputs the
input; otherwise, the output is zero. The convolution stride is fixed at 1 pixel to keep the
spatial resolution preserved after convolution (stride is the number of pixel shifts over the
input matrix).
Hidden Layers: The VGG network’s hidden layers all make use of ReLU. Local Response
Normalization (LRN) is typically not used with VGG as it increases memory usage and
training time. Furthermore, it doesn’t increase overall accuracy.
Fully Connected Layers: The VGGNet contains three layers with full connectivity. The first
two levels each have 4096 channels, while the third layer has 1000 channels with one channel
for each class.
Understanding VGG-16
The deep neural network’s 16 layers are indicated by the number 16 in their name, which is VGG
(VGGNet). This indicates that the VGG16 network is quite large, with a total of over 138 million
parameters. Even by today’s high standards, it is a sizable network. The network is more appealing
due to the simplicity of the VGGNet16 architecture, nevertheless. Its architecture alone can be used to
describe how uniform it is.
28
The height and width are decreased by a pooling layer that comes after a few convolution layers.
There are around 64 filters available, which we can then multiply by two to get about 128 filters, and
so on up to 256 filters. In the last layer, we can use 512 filters.
GoogleNet, also called Inception V1, introduced the Inception module, which allows the network to
learn both wide and deep representations simultaneously. Instead of stacking layers linearly, Inception
modules use multiple parallel convolution filters (1×1, 3×3, 5×5) and concatenate their outputs. This
design improves performance while keeping computational cost manageable. GoogleNet also
29
introduced 1×1 convolutions for dimensionality reduction and achieved state-of-the-art results on the
ImageNet challenge with fewer parameters than VGG.
GoogLeNet addressed the challenges of previous CNN architectures by introducing the concept of
inception modules. Inception modules are a type of building block that allows for the parallel
processing of data at multiple scales. This allows the network to capture features at different scales
more efficiently than previous architectures.
An inception module typically consists of several convolutional layers with different filter sizes.
These layers are arranged in parallel, so that the network can process the input data at multiple
resolutions simultaneously. The output of the convolutional layers is then concatenated and passed
through a pooling layer. However, later there were various versions of the inception module which
was integrated accordingly in the architecture which consisted of different layers and filter size
patterns.
This parallel processing approach has several advantages. First, it allows the network to capture
features at different scales more efficiently. This is because the network can process the input data at
multiple resolutions simultaneously, which allows it to capture both large-scale and small-scale
features. Second, it helps to alleviate the problem of vanishing gradients. This is because the parallel
processing approach allows the network to learn features at multiple scales, which can help to
stabilise the training process.
The inception module allows the network to capture information at different scales. The inception
module is made up of four paths:
1x1 convolution: This path applies a 1x1 convolution to the input. This reduces the number of
channels in the input, which helps to reduce the computational complexity of the network.
3x3 convolution: This path applies a 3x3 convolution to the input. This is a standard
convolutional operation that is used to extract features from the input image.
30
5x5 convolution: This path applies a 5x5 convolution to the input. This path is used to capture
larger-scale features from the input image.
Max pooling: This path applies a max pooling operation to the input. This operation reduces
the size of the input by keeping the maximum value in each 2x2 window.
The outputs of the four paths are then concatenated together, and a 1x1 convolution is applied to the
result. This final 1x1 convolution helps to reduce the number of channels in the output, and also helps
to improve the accuracy of the network.
Global Average Pooling performs an average operation across the Width and Height for each filter
channel separately. This reduces the feature map to a vector that is equal to the size of the number of
channels. The output vector captures the most prominent features by summarizing the activation of
each channel across the entire feature map.
In GoogLeNet architecture, replacing fully connected layers with global average pooling improved
the top-1 accuracy by about 0.6%. In GoogLeNet, global average pooling can be found at the end of
the network, where it summarises the features learned by the CNN and then feeds it directly into the
SoftMax classifier.
ResNet (Residual Network), developed by Microsoft, addressed the problem of vanishing gradients in
very deep networks by introducing residual connections or skip connections. These connections allow
the network to learn residual mappings instead of direct mappings, making it easier to train networks
with hundreds of layers. A residual block typically consists of a few convolutional layers with a direct
addition of the input to the output. ResNet-50, ResNet-101, and ResNet-152 are widely used models
that balance depth, accuracy, and training feasibility.
ResNet, short for Residual Network is a specific type of neural network that was introduced in 2015
by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun in their paper “Deep Residual Learning
31
for Image Recognition”.The ResNet models were extremely successful which you can guess from the
following:
Won 1st place in the ILSVRC 2015 classification competition with a top-5 error rate of
3.57% (An ensemble model)
Won the 1st place in ILSVRC and COCO 2015 competition in ImageNet Detection,
ImageNet localization, Coco detection and Coco segmentation.
Replacing VGG-16 layers in Faster R-CNN with ResNet-101. They observed relative
improvements of 28%
Efficiently trained networks with 100 layers and 1000 layers also.
Error % for 56-layer is more than a 20-layer network in both cases of training data as well as testing
data. This suggests that with adding more layers on top of a network, its performance degrades. This
could be blamed on the optimization function, initialization of the network and more importantly
vanishing gradient problem. You might be thinking that it could be a result of overfitting too, but here
the error% of the 56-layer network is worst on both training as well as testing data which does not
happen when the model is overfitting.
Residual Block
This problem of training very deep networks has been alleviated with the introduction of ResNet or
residual networks and these Resnets are made up from Residual Blocks.
The very first thing we notice to be different is that there is a direct connection which skips some
layers(may vary in different models) in between. This connection is called ’skip connection’ and is
the core of residual blocks. Due to this skip connection, the output of the layer is not the same now.
Without using this skip connection, the input ‘x’ gets multiplied by the weights of the layer followed
by adding a bias term.
Next, this term goes through the activation function, f() and we get our output as H(x).
H(x)=f( wx + b )
or H(x)=f(x)
Now with the introduction of skip connection, the output is changed to
H(x)=f(x)+x
There appears to be a slight problem with this approach when the dimensions of the input vary from
that of the output which can happen with convolutional and pooling layers. In this case, when
dimensions of f(x) are different from x, we can take two approaches:
The skip connection is padded with extra zero entries to increase its dimensions.
The projection method is used to match the dimension which is done by adding 1×1
convolutional layers to input. In such a case, the output is:
H(x)=f(x)+w1.x
32
connections help is by allowing the model to learn the identity functions which ensures that the higher
layer will perform at least as good as the lower layer, and not worse. Let me explain this further.
Shallow network and a deep network that maps an input ‘x’ to output ’y’ by using the function H(x).
Deep network to perform at least as good as the shallow network and not degrade the performance as
in case of plain neural networks(without residual blocks). One way of achieving so is if the additional
layers in a deep network learn the identity function and thus their output equals inputs which do not
allow them to degrade the performance even with extra layers.
It has been seen that residual blocks make it exceptionally easy for layers to learn identity functions. It
is evident from the formulas above. In plain networks the output is
H(x)=f(x),
So to learn an identity function, f(x) must be equal to x which is grader to attain whereas incase of
ResNet, which has output:
H(x)=f(x)+x,
f(x)=0
H(x)=x
In the best-case scenario, additional layers of the deep neural network can better approximate the
mapping of ‘x’ to output ‘y’ than it’s the shallower counterpart and reduces the error by a significant
margin. ResNet to perform equally or better than the plain deep neural networks.
Using ResNet has significantly enhanced the performance of neural networks with more layers and
here is the plot of error% when comparing it with neural networks with plain layers.
Clearly, the difference is huge in the networks with 34 layers where ResNet-34 has much lower
error% as compared to plain-34. Also, we can see the error% for plain-18 and ResNet-18 is almost the
same.
Network Architecture: This network uses a 34-layer plain network architecture inspired by VGG-19
in which then the shortcut connection is added. These shortcut connections then convert the
architecture into a residual network.
33
Each of these architectures has become a benchmark model in the deep learning community and is
widely used for various applications. VGG is favored for simplicity, GoogleNet for efficiency, and
ResNet for its ability to scale to deep architectures.
popular CNN architectures like VGG, GoogleNet, and ResNet have played a pivotal role in advancing
computer vision. They provide powerful building blocks for designing custom models and are often
used as feature extractors or pretrained backbones in modern deep learning applications.
Dropout
Dropout is a regularization technique used in training deep neural networks to prevent overfitting.
Overfitting occurs when a model learns the training data too well, including its noise and minor
fluctuations, leading to poor performance on new, unseen data. Dropout addresses this by randomly
“dropping out” or deactivating neurons during the training process.
The core idea of dropout is simple: during each forward pass in training, every neuron in a given layer
has a probability ppp of being temporarily ignored (i.e., its output is set to zero). The typical value of
ppp is around 0.5 for hidden layers. This means that at each training step, the model effectively
samples from a different architecture. By doing this, dropout prevents neurons from co-adapting too
strongly to each other, which encourages the network to learn more robust and generalized features.
For example, in a fully connected layer with 100 neurons and a dropout rate of 0.5, only about 50
neurons will be active during any single forward pass. However, during testing or inference, dropout
is turned off, and the outputs of all neurons are used. To maintain consistency between training and
inference, the outputs are typically scaled down at inference time, or equivalently, scaled up during
training. This ensures that the expected total activation remains constant.
When data scientists apply dropout to a neural network, they consider the nature of this random
processing. They make decisions about which data noise to exclude and then apply dropout to the
different layers of a neural network as follows:
34
Input layer. This is the top-most layer of artificial intelligence (AI) and machine learning
where the initial raw data is being ingested. Dropout can be applied to this layer of visible
data based on which data is deemed to be irrelevant to the business problem being worked on.
Intermediate or hidden layers. These are the layers of processing after data ingestion. These
layers are hidden because we can't exactly see what they do. The layers, which could be one
or many, process data and then pass along intermediate -- but not final -- results that they send
to other neurons for additional processing. Because much of this intermediate processing will
end up as noise, data scientists use dropout to exclude some of it.
Output layer. This is the final, visible processing output from all neuron units. Dropout is not
used on this layer.
Here's another real-world example that shows how dropout works: A biochemical company wants to
design a new molecular structure that will enable it to produce a revolutionary form of plastic. The
company already knows the individual elements that will comprise the molecule. What it doesn't
know is the correct formulation of these elements.
To save time and processing, the company develops a neural network that can evaluate troves of
worldwide research, but that will only ingest and process research that directly refers to the molecule
and its identified elements. Any other information is automatically excluded as irrelevant and is
dropped out. By excluding irrelevant data upfront, this biochemical company's AI model avoids a
phenomenon known as overfitting. Overfitting occurs when an AI model tries to predict a trend from
data that's too noisy, because extraneous data wasn't dropped out at the beginning of the process.
Dropout can be applied to different parts of the network, but it is most effective when used in the fully
connected layers, especially those near the output. It is less common in convolutional layers, where
spatial features are crucial and redundancy is beneficial. However, variations like SpatialDropout are
used to drop entire feature maps in convolutional networks.
35
Dropout is easy to implement and has been shown to significantly improve generalization
performance. It is especially useful when the training dataset is small or when the model is large and
prone to overfitting. However, dropout also introduces stochasticity, which can make training noisy
and may require more epochs to converge.
In summary, dropout is a powerful and efficient regularization technique that improves model
generalization by preventing over-reliance on specific neurons. It helps neural networks learn diverse
and robust internal representations, making them perform better on unseen data.
Normalization
Normalization in deep learning refers to techniques that standardize the input features or internal
activations in a network. The primary goal is to stabilize and accelerate training, reduce the sensitivity
to weight initialization, and improve the model’s generalization ability.
One of the most widely used normalization techniques is Batch Normalization (BatchNorm).
Introduced in 2015, BatchNorm normalizes the input of each layer to have zero mean and unit
variance across the mini-batch. This helps mitigate the problem of internal covariate shift, where the
distribution of activations changes during training due to updates in earlier layers.
To address some of these issues, other normalization techniques have been developed:
Layer Normalization: Normalizes across features, useful for RNNs and Transformers.
Instance Normalization: Normalizes each example independently, used in style transfer.
Group Normalization: Divides channels into groups and normalizes within each group.
Input normalization (e.g., scaling pixel values between 0 and 1 or standardizing images with mean
and variance) is also critical before feeding data into the network.
In conclusion, normalization is an essential part of modern deep learning architectures. By ensuring
that the inputs to each layer are properly scaled, normalization improves convergence, stability, and
generalization of CNNs and other deep models.
36
Data augmentation
Data augmentation is a strategy used to artificially expand the size and diversity of a training dataset
by generating new samples through random transformations of the original data. It is particularly
important in deep learning for image classification, where large labeled datasets are required to train
models effectively.
The primary purpose of data augmentation is to improve the model’s ability to generalize to new,
unseen data. By exposing the network to a wider variety of input conditions, it becomes more robust
to variations in real-world data, such as changes in lighting, orientation, or scale.
1. Geometric Transformations
Geometric transformations alter the spatial properties of an image. It include:
Rotation: It rotate the image to a certain angle like 90° or 180°.
Flipping: It flips the image horizontally or vertically.
Scaling: Helps in zooming in or out in image.
Translation: Shifting the image along the x or y axis.
Shearing: Slanting the shape of the image.
3. Kernel Filters
Kernel filters apply convolutional operations to enhance or suppress specific features in the image. It
includes:
Blurring: Applying Gaussian blur to smooth the image.
Sharpening: Enhancing the edges to make the image sharper.
Edge Detection: Highlighting the edges in the image using filters like Sobel or Laplacian.
4. Random Erasing
Random erasing involves randomly masking out a rectangular region of the image. This helps the
model become invariant to occlusions and improves its ability to handle missing parts of objects.
5. Combining Augmentations
In this multiple augmentation techniques are combined to create more varied training data. For
example an image might be rotated, flipped and then have its brightness adjusted in a single
augmentation pipeline.
37
In modern frameworks like TensorFlow and PyTorch, data augmentation is implemented using
libraries such as ImageDataGenerator, [Link], or Albumentations. For example:
from [Link] import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
zoom_range=0.2
)
Data augmentation can be performed offline (by pre-processing and storing augmented images) or on-
the-fly (during training using real-time augmentation pipelines). The latter is more flexible and
memory-efficient.
In recent advances, advanced augmentation techniques like Mixup (combining two images and their
labels) and CutMix (mixing image patches) have shown significant improvements in training
robustness and accuracy.
However, overuse of augmentation can lead to noisy data, and improper augmentations can distort
critical features, especially in sensitive domains like biomedical images.
Data augmentation is a powerful technique to enhance model performance, increase data diversity,
and prevent overfitting. It enables deep learning models to learn more general and resilient features by
simulating real-world variations during training.
Data augmentation is a technique for expanding and diversifying datasets particularly in image
processing. By applying various transformations to existing data we can create new training examples
that help improve model generalization, reduce overfitting and enhance robustness.
38
UNIT-III
Recurrent Neural Networks (RNNs) are a class of artificial neural networks designed for processing
sequential data. Unlike feedforward neural networks, RNNs have loops that allow information to
persist, making them suitable for tasks where the order of inputs matters.
Recurrent Neural Networks (RNNs) are a special class of artificial neural networks designed to handle
sequential data, where the order of inputs carries significant meaning. Unlike traditional feedforward
networks, which assume inputs are independent of each other, RNNs are built to capture temporal
dependencies through loops in their architecture. These loops allow information to persist across time
steps, meaning that the output at a given time can depend not only on the current input but also on
inputs seen previously. This internal memory mechanism makes RNNs highly effective for tasks
involving time-series data, natural language, audio signals, and more.
At the core of an RNN is the recurrent cell, which receives both the current input and the hidden state
from the previous time step, processes them through a nonlinear transformation (often a tanh or ReLU
function), and produces a new hidden state. This updated hidden state is then passed on to the next
time step, creating a chain-like structure across the temporal sequence. Because of this design, RNNs
can learn complex patterns that evolve over time, making them suitable for a variety of applications.
In natural language processing (NLP), RNNs are employed for language modeling, machine
translation, text generation, and sentiment analysis. In speech recognition, they are used to analyze
audio waveforms over time to produce text transcriptions. In financial and environmental domains,
RNNs are widely applied in forecasting tasks, such as predicting stock prices or weather conditions.
Their versatility also extends to fields like video analysis, where RNNs can interpret frame sequences,
and music generation, where they can learn and generate rhythmic patterns. However, standard RNNs
have limitations in modeling long-term dependencies, which led to the development of more
advanced architectures like LSTM and GRU.
39
RNNs, on the other hand, are designed to recognize patterns in sequences of data. They have a
“memory” that captures information about what has been calculated so far. This memory allows
RNNs to use prior inputs to influence the current output.
Example: Consider the idiom “feeling under the weather., which is commonly used when someone is
ill, to aid us in the explanation of RNNs. In order for the idiom to make sense, it needs to be expressed
in that specific order. An RNN can understand this sequence because it processes each word in the
context of the previous words.
Looking at the visuals above, the “rolled” visual of the RNN represents the whole neural network, or
rather the entire predicted phrase, like “feeling under the weather.” The “unrolled” visual represents
the individual layers, or time steps, of the neural network. Each layer maps to a single word in that
phrase, such as “weather”. Prior inputs, such as “feeling” and “under”, would be represented as a
hidden state in the third timestep to predict the output in the sequence, “the”.
Challenges in RNNs
While RNNs are powerful, they come with their own set of challenges, primarily the exploding and
vanishing gradient problems. These issues are defined by the size of the gradient, which is the slope of
the loss function along the error curve.
When the gradient is too small, it continues to become smaller, updating the weight
parameters until they become insignificant — i.e. 0. When that occurs, the algorithm is no
longer learning.
Exploding gradients occur when the gradient is too large, creating an unstable model. In this
case, the model weights will grow too large, and they will eventually be represented as NaN.
One solution to these issues is to reduce the number of hidden layers within the neural network,
eliminating some of the complexity in the RNN model.
40
RNN Architectures
RNNs can be structured in various ways to suit different tasks:
41
(left image) X₁, X₂, X₃ sequence will be passed through RNN and we would get Y₁, Y₂, Y₃. But we
care only about the final output Y, not the intermediate outputs. (right image) we need the sequence
output from RNN1 so that it can become the sequential input given inside RNN2.
Applications of RNNs
Variants of RNNs
42
2. Long Short-Term Memory Networks (LSTM)
LSTMs are designed to handle the vanishing gradient problem by introducing a memory cell that can
maintain information over long periods.
Components:
Cell State: Stores long-term dependencies.
Gates:
— — → Input Gate: Controls how much new information flows into the cell.
— — → Forget Gate: Decides what information to discard from the cell.
— — → Output Gate: Determines what information to output.
Use-Case: Ideal for tasks requiring long-term memory, like essay writing or speech recognition.
Applications
1. Natural Language Processing (NLP):
o Text generation
o Language modeling
o Machine translation
2. Speech Recognition:
o Temporal dependency modeling in audio signals
3. Time-Series Forecasting:
o Predicting stock prices, weather patterns, etc.
4. Video Analysis:
o Activity recognition in video frames
5. Music Generation:
o Learning and generating melodies and sequences
o
Back propagation through time (BPTT)
BPTT is the extension of the backpropagation algorithm used to train RNNs. It unfolds the network
through time and applies standard backpropagation.
Backpropagation through time (BPTT) is a method used in recurrent neural networks (RNNs) to train
the network by backpropagating errors through time. In a traditional feedforward neural network, the
data flows through the network in one direction, from the input layer through the hidden layers to the
output layer. However, in RNNs, there are connections between nodes in different time steps, which
means that the output of the network at one time step depends on the input at that time step as well as
the previous time steps.
43
BPTT works by unfolding the RNN over time, creating a series of interconnected feedforward
networks. Each time step corresponds to one layer in this unfolded network, and the weights between
layers are shared across time steps. The unfolded network can be thought of as a very deep
feedforward network, where the weights are shared across layers.
During training, the error is backpropagated through the unfolded network, and the weights are
updated using gradient descent. This allows the network to learn to predict the output at each time
step based on the input at that time step as well as the previous time steps.
However, BPTT has some challenges, such as the vanishing gradient problem, where the gradients
become very small as they propagate back in time, making it difficult to learn long-term
dependencies. To address this issue, various modifications of BPTT have been proposed, such as
truncated backpropagation through time and gradient clipping.
Uses of BPTT:
BPTT is a widely used technique for training recurrent neural networks (RNNs) that can be used for
various applications such as speech recognition, language modeling, and time series prediction. Here
are some specific use cases for BPTT:
Speech recognition: BPTT can be used to train RNNs for speech recognition tasks, where the
network takes in a sequence of audio samples and predicts the corresponding text. BPTT allows the
network to learn the temporal dependencies in the audio signal and use them to make accurate
predictions.
Language modeling: BPTT can also be used to train RNNs for language modeling tasks, where the
network predicts the probability distribution of the next word in a sequence given the previous words.
This can be useful for applications such as text generation and machine translation.
44
Time series prediction: BPTT can be used to train RNNs for time series prediction tasks, where the
network takes in a sequence of data points and predicts the next value in the sequence. BPTT allows
the network to learn the temporal dependencies in the data and use them to make accurate predictions.
Overall, BPTT is a powerful tool for training RNNs to model sequential data, and it has been applied
successfully to a wide range of applications in various fields such as speech recognition, natural
language processing, and finance.
Example of BPTT:
Let’s consider a simple example of using BPTT to train a recurrent neural network (RNN) for time
series prediction. Suppose we have a time series dataset that consists of a sequence of data points:
{x1, x2, x3, …, xn}. The goal is to train an RNN to predict the next value in the sequence, xn+1,
given the previous values in the sequence.
To do this, use BPTT to backpropagate errors through time and update the weights of the RNN.
Here’s how the BPTT algorithm might work:
During training, the weights of the RNN are updated based on the gradients computed by
backpropagating the errors through time. This allows the RNN to learn the temporal dependencies in
the data and make accurate predictions for the next value in the sequence.
Overall, BPTT is a powerful technique for training RNNs to model sequential data, and it has been
successfully applied to a wide range of applications in various fields.
Limitation of BPTT:
While backpropagation through time (BPTT) is a powerful technique for training recurrent neural
networks (RNNs), it has some limitations:
Computational complexity: BPTT requires computing the gradient at each time step, which can be
computationally expensive for long sequences. This can lead to slow training times and may require
specialized hardware to train large-scale models.
Vanishing gradients: BPTT is prone to the problem of vanishing gradients, where the gradients
become very small as they propagate back in time. This can make it difficult to learn long-term
dependencies, which are important for many sequential data modeling tasks.
Exploding gradients: On the other hand, BPTT is also prone to the problem of exploding gradients,
where the gradients become very large as they propagate back in time. This can lead to unstable
training and can cause the weights of the network to become unbounded, resulting in NaN values.
45
Memory limitations: BPTT requires storing the activations of each time step, which can be memory-
intensive for long sequences. This can limit the size of the sequence that can be processed by the
network.
Training Recurrent Neural Networks involves a specialized version of the backpropagation algorithm
called Backpropagation Through Time (BPTT). This technique is designed to handle the sequential
nature of RNNs by unfolding the network over time, effectively transforming it into a deep
feedforward network with one layer per time step. Each layer in the unrolled network shares the same
parameters but processes different inputs from the sequence. During the forward pass, the network
processes the input sequence one element at a time, computing the hidden states and outputs
sequentially. The total loss for the entire sequence is calculated as the sum of the individual losses at
each time step.
In the backward pass, gradients are computed by backpropagating errors from the final time step to
the initial one. This involves applying the chain rule repeatedly across time steps, which enables the
network to learn how past inputs influence future outputs. However, this process is computationally
intensive and memory-consuming, especially for long sequences. One major challenge with BPTT is
that gradients can either vanish or explode due to repeated multiplications by the same weight
matrices. When gradients become too small, the network struggles to learn long-term dependencies,
while excessively large gradients can destabilize the learning process. These problems are known as
the vanishing and exploding gradient problems, respectively. Despite its limitations, BPTT remains a
fundamental method for training RNNs, and various optimizations and alternative architectures have
been proposed to mitigate its drawbacks.
1. Forward Pass:
o Inputs are passed through the network sequentially to compute outputs and hidden
states.
2. Unrolling the RNN:
o The RNN is unrolled into a feedforward network where each time step is treated as a
separate layer.
3. Loss Calculation:
o The total loss is computed as the sum over all time steps:
1. Backward Pass:
o Gradients are computed with respect to weights using chain rule, backpropagating
errors from last time step to the first.
Challenges:
Vanishing and Exploding Gradients: Gradients can become too small or too large due to
long time dependencies.
Computational Cost: Unrolling increases the computation and memory complexity.
Vanishing Gradient
When using gradient descent in RNNs, gradients are multiplied by the same weight matrices
multiple times.
If weights have eigenvalues < 1, gradients shrink exponentially → vanishing gradients.
This leads to poor learning of long-term dependencies.
46
The vanishing gradient problem is a significant issue encountered during the training of deep
neural networks, especially in Recurrent Neural Networks when learning long-term
dependencies. It occurs when gradients used in backpropagation become exceedingly small as
they are propagated backward through many layers or time steps. In the context of RNNs, this
problem is exacerbated because the same weight matrix is used repeatedly during the
unfolding process. As the gradients are calculated by multiplying several derivatives across
time, they tend to shrink exponentially if the weights are less than one in magnitude.
Mathematically, the gradients involve products of partial derivatives of the hidden states, and
when these derivatives are small, the product becomes even smaller over multiple time steps.
As a result, the influence of earlier inputs on the current output becomes negligible, and the
network effectively “forgets” earlier information. This severely limits the ability of standard
RNNs to learn dependencies that span over long intervals. For instance, in language
modeling, where understanding the context of a word might require remembering a subject
several words earlier, vanishing gradients prevent the model from learning meaningful
patterns. This issue also hampers the learning of temporal structures in time-series forecasting
and sequential decision-making. To combat the vanishing gradient problem, several
techniques have been introduced, including using activation functions like ReLU that
maintain stronger gradients, applying careful weight initialization, and introducing
architectural changes like LSTM and GRU, which explicitly preserve gradients over long
sequences. These advanced models address the limitations by allowing better gradient flow
during training, making them more suitable for tasks requiring memory over long sequences.
47
Illustrating saturation region and vanishing gradient problem (derivative close to 0) for a
Sigmoid activation function
Activation functions like sigmoid and hyperbolic tangent (tanH) have saturated regions and
are more prone to vanishing gradient problems in DNN training. The use of activation
functions like ReLU and its variants can alleviate the vanishing gradient problem since they
do not saturate for positive inputs. The derivative of ReLu is either 0 or 1. During
backpropagation, when gradients are multiplied several times to obtain the gradients of the
lower layers, ReLU derivatives has a nice property of being 0 or 1, instead of vanishing,
leading to a more effective and faster training.
Mathematical Insight:
Given:
Consequences:
Earlier layers learn very slowly.
Model forgets early sequence information.
Solutions:
Use of LSTM, GRU architectures
Gradient clipping
Better initialization and normalization
48
Exploding Gradient problem
In this problem, the gradients of the network’s cost function grow exponentially during training.
When the gradient values become excessively large, they can cause large update to the weights; the
weights can become NaN (not a number), or infinity, leading to numerical instability.
Similar to vanishing gradient, the issue of exploding gradient occurs more often when the tanh or
sigmoid activation function is used in the hidden layers, since the output of these activations tends to
be concentrated towards the extreme ends of the curve (0 or 1 for the sigmoid, or -1 and 1 for the
tanh). The exploding gradient problem is particularly pronounced in deep networks with many layers,
where the gradients are computed using the chain rule and can accumulate multiplicatively.
Following techniques are commonly used to prevent the exploding gradient problem, including:
Gradient clipping: This technique involves clipping the gradients during backpropagation to
ensure that they do not exceed a specified threshold
Weight regularization: Adding a regularization term to the loss function can help to prevent
the weights from becoming too large
Proper weight initialization: Choosing the appropriate strategy for initializing the weights
can help prevent gradients from exploding at the start of training
Gradient clipping
One common solution to the exploding gradient problem in RNNs is gradient clipping. When
gradients become excessively large during backpropagation, they can destabilize the learning process,
leading to numerical errors or divergence. Gradient clipping involves setting a threshold and scaling
the gradients when their norm exceeds this threshold. By rescaling large gradients to fall within a
manageable range, the model avoids large parameter updates that could corrupt learning. This
technique is particularly helpful in stabilizing the training of deep and recurrent models and is often
used alongside other mechanisms like dropout and normalization.
Gradient Clipping is a technique used during the training of neural networks to address the issue of
exploding gradients. When the gradients of the loss function concerning the parameters become too
large, it can cause the model's weights to be updated by huge amounts, leading to numerical instability
and a slow or even halted convergence of the training process. By using Gradient Clipping, we can
maintain numerical stability by preventing the gradients from growing too large, thus improving the
model's overall performance.
Gradient clipping is a very effective technique that helps address the exploding gradient problem
during training. By limiting the magnitude of the gradients, it helps to prevent them from growing
unchecked and becoming too large. This ensures that the model learns more effectively and prevents
it from getting stuck in a local minima. The clip value or clip threshold is an important parameter that
determines how aggressively the gradients are scaled down.
1. CalculateGradients:
When the model is learning, it's like a student taking an exam. Backpropagation is like a
teacher grading the exam and giving feedback to the student. It calculates the gradients of the
model's parameters with respect to the loss function, helping the model learn and improve its
performance. So, think of backpropagation as a helpful teacher guiding the model to success!
2. ComputeGradientNorm:
To measure the magnitude of the gradients, we can use different types of norms such as the
L2 norm (also known as the Euclidean norm) or the L1 norm. These norms help us to
quantify the size of the gradients and understand how fast the parameters are changing. The
L2 norm calculates the square root of the sum of the squares of the individual gradients, while
the L1 norm calculates the sum of the absolute values of the gradients. By measuring the
49
norm of the gradients, we can monitor the training process and adjust the learning rate
accordingly to ensure that the model is converging efficiently.
3. Clip Gradients:
If the computed gradient norm exceeds the predefined clip threshold, the gradients are scaled
down to ensure that the norm does not exceed this threshold. The scaling factor is determined
by dividing the clip threshold by the gradient norm.
[Link]:
The clipped gradients are used to update the model parameters. By using the clipped gradients
to update the model parameters, we can prevent the weights from being updated by
excessively large amounts, which can lead to numerical instability and slow down the training
process. This helps to ensure that the model is learning effectively and converging towards a
good solution.
The clip_threshold discussed here is a type of hyperparameter whose value could be determined by
experimenting on the dataset present in front of us.
LSTMs are designed to overcome the vanishing gradient problem and model long-term dependencies.
To address the vanishing gradient problem and enable the modeling of long-term dependencies, Long
Short-Term Memory (LSTM) networks were developed. LSTMs are a type of RNN with a more
sophisticated internal structure that includes memory cells and gating mechanisms. Each LSTM unit
maintains a cell state that acts as a conveyor belt of information, allowing data to flow through many
time steps with minimal modification. The cell state is regulated by three gates: the forget gate, input
gate, and output gate. The forget gate determines what information to discard from the cell state, the
input gate controls what new information is added, and the output gate decides what part of the cell
state should be output as the hidden state. These gates use sigmoid activation functions to make soft
decisions, which are then used to update the memory in a controlled manner.
LSTMs overcome the vanishing gradient problem by providing paths through which gradients can
flow unchanged over many time steps. As a result, they can capture both short-term and long-term
dependencies effectively. This makes them highly effective in complex sequence modeling tasks such
as machine translation, handwriting recognition, and video analysis. Although they are
computationally more expensive than basic RNNs due to their more complex structure, the
performance gains in terms of learning long-range relationships often justify their use.
50
Architecture
Vanishing Gradient: When training a model over time, the gradients which help the model
learn can shrink as they pass through many steps. This makes it hard for the model to learn
long-term patterns since earlier information becomes almost irrelevant.
Exploding Gradient: Sometimes gradients can grow too large causing instability. This
makes it difficult for the model to learn properly as the updates to the model become erratic
and unpredictable.
Both of these issues make it challenging for standard RNNs to effectively capture long-term
dependencies in sequential data.
LSTM Architecture
LSTM architectures involves the memory cell which is controlled by three gates:
1. Input gate: Controls what information is added to the memory cell.
2. Forget gate: Determines what information is removed from the memory cell.
3. Output gate: Controls what information is output from the memory cell.
This allows LSTM networks to selectively retain or discard information as it flows through the
network which allows them to learn long-term dependencies. The network has a hidden state which is
like its short-term memory. This memory is updated using the current input, the previous hidden state
and the current state of the memory cell.
51
Working of LSTM
LSTM architecture has a chain structure that contains four neural networks and different memory
blocks called cells.
52
Advantages
Solves vanishing gradient problem
Learns both long-term and short-term patterns
Applications of LSTM
Some of the famous applications of LSTM includes:
Language Modeling: Used in tasks like language modeling, machine translation and text
summarization. These networks learn the dependencies between words in a sentence to
generate coherent and grammatically correct sentences.
Speech Recognition: Used in transcribing speech to text and recognizing spoken commands.
By learning speech patterns they can match spoken words to corresponding text.
Time Series Forecasting: Used for predicting stock prices, weather and energy consumption.
They learn patterns in time series data to predict future events.
Anomaly Detection: Used for detecting fraud or network intrusions. These networks can
identify patterns in data that deviate drastically and flag them as potential anomalies.
Recommender Systems: In recommendation tasks like suggesting movies, music and books.
They learn user behavior patterns to provide personalized suggestions.
Video Analysis: Applied in tasks such as object detection, activity recognition and action
classification. When combined with Convolutional Neural Networks (CNNs) they help
analyze video data and extract useful information.
Gated Recurrent Units (GRUs) are a streamlined variant of the LSTM designed to achieve similar
performance with a simpler architecture and fewer parameters. Introduced as an alternative to LSTM,
the GRU aims to reduce computational complexity while retaining the ability to capture long-term
dependencies in sequential data. Unlike LSTMs, which maintain a separate cell state and hidden state,
GRUs merge these into a single state vector. They employ two gates: the update gate and the reset
gate. The update gate functions similarly to a combination of the forget and input gates in LSTM,
determining the proportion of past information to retain and new information to add. The reset gate
decides how much of the previous hidden state to forget when computing the new state.
Gated Recurrent Units (GRUs) are a type of RNN introduced by Cho et al. in 2014. The core idea
behind GRUs is to use gating mechanisms to selectively update the hidden state at each time step
allowing them to remember important information while discarding irrelevant details. GRUs aim to
simplify the LSTM architecture by merging some of its components and focusing on just two main
gates: the update gate and the reset gate.
53
Structure of GRU:
1. Update Gate (zt): This gate decides how much information from previous hidden state
should be retained for the next time step.
2. Reset Gate (rt): This gate determines how much of the past hidden state should be forgotten.
These gates allow GRU to control the flow of information in a more efficient manner compared to
traditional RNNs which solely rely on hidden state.
This simplicity allows GRUs to train faster and require less memory while often achieving
comparable results to LSTMs. The update mechanism enables the network to decide whether to carry
forward previous state information or overwrite it with new data, thus maintaining an effective
balance between short-term and long-term memory. GRUs have been successfully applied in many
areas, including speech recognition, machine translation, and time-series forecasting. In particular,
they are favored in scenarios where computational efficiency is critical, such as on-device processing
and real-time inference systems. Despite their simpler architecture, GRUs often perform nearly as
well as LSTMs and sometimes even outperform them, depending on the specific dataset and task.
GRU is a simplified version of LSTM that combines the forget and input gates into a single update
gate.
54
How GRUs Solve the Vanishing Gradient Problem
Like LSTMs, GRUs were designed to address the vanishing gradient problem which is common in
traditional RNNs. GRUs help mitigate this issue by using gates that regulate the flow of gradients
during training ensuring that important information is preserved and that gradients do not shrink
excessively over time. By using these gates, GRUs maintain a balance between remembering
important past information and learning new, relevant data.
GRU vs LSTM
GRUs are more computationally efficient because they combine the forget and input gates into a
single update gate. GRUs do not maintain an internal cell state as LSTMs do, instead they store
information directly in the hidden state making them simpler and faster.
55
Advantages
Fewer parameters than LSTM
Comparable performance
Faster training
Use Cases
Real-time applications where efficiency is critical
Language modeling and machine translation
Bidirectional LSTMs
Bidirectional LSTM (BiLSTM) processes sequences in both forward and backward directions,
capturing both past and future contexts.
Bidirectional Long Short-Term Memory (BiLSTM) networks extend the capabilities of standard
LSTMs by allowing them to access both past and future context in a sequence. While traditional
LSTMs process data in one direction—typically from left to right—BiLSTMs consist of two parallel
LSTM layers: one processes the sequence forward, and the other processes it backward. At each time
step, the outputs from both directions are concatenated or combined to form a richer representation of
the sequence element.
This bidirectional processing is particularly beneficial for tasks where understanding the full context
of a sequence is important. For example, in named entity recognition, determining whether a word is a
person’s name might require information about both preceding and following words. Similarly, in
speech recognition or part-of-speech tagging, knowing what comes after a word can be just as
important as knowing what came before. BiLSTMs enable the model to use the complete context
around each time step to make better predictions. However, because BiLSTMs require access to the
entire sequence during training and inference, they are not suitable for real-time applications where
future inputs are not yet available. Nevertheless, for many offline tasks, BiLSTMs offer significant
improvements in accuracy and are widely used in state-of-the-art NLP and speech systems.
56
A Bidirectional LSTM (BiLSTM) consists of two separate LSTM layers:
Forward LSTM: Processes the sequence from start to end
Backward LSTM: Processes the sequence from end to start
The outputs of both LSTMs are then combined to form the final output. Mathematically, the final
output at time t is computed as:
Here:
Xi is the input token
Yi is the output token
A and A′ are Forward and backward LSTM units
The final output of Yi is the combination of A and A′ LSTM nodes.
Benefits
Richer contextual understanding
Useful in applications like:
o Named Entity Recognition (NER)
o Speech Recognition
o Text Classification
Limitations
Not suitable for real-time predictions (can’t wait for future inputs)
Bidirectional RNNs
Bidirectional Recurrent Neural Networks (BiRNNs) generalize the idea of bidirectional sequence
processing beyond LSTMs to any recurrent architecture, including basic RNNs and GRUs. In a
BiRNN, two separate RNNs are run in parallel: one processes the input sequence from start to end,
and the other processes it in reverse. The hidden states from both the forward and backward passes are
combined at each time step, typically through concatenation, to produce a context-rich representation
that includes information from both the past and the future.
57
This approach is especially powerful in tasks where contextual understanding from both directions
enhances performance. For example, in machine translation, knowing the full sentence helps in
choosing the correct translation for a word. Similarly, in document classification or sequence tagging,
future context often plays a role in understanding the meaning or function of a word. BiRNNs allow
models to make more informed decisions by considering both directions simultaneously. Like
BiLSTMs, BiRNNs are not suitable for tasks that require online or real-time processing since they
rely on having the complete sequence available. Nonetheless, they are widely used in offline settings
where bidirectional context can significantly improve the quality of sequence modeling.
In a traditional unidirectional RNN the network might struggle to understand whether "apple" refers
to the fruit or the company based on the first sentence. However a BRNN would have no such issue.
By processing the sentence in both directions, it can easily understand that "apple" refers to the fruit,
thanks to the future context provided by the second sentence ("It is very healthy.").
Structure
1. Inputting a Sequence: A sequence of data points each represented as a vector with the same
dimensionality is fed into the BRNN. The sequence may have varying lengths.
58
Backward direction: The hidden state at each time step is influenced by the current input and
the next hidden state.
3. Computing the Hidden State: A non-linear activation function is applied to the weighted
sum of the input and the previous hidden state creating a memory mechanism that allows the
network to retain information from earlier steps.
4. Determining the Output: A non-linear activation function is applied to the weighted sum of the
hidden state and output weights to compute the output at each step. This output can either be:
The final output of the network.
An input to another layer for further processing.
Advantages
Better context awareness
Suitable for tasks with fixed-length input/output
Applications
Part-of-speech tagging
Sentiment analysis
Machine translation
59
UNIT- IV
Generative models
Generative models are a class of machine learning models that are capable of generating new data
samples that resemble a given training dataset. Unlike discriminative models, which focus on learning
the boundary between classes and modeling the conditional probability P(y∣x), generative models aim
to learn the underlying distribution of the data P(x) itself. Once trained, they can produce entirely
new, realistic-looking samples, such as images, audio, or text, that could plausibly have come from
the original dataset. These models are especially valuable in unsupervised learning tasks where
labeled data is limited, and the goal is to understand or reproduce the structure of the input data.
Some classical generative models include Gaussian Mixture Models (GMMs), Hidden Markov
Models (HMMs), and Naive Bayes classifiers. More recently, deep generative models have gained
prominence, including Variational Autoencoders (VAEs), Generative Adversarial Networks (GANs),
and autoregressive models like PixelCNN and WaveNet. These models are trained using various
probabilistic or adversarial techniques to capture the data distribution. Generative models have a wide
range of applications, from image synthesis and text generation to semi-supervised learning, anomaly
detection, and drug discovery. Their ability to simulate realistic data makes them a powerful tool in
AI research and practical deployments.
Generative models are a core component of modern machine learning, particularly within the field of
unsupervised learning. These models aim to capture the underlying data distribution of a dataset,
allowing them to generate new data samples that are statistically similar to those seen during training.
Unlike discriminative models, which attempt to classify input data or predict labels, generative
models focus on modeling the input features themselves. This fundamental difference gives
generative models the unique ability to "imagine" or simulate new data that was not explicitly
provided.
To understand the importance of generative models, it's helpful to compare them with discriminative
models. Discriminative models, such as logistic regression or support vector machines, model the
conditional probability distribution P(y∣x), which helps in tasks like classification. On the other hand,
generative models attempt to learn the joint distribution P(x,y) or marginal distribution P(x). This
enables them not only to classify but also to generate samples, perform density estimation, and fill in
missing data. Essentially, generative models can "understand" the structure of data in a way that
discriminative models cannot.
Before the rise of deep learning, classical generative models were widely used in probabilistic
modeling. Examples include Naive Bayes, Gaussian Mixture Models (GMMs), and Hidden Markov
Models (HMMs). These models rely on strong statistical assumptions, such as feature independence
in Naive Bayes or Gaussian-distributed clusters in GMMs. Despite their simplicity, they were
effective in tasks like document classification, speech recognition, and sequence modeling. However,
their limited expressiveness made them less suitable for high-dimensional or complex data.
With the advent of deep learning, more expressive generative models have emerged that can handle
high-dimensional data like images, text, and audio. Among the most notable are Variational
Autoencoders (VAEs), Generative Adversarial Networks (GANs), and Autoregressive Models. These
models leverage neural networks to approximate complex distributions and have achieved impressive
60
results in synthesizing realistic images, generating coherent text, and mimicking human speech. Their
success lies in their ability to represent intricate data patterns without requiring explicit labels.
VAEs are a type of probabilistic autoencoder that learns a compressed latent representation of the
input data. During training, they optimize a loss function that balances the reconstruction error and a
regularization term (Kullback-Leibler divergence) that enforces the latent space to follow a known
prior distribution, typically Gaussian. Once trained, new data samples can be generated by sampling
from this latent space and decoding the samples back into the input space. VAEs are particularly
suited for tasks that require structured and continuous latent representations.
GANs are another class of powerful generative models introduced by Ian Goodfellow in 2014. A
GAN consists of two neural networks: a generator, which creates fake data samples, and a
discriminator, which tries to distinguish between real and fake samples. These two networks are
trained in a minimax game where the generator tries to fool the discriminator, and the discriminator
tries to detect fakes. This adversarial training leads to the generation of highly realistic outputs,
especially in image generation. However, GANs can be unstable to train and suffer from issues like
mode collapse.
Autoregressive models like PixelCNN, PixelRNN, and WaveNet model the probability of each data
point conditioned on previous data points. For example, in the case of PixelCNN, the model generates
images pixel by pixel, each conditioned on previously generated pixels. These models offer high-
quality sample generation and exact likelihood estimation but can be computationally expensive due
to their sequential nature. They are commonly used in text and audio generation, where the temporal
structure is essential.
Training generative models requires sophisticated optimization techniques. For instance, VAEs use
variational inference, while GANs rely on adversarial training, which involves a delicate balance
between the generator and the discriminator. Common challenges include mode collapse in GANs
(where the generator produces limited diversity), posterior collapse in VAEs (where the latent
variables become uninformative), and slow sampling in autoregressive models. Ongoing research
seeks to overcome these limitations through architectural innovations and improved training
objectives.
Generative models have found applications across a wide range of domains. In computer vision, they
are used for image generation, super-resolution, image inpainting, and style transfer. In natural
language processing, they power text generation, machine translation, and dialogue systems. In
healthcare, generative models assist in medical image synthesis, drug discovery, and disease
progression modeling. They are also used in anomaly detection, where the ability to model normal
data allows the detection of outliers or rare events.
As generative models continue to evolve, they promise to revolutionize creative industries, scientific
simulations, and human-computer interaction. However, their growing capabilities also raise ethical
concerns. For example, GANs can generate deepfakes, which pose challenges to privacy, security,
and misinformation. Responsible deployment requires careful consideration of fairness, transparency,
and accountability. Future research is also exploring multimodal generative models, self-supervised
learning, and hybrid models that combine the strengths of VAEs, GANs, and autoregressive methods.
61
Concept and principles of GANs
Generative Adversarial Networks (GANs) are a type of deep generative model introduced by Ian
Goodfellow in 2014. The fundamental idea of GANs is to train two neural networks—the generator
and the discriminator—simultaneously in a competitive setting. The generator’s goal is to produce
fake data that resembles real data, while the discriminator’s task is to distinguish between real and
fake data. This adversarial training setup forms the core principle of GANs and drives both networks
to improve iteratively.
The generator starts with a random input, often drawn from a Gaussian or uniform noise distribution,
and transforms it into a data sample (e.g., an image) that should look like it came from the real
dataset. The discriminator receives both real samples (from the training data) and fake samples (from
the generator) and outputs a probability indicating whether a given sample is real or fake. During
training, the discriminator tries to maximize its accuracy in distinguishing real from fake, while the
generator tries to fool the discriminator by generating increasingly realistic samples. This adversarial
game is formulated as a minimax optimization problem. When trained effectively, the generator
becomes proficient at modeling the true data distribution, and the discriminator becomes less able to
tell real and fake apart, ideally reaching a state where generated data is indistinguishable from real
data.
The architecture of GANs consists of two main components: the generator and the discriminator, both
typically implemented as deep neural networks. These networks play opposing roles in the training
process but are intrinsically linked, as the output of one serves as the input to the other.
GANs consist of two main models that work together to create realistic synthetic data which are as
follows:
1. Generator Model
The generator is a deep neural network that takes random noise as input to generate realistic data
samples like images or text. It learns the underlying data patterns by adjusting its internal parameters
during training through backpropagation. Its objective is to produce samples that the discriminator
classifies as real.
62
Generator Loss Function: The generator tries to minimize this loss:
where
JG measure how well the generator is fooling the discriminator.
G(zi) is the generated sample from random noise zizi
D(G(zi)) is the discriminator’s estimated probability that the generated sample is real.
The generator aims to maximize D(G(zi)) meaning it wants the discriminator to classify its fake data
as real (probability close to 1).
2. Discriminator Model
The discriminator acts as a binary classifier helps in distinguishing between real and generated data. It
learns to improve its classification ability through training, refining its parameters to detect fake
samples more accurately. When dealing with image data, the discriminator uses convolutional layers
or other relevant architectures which help to extract features and enhance the model’s ability.
GANs train by having two networks the Generator (G) and the Discriminator (D) compete and
improve together. Here's the step-by-step process
2. Discriminator's Turn
The discriminator receives two types of data:
Real samples from the actual training dataset.
Fake samples created by the generator.
D's job is to analyze each input and find whether it's real data or something G cooked up. It outputs a
probability score between 0 and 1. A score of 1 shows the data is likely real and 0 suggests it's fake.
3. Adversarial Learning
If the discriminator correctly classifies real and fake data it gets better at its job.
If the generator fools the discriminator by creating realistic fake data, it receives a positive
update and the discriminator is penalized for making a wrong decision.
4. Generator's Improvement
Each time the discriminator mistakes fake data for real, the generator learns from this success.
Through many iterations, the generator improves and creates more convincing fake samples.
5. Discriminator's Adaptation
The discriminator also learns continuously by updating itself to better spot fake data.
This constant back-and-forth makes both networks stronger over time.
6. Training Progression
As training continues, the generator becomes highly proficient at producing realistic data.
Eventually the discriminator struggles to distinguish real from fake shows that the GAN has
reached a well-trained state.
63
At this point, the generator can produce high-quality synthetic data that can be used for
different applications.
Types of GANs
There are several types of GANs each designed for different purposes. Here are some important types:
1. Vanilla GAN
Vanilla GAN is the simplest type of GAN. It consists of:
A generator and a discriminator both are built using multi-layer perceptrons (MLPs).
The model optimizes its mathematical formulation using stochastic gradient descent (SGD).
While foundational, Vanilla GANs can face problems like:
Mode collapse: The generator produces limited types of outputs repeatedly.
Unstable training: The generator and discriminator may not improve smoothly.
Working of CGANs:
A conditional variable (y) is fed into both the generator and the discriminator.
This ensures that the generator creates data corresponding to the given condition (e.g
generating images of specific objects).
The discriminator also receives the labels to help distinguish between real and fake data.
Example: Instead of generating any random image, CGAN can generate a specific object like a dog
or a cat based on the label.
64
Super-Resolution GAN (SRGAN) is designed to increase the resolution of low-quality images while
preserving details.
Working of SRGAN:
Uses a deep neural network combined with an adversarial loss function.
Enhances low-resolution images by adding finer details helps in making them appear sharper
and more realistic.
Helps to reduce common image upscaling errors such as blurriness and pixelation.
Discriminative and generative models differ fundamentally in their objectives and how they approach
learning from data. Discriminative models aim to learn the conditional probability P(y∣x)P(y|x)P(y∣x),
which is the probability of a label yyy given an input xxx. These models focus solely on drawing
decision boundaries that separate different classes, and they are typically used for classification and
regression tasks. Examples include logistic regression, support vector machines (SVMs), and
discriminative neural networks like CNNs and RNNs. Discriminative models tend to have better
performance in supervised learning settings because they are optimized directly for the prediction
task.
In contrast, generative models learn the joint probability distribution P(x,y) or the marginal
distribution P(x) alone. This means they aim to model how the data is generated, which allows them
to generate new data points. Generative models are capable of performing both unsupervised and
semi-supervised learning tasks. They can be used for data synthesis, density estimation, and imputing
missing data. Examples include Naive Bayes, Hidden Markov Models, Variational Autoencoders
(VAEs), and GANs. Although generative models may not always match the classification accuracy of
discriminative models, their ability to understand and reproduce the structure of the input data makes
them more flexible in creative and data-centric applications. In summary, discriminative models are
better suited for predictive accuracy, while generative models offer the advantage of data generation
and broader probabilistic reasoning.
65
Generative Adversarial Networks (GANs)
Generative Adversarial Networks (GANs) are a powerful class of generative models that learn to
synthesize new data samples through adversarial training. At their core, GANs involve a game
between two neural networks: the generator, which tries to create realistic data, and the discriminator,
which evaluates whether a sample is real or generated. The training objective is formulated as a
minimax game, where the generator seeks to minimize the discriminator’s ability to distinguish fake
data, and the discriminator seeks to maximize it.
The GAN training process begins with the generator producing synthetic data from random noise. The
discriminator is then presented with both real and fake data and must classify them correctly. The
discriminator’s feedback helps the generator learn to produce better-quality data over time. This
feedback loop continues until the generator produces data that is indistinguishable from real data.
Mathematically, the objective can be expressed as:
Training GANs can be tricky due to issues like mode collapse (where the generator outputs limited
variations), vanishing gradients, and unstable convergence. Techniques such as feature matching,
Wasserstein GANs (WGAN), and improved loss functions have been proposed to address these
challenges. Despite these difficulties, GANs have achieved remarkable success in generating high-
quality data across various domains.
66
Applications of GANs
Generative Adversarial Networks have found numerous applications across a wide range of fields,
owing to their exceptional ability to generate realistic and high-quality data. In the field of computer
vision, GANs are widely used for image synthesis, where they generate realistic images from random
noise or conditional inputs. This includes tasks like generating faces, animals, or landscapes. In
image-to-image translation, GANs can convert images from one domain to another, such as turning
sketches into photographs, black-and-white images into color, or day scenes into night scenes. Models
like CycleGAN and Pix2Pix are prominent examples.
GANs also play a crucial role in super-resolution, where low-resolution images are transformed into
high-resolution versions, enhancing image quality. In style transfer, GANs apply the artistic style of
one image to the content of another. In the medical domain, GANs are used to synthesize medical
images, augment datasets, and improve diagnostics through better data availability. They also assist in
anomaly detection by learning the distribution of normal data and identifying deviations.
Beyond vision, GANs are applied in natural language processing for tasks like text generation,
machine translation, and dialogue systems, although training GANs for discrete data like text poses
additional challenges. In music generation, GANs can compose new musical sequences in various
styles. In gaming and simulation, GANs are used to create realistic textures, environments, and
character animations. Additionally, in fields like cybersecurity, finance, and robotics, GANs are used
for synthetic data generation to improve model training and simulation fidelity. Overall, GANs
represent a transformative approach in machine learning, enabling creative and practical innovations
across disciplines.
Generative Adversarial Networks (GANs) are powerful tools for data augmentation. In domains like
medical imaging, where annotated data is scarce, GANs can synthesize realistic images. This helps
researchers build larger and more balanced datasets. The generated images closely resemble real data
and improve model generalization. In satellite imagery, GANs can simulate various terrains and
weather conditions. For NLP applications with image tasks, GANs can create matching visuals for
text content. This synthetic data supports better training for computer vision models. GAN-generated
datasets also help in rare-class recognition problems. Researchers use this approach to avoid
overfitting. It is a practical and scalable method to enrich datasets.
GANs, especially StyleGAN, can create ultra-realistic human face images. These faces often look
indistinguishable from real ones, despite not belonging to any actual person. These synthetic portraits
can be used in marketing, gaming, and entertainment. They help preserve privacy by avoiding the use
of real faces. Developers use them to create avatars for apps and virtual environments. Social media
platforms can generate default profile pictures using them. Researchers use face datasets generated
this way for facial recognition models. These synthetic faces can vary by age, gender, and ethnicity.
The diversity in generated images improves fairness in AI systems. Overall, it’s a powerful alternative
to collecting real human data.
67
3. Generate Realistic Photographs
GANs can synthesize natural scenes like mountains, beaches, or forests. These generated images look
lifelike and are useful for training machine learning models. They also help in content creation for
games and media. GANs like BigGAN are particularly good at high-resolution photo generation. This
technology reduces the need for expensive image datasets. Designers can use them for mockups or
testing visual interfaces. Training datasets for object detection and classification are often expanded
using GANs. This aids in improving accuracy and robustness. Researchers simulate lighting and
seasonal conditions using GANs. They offer endless variations without the need for manual
photography.
GANs can be trained on animation or cartoon datasets to produce stylized characters. These models
learn artistic features like exaggerated facial expressions and bright colors. The result is new, unique
cartoon characters that can be used in games or shows. Artists can use them as inspiration or baseline
designs. GANs simplify the animation pipeline by generating multiple character views. They can
replicate drawing styles from famous studios like Disney or Studio Ghibli. This application is popular
in mobile games and avatar generators. Content creators use them to personalize digital experiences.
They reduce manual work for illustrators. It’s a bridge between AI and creative design.
5. Image-to-Image Translation
Image-to-image translation involves converting an image from one domain to another. Examples
include converting a daytime scene into a nighttime version. CycleGAN and Pix2Pix are popular
GAN models for this task. You can also convert a photo into a sketch or painting. This is widely used
in photo filters and editing apps. GANs can also remove rain, snow, or blur from images. In urban
planning, they convert building blueprints into 3D renderings. Farmers use image translation for
analyzing crop health from aerial images. In medical imaging, it helps translate CT scans to MRI-style
visuals. It's a versatile tool for data enhancement.
6. Text-to-Image Translation
Text-to-image generation uses GANs to create visuals based on textual descriptions. Models like
AttnGAN and DALL·E are designed for this purpose. For example, given the text "a red bird with
blue wings," the model generates the matching image. This is useful in e-commerce for previewing
products from descriptions. Designers generate concept art from storyline prompts. In education, it
helps visualize complex topics. It also aids people with visual impairments by turning text into visual
content. In AI storytelling, scenes are auto-generated based on narrative. This bridges NLP and
computer vision domains. The creative possibilities are nearly endless.
7. Semantic-Image-to-Photo Translation
68
8. Face Frontal View Generation
GANs can reconstruct frontal views from side-angle face images. This is particularly useful in
improving facial recognition systems. It helps match faces taken from different angles, improving
verification accuracy. Surveillance systems benefit by converting partial face views into frontal ones.
GANs like FF-GAN specialize in such applications. It aids law enforcement and security by
reconstructing faces from video. Artists can also use it to complete portraits. In AR/VR, realistic face
views are needed for better immersion. It's also used in fashion tech for virtual try-ons. It makes
incomplete or obscure face data more usable. The output is consistent and realistic.
GANs can synthesize human figures in new or complex poses. They are trained using pose-estimation
data, like body keypoints. This helps animation and game industries create new movement styles.
Datasets like Human3.6M are used for training such models. GANs can generate gymnastic or dance
poses not in the training data. Sports simulations and biomechanics research benefit from this.
Medical rehab tools simulate human movement in new positions. The models maintain body
proportions and realistic textures. Augmented datasets with diverse poses enhance action recognition
models. It's especially helpful for 3D avatar creation and fitness apps.
GANs can convert real face photos into emojis or cartoon avatars. They learn mappings between
facial features and emoji expressions. Applications include personalized sticker packs for messaging
apps. EmojifyGAN and similar models are used in mobile tools. Users can create custom emoji sets
that resemble them. This adds emotional richness to digital communication. It’s a fun, user-friendly
application of deep learning. Some apps even animate these emojis in real-time using GANs. Artists
can prototype new emoji ideas quickly. It blends entertainment with personalization technology.
GANs can alter photographs in intelligent, realistic ways. You can change backgrounds, add or
remove objects, or adjust lighting. Tools like Deep Photo Style Transfer allow editing without pixel-
level changes. GANs can modify facial expressions or hairstyles in portraits. They are used in beauty
apps for real-time makeup simulation. Editors use them for smart content-aware fill. In movies, GANs
are used for seamless scene transitions. AI-powered photo retouching is now a standard in mobile
devices. The edits maintain high visual fidelity. GANs automate what used to be manual photo
manipulation.
Face aging GANs simulate how a person might look at different ages. Given a young face, the model
generates older versions while preserving identity. Applications include age progression for missing
person investigations. Apps like FaceApp use GANs for this purpose. It also finds use in healthcare
for predicting age-related changes. Entertainment industries use it for character aging in films.
Training data includes faces across various age groups. The transformation looks natural and smooth.
Researchers use this for longitudinal face recognition studies. It's a powerful visual storytelling tool. It
blends prediction with creativity.
69
GANs can blend two or more images into a single coherent picture. For example, merging a cat with a
dog to generate a hybrid animal. The model ensures smooth transitions between the merged areas. It’s
used in art generation and experimental design. Designers use blending for surreal visual
compositions. This is also useful in genetic simulations or concept design. GANs learn to preserve
structure while mixing style. Multiple face features can be blended to create new identities. It supports
fashion try-on by merging clothes and body images. The output remains photorealistic and coherent.
GANs like SRGAN can enhance low-resolution images. These models predict finer details that aren’t
present in the original image. They’re widely used in CCTV image enhancement. Super-resolution
GANs are used to restore old or damaged photos. Medical imaging applications benefit by enhancing
scans. Video streaming platforms use them to upscale content. It enables higher-quality printing from
poor-quality inputs. GANs preserve textures and edges during upscaling. The resulting images are
clearer and more detailed. It's a key tool in digital restoration and enhancement.
GANs can fill in missing or corrupted regions of an image. Given an incomplete image, the model
intelligently guesses the missing part. DeepFill and Contextual Attention GANs are popular for this
task. Applications include restoring torn or scratched photos. It’s useful in removing unwanted objects
from pictures. Museums use it for historical image restoration. In AR apps, inpainting fills gaps
caused by occlusions. GANs maintain color and texture consistency. Editors use it for content-aware
background fill. It adds realism to reconstructed visuals.
GANs can modify clothing style, color, or pattern in images. This is helpful in virtual fitting rooms or
fashion design apps. Models like FashionGAN translate one outfit into another style. A user can
visualize the same dress in different colors. Designers use it to generate new clothing combinations.
E-commerce platforms apply it for interactive previews. It reduces the need for physical trials.
Retailers simulate how an item looks across various demographics. The changes are rendered
realistically. It enhances user engagement in fashion platforms.
Video prediction models forecast future frames from past frames using GANs. This is useful in
robotics and autonomous driving. The model learns patterns of motion and change. It predicts how a
scene evolves, like a person walking or a car turning. Sports analytics use it to simulate future plays.
Healthcare systems use it to monitor patient movements. Security systems can anticipate suspicious
activities. GANs like PredNet are designed for this task. It supports real-time decision-making in
dynamic environments. Predictive accuracy improves with temporal consistency.
GANs can generate 3D models from 2D images or sketches. This helps in virtual reality, gaming, and
CAD systems. Models like 3D-GAN learn spatial features for object construction. A single image can
lead to a complete 3D mesh. Designers use it for quick prototyping. It saves hours of manual
modeling. It’s also useful in medical imaging for organ reconstruction. GANs predict depth, volume,
and structure realistically. The output can be rotated, scaled, and animated. It’s revolutionizing 3D
content creation.
70
UNIT-V
Auto-encoders
Autoencoders are a class of unsupervised neural network models designed to learn efficient codings
of input data. The fundamental objective of an autoencoder is to learn a compressed representation
(encoding) of the input data and then reconstruct the original input from this representation with
minimal loss. Autoencoders are typically used for tasks such as dimensionality reduction, feature
learning, denoising, and data compression. They operate by mapping the input to a latent space (also
called bottleneck or code), which captures the most essential information of the data in a compact
form, and then reconstructing the input from this latent code.
The learning process involves minimizing the difference between the input and its reconstruction
using a loss function, typically mean squared error (MSE) for continuous data or binary cross-entropy
for binary data. Unlike supervised learning, autoencoders do not require labeled data. Instead, the
training is driven by the objective of accurate self-reconstruction. Autoencoders have inspired many
variants, such as denoising autoencoders, sparse autoencoders, and variational autoencoders (VAEs),
which are used in diverse applications ranging from image denoising and anomaly detection to
pretraining deep networks and generative modeling.
71
1. Encoder
It compress the input data into a smaller, more manageable form by reducing its dimensionality while
preserving important information. It has three layers which are:
Input Layer: This is where the original data enters the network. It can be images, text
features or any other structured data.
Hidden Layers: These layers perform a series of transformations on the input data. Each
hidden layer applies weights and activation functions to capture important patterns,
progressively reducing the data's size and complexity.
Output(Latent Space): The encoder outputs a compressed vector known as the latent
representation or encoding. This vector captures the important features of the input data in a
condensed form helps in filtering out noise and redundancies.
3. Decoder
It is responsible for taking the compressed representation from the latent space and reconstructing it
back into the original data form.
Hidden Layers: These layers progressively expand the latent vector back into a higher-
dimensional space. Through successive transformations decoder attempts to restore the
original data shape and details
Output Layer: The final layer produces the reconstructed output which aims to closely
resemble the original input. The quality of reconstruction depends on how well the encoder-
decoder pair can minimize the difference between the input and output during training.
During training the network updates its weights using backpropagation to minimize this
reconstruction loss. By doing this it learns to extract and retain the most important features of the
input data which are encoded in the latent space.
Constraining an autoencoder helps it learn meaningful and compact features from the input data
which leads to more efficient representations. After training only the encoder part is used to encode
similar data for future tasks. Various techniques are used to achieve this are as follows:
Keep Small Hidden Layers: Limiting the size of each hidden layer forces the network to
focus on the most important features. Smaller layers reduce redundancy and allows efficient
encoding.
Regularization: Techniques like L1 or L2 regularization add penalty terms to the loss
function. This prevents overfitting by removing excessively large weights which helps in
ensuring the model to learns general and useful representations.
Denoising: In denoising autoencoders random noise is added to the input during training. It
learns to remove this noise during reconstruction which helps it focus on core, noise-free
features and helps in improving robustness.
72
Tuning the Activation Functions: Adjusting activation functions can promote sparsity by
activating only a few neurons at a time. This sparsity reduces model complexity and forces
the network to capture only the most relevant features.
1. Denoising Autoencoder
2. Sparse Autoencoder
A Sparse Autoencoder is a variation that introduces a sparsity constraint on the hidden units during
training. While it may have more hidden neurons than input features, only a small fraction of neurons
are allowed to activate at once. This forces the network to learn efficient and compact feature
representations. The sparsity is typically enforced using a regularization term (such as L1 or KL
divergence) added to the loss function. Alternatively, activation functions or thresholding methods
can be used to limit neuron activation. Sparse autoencoders mimic how neurons in the human brain
fire selectively. They are particularly useful in feature selection, compressed sensing, and
unsupervised pretraining for deep neural networks. The sparse representations help the network focus
on the most informative features of the input. In image processing, they can extract high-level visual
features like edges and corners. Sparse Autoencoders are commonly used in anomaly detection, where
anomalies trigger more neuron activations than typical samples. They are also used in natural
language processing for learning sparse word embeddings. Unlike traditional autoencoders, they do
not require noise to learn robust features. The sparsity encourages the discovery of latent patterns in
high-dimensional data. Overfitting is reduced due to limited neuron usage. Training can be slower due
to the additional constraint, but the representations learned are often more interpretable. Sparse
autoencoders are considered biologically plausible and are used in cognitive modeling.
3. Variational Autoencoder
A Variational Autoencoder (VAE) is a probabilistic generative model that learns the distribution of
data and can generate new, realistic samples from that distribution. Unlike traditional autoencoders,
which encode inputs into a fixed latent vector, VAEs encode inputs into distributions (typically
Gaussian). The encoder outputs the mean and variance of the latent variables instead of a single
vector. Then, a latent vector is sampled using the reparameterization trick, which allows
73
backpropagation through stochastic nodes. The decoder reconstructs the input from the sampled latent
vector. The loss function consists of two parts: reconstruction loss (e.g., MSE) and KL divergence
(which measures the difference between the learned latent distribution and a prior, usually standard
normal). This setup encourages smooth, continuous latent spaces, making interpolation and data
generation possible. VAEs are used for image generation, data compression, anomaly detection, and
semi-supervised learning. In NLP, VAEs can be used to generate coherent sentences. In the medical
domain, VAEs are used for synthetic medical data generation. Their probabilistic nature enables better
uncertainty modeling compared to standard autoencoders. The latent space learned by VAEs is more
structured, allowing arithmetic operations like morphing between images. However, VAEs may
produce blurrier images compared to GANs. Despite this, they are more stable and easier to train than
adversarial models. They represent a bridge between deep learning and Bayesian inference.
4. Convolutional Autoencoder
A Convolutional Autoencoder (CAE) is an autoencoder that uses convolutional layers instead of fully
connected layers, making it particularly suited for image data. The encoder uses convolutional
operations to extract spatial features such as edges, textures, and shapes from the input image. These
features are then compressed into a latent representation or bottleneck. The decoder uses
deconvolution (transposed convolution) or upsampling layers to reconstruct the original image from
this compressed form. CAEs preserve spatial locality, which is essential for tasks like image
reconstruction, denoising, and segmentation. Unlike traditional autoencoders, CAEs do not flatten the
image, allowing for better preservation of spatial structure. They are used in medical imaging, such as
MRI reconstruction or removing noise from X-ray scans. In self-supervised learning, CAEs can be
used to pretrain CNNs. Their compact feature maps are also used for object detection and face
recognition. Training involves minimizing the pixel-wise difference (e.g., MSE) between the input
and output images. CAEs are also used in video frame prediction and image colorization tasks. The
model learns to ignore irrelevant information and focuses on key visual features. CAEs are more
efficient and scalable for high-dimensional data. They are also integrated into architectures like U-
Nets for semantic segmentation. Overall, CAEs combine the power of autoencoders with the spatial
intelligence of CNNs, making them vital in modern deep learning for visual data.
Training an autoencoder involves feeding the input data through the encoder to obtain a latent code,
and then passing this code through the decoder to reconstruct the original input. The model is
optimized by minimizing a reconstruction loss, which measures how close the reconstructed output is
to the original input x. A common choice for this loss is the mean squared error (MSE) for continuous
data, although other metrics such as binary cross-entropy can be used depending on the data type.
In the context of data compression, the latent representation z serves as a compact encoding of the
input, ideally capturing the most relevant features in fewer dimensions. The smaller the latent space,
the higher the compression, although overly small latent spaces may result in loss of important
information and poor reconstructions. Data reconstruction is the process of reconstructing the original
data from the latent code. The ability of the autoencoder to accurately reconstruct the input
demonstrates how well it has captured the underlying structure of the data.
To train an autoencoder effectively, the encoder and decoder are jointly optimized using
backpropagation and gradient descent. During training, the model learns to ignore irrelevant noise and
redundancy in the input, leading to cleaner, more meaningful representations. This property also
makes autoencoders useful for denoising tasks, where they are trained on noisy inputs but expected to
74
reconstruct clean outputs. Autoencoders can also be extended with regularization (e.g., sparsity
constraints or dropout) to encourage robustness and generalization.
import numpy as np
import [Link] as plt
from [Link] import mnist
from [Link] import Model
from [Link] import Input, Dense
from [Link] import Adam
# Autoencoder model
autoencoder = Model(input_img, decoded)
75
[Link]("Original")
[Link]('off')
# Reconstructed
ax = [Link](2, n, i + 1 + n)
[Link](decoded_imgs[i].reshape(28, 28), cmap='gray')
[Link]("Reconstructed")
[Link]('off')
[Link]()
Although autoencoders and Generative Adversarial Networks (GANs) are conceptually different, they
share several common goals, such as learning data representations and generating realistic outputs.
Autoencoders focus on reconstruction: they compress the input into a latent code and attempt to
reconstruct the original data. GANs, on the other hand, focus on generation: they learn to generate
new, synthetic data that mimics the distribution of real data using an adversarial training process.
The relationship between the two lies in their ability to learn latent representations and generate data.
While autoencoders are deterministic and directly reconstruct the input, GANs use a generator
network to map random noise to realistic data, and they are trained in a probabilistic adversarial setup.
A major difference is that the generator in a GAN does not necessarily reconstruct an existing sample
but creates new data points, whereas the autoencoder always tries to reconstruct its input.
Recent research has combined the strengths of both approaches to create hybrid models such as
Variational Autoencoders (VAEs) and Encoder-Decoder GANs, where encoders are integrated into
GAN-like architectures to provide structured latent spaces. This integration helps address GAN
challenges like mode collapse and provides better control over the generation process. Thus,
autoencoders and GANs can be viewed as complementary models—autoencoders excel at
representation learning and reconstruction, while GANs are powerful tools for generating realistic and
diverse data.
Autoencoders and Generative Adversarial Networks (GANs) are two major families of generative
models that have significantly influenced the field of machine learning and artificial intelligence.
Although they are conceptually different in terms of structure and training paradigms, they share the
overarching goal of learning meaningful data representations and generating new data samples that
resemble real data. In this document, we delve into the similarities, differences, and hybrid
approaches that integrate both models, offering a detailed view of their theoretical and practical
significance.
76
Autoencoders are neural networks trained to copy their input to their output. They consist of two main
components: an encoder and a decoder. The encoder compresses the input into a latent-space
representation, often of lower dimensionality. The decoder then attempts to reconstruct the original
input from this latent representation. The reconstruction loss, such as Mean Squared Error (MSE),
guides the training process. Autoencoders are deterministic and are primarily used for dimensionality
reduction, data compression, and noise removal.
Autoencoders are useful in feature learning and are often used as a pretraining mechanism in deep
neural networks.
GANs: Principles and Architecture GANs consist of two neural networks: a generator and a
discriminator, engaged in a two-player minimax game. The generator creates synthetic data from
random noise, aiming to fool the discriminator. The discriminator attempts to distinguish between real
and fake data. The adversarial training setup allows the generator to produce increasingly realistic
data over time.
The generator learns to map from a simple distribution (e.g., Gaussian noise) to a complex data
distribution (e.g., images of faces). Unlike autoencoders, GANs are trained not to reconstruct but to
generate new samples that are statistically similar to the training data. The objective function typically
includes a binary cross-entropy loss representing the adversarial game between the two networks.
GANs have revolutionized generative modeling and are widely used in:
Autoencoders are more stable to train and offer clear insight into the latent space, making them
suitable for interpretability and analysis. GANs, on the other hand, often face challenges such as mode
collapse, non-convergence, and training instability but excel in producing high-quality and diverse
outputs.
Hybrid Models and Recent Advances Recent research has focused on combining the strengths of
autoencoders and GANs to develop hybrid architectures. These include:
77
Adversarial Autoencoders (AAEs): Replace the KL-divergence in VAEs with an
adversarial loss to match the encoded latent distribution to a prior.
BiGANs (Bidirectional GANs): Introduce an encoder to GANs, enabling learning of inverse
mappings from data to latent space.
These hybrid models address issues like poor sample quality in VAEs and lack of latent structure in
GANs. They are also used in semi-supervised learning, anomaly detection, and conditional generation
tasks.
Applications and Practical Use-Cases Autoencoders and GANs have found applications across
diverse domains:
Medical Imaging: Autoencoders are used for noise reduction, anomaly detection, and image
compression. GANs help in generating synthetic medical images for data augmentation.
Natural Language Processing: Autoencoders are applied in sentence embedding and
document summarization, whereas GANs are used for text generation and machine
translation.
Cybersecurity: Autoencoders can detect anomalies in network traffic, while GANs are used
to generate realistic attack patterns for defense testing.
Art and Design: GANs have been used to create art, design new clothing patterns, and even
assist in architectural design.
Autoencoders and GANs represent two distinct yet complementary approaches to generative
modeling. Autoencoders focus on learning efficient representations and reconstructing inputs, while
GANs aim to generate realistic samples from scratch. Each has its strengths and weaknesses, but their
combination has opened new avenues for robust and controllable generation. Understanding both
models provides a comprehensive toolkit for anyone involved in machine learning, especially in the
field of unsupervised learning and creative AI applications.
As the research community continues to explore these models' frontiers, we can expect further
innovation, improved training methods, and broader adoption across scientific, industrial, and creative
domains.
Hybrid models that combine encoder-decoder structures with GANs aim to unify the benefits of both
architectures, resulting in more powerful and interpretable generative systems. One such approach is
the Encoder-Decoder GAN, which introduces an encoder into the GAN framework to map real data
into the latent space, thereby enabling bidirectional mapping between the data space and the latent
space. Traditional GANs lack this ability, as they only map from a latent vector to a data sample,
making it difficult to interpret or manipulate the latent representation of real data.
In an Encoder-Decoder GAN, the encoder learns to map real input samples to the corresponding latent
vectors, and the generator (decoder) maps those latent vectors back to data samples. This setup allows
for tasks like image editing, interpolation, and semantic manipulation by modifying the latent code.
The discriminator, as in standard GANs, is trained to distinguish real data from generated data.
However, it may also be conditioned on the latent vector or the encoding to ensure consistency
between the encoded and decoded representations.
78
Encoder: The Compressor
The encoder is a neural network that processes the input data (such as an image or video frame) and
compresses it into a lower-dimensional representation known as a latent space or hidden state. This
process involves the abstraction of essential features from the input while discarding redundant
information.
The decoder is another neural network that takes the compressed data from the encoder and
reconstructs it into the desired output. In deepfake generation, the decoder is responsible for creating
the final manipulated image or video by applying the learned features onto a target face or scene.
79
While not strictly part of the encoder-decoder architecture, GANs are often used in conjunction with it
to enhance the quality of deepfakes. A GAN consists of two competing networks: a generator (akin to
the decoder) that creates images, and a discriminator that evaluates their authenticity. The generator
strives to produce outputs that the discriminator cannot distinguish from real images, thus improving
the realism of the deepfakes.
80
Sequence to Sequence Learning
81
Models like BiGAN (Bidirectional GAN) and ALI (Adversarially Learned Inference) are prominent
examples of encoder-decoder GANs. These models improve the interpretability of GANs by enabling
inference—mapping real data to the latent space—and support tasks such as anomaly detection,
feature extraction, and conditional generation. In practice, encoder-decoder GANs bring together the
strengths of both worlds: the structured latent representation and reconstruction capabilities of
autoencoders, and the high-quality generation and adversarial training dynamics of GANs.
1. Generator (G): Takes a latent variable sampled from a prior distribution and generates a data
sample .
2. Encoder (E): Maps a data sample to a latent representation .
3. Discriminator (D): Differentiates between pairs (x, E(x)) and (G(z), z), attempting to
distinguish real from generated pairs.
The discriminator is trained to assign high probability to pairs of real data and their encoded latent
vectors and low probability to pairs of generated data and the latent vectors they originated from. The
generator and encoder are trained jointly to fool the discriminator, effectively minimizing the
divergence between the joint distributions of real and generated data pairs.
82
Advantages
Latent space inference: Unlike vanilla GANs, BiGANs can map data to the latent space,
making them useful for tasks like clustering, retrieval, and interpolation.
Unsupervised feature learning: BiGANs naturally learn useful representations of input data
without labeled supervision.
Compatibility: The model structure is general and compatible with various GAN
improvements and extensions.
Introduction Adversarially Learned Inference (ALI) is another generative model that jointly learns a
generator and an encoder using adversarial training. ALI was introduced around the same time as
BiGAN and is structurally and functionally similar. The key idea in ALI is to learn the joint
distribution over data and latent variables by matching the joint distribution of (x, z) pairs from the
encoder and generator.
Unlike traditional GANs that only generate data from noise, ALI adds the ability to infer the noise
vector (latent code) from observed data. The goal is to make the joint distribution of real data and its
encoded latent variables indistinguishable from the joint distribution of generated data and the original
latent variables.
Advantages
83