MLT UNIT-4 & 5 imp sol
MLT UNIT-4 & 5 imp sol
A Convolutional Neural Network (CNN) is a type of deep learning neural network designed
primarily for processing structured grid-like data, such as images or time-series data. CNNs are
particularly effective for tasks like image recognition, object detection, and natural language
processing due to their ability to automatically and adaptively learn spatial hierarchies of
features from data.
1. Convolution Layer:
○ The core building block of CNN.
○ Performs a convolution operation by sliding a filter (kernel) over the input data to
detect local patterns such as edges, corners, and textures.
○ Output: A feature map that highlights the detected patterns.
2. Mathematical Operation:
1. Input Layer:
○ Accepts raw input, such as an image represented as a multi-dimensional array
(e.g., RGB image as a 3D array).
2. Feature Extraction:
○ Sequential layers of convolution, ReLU, and pooling extract hierarchical
features from the input.
3. Flattening:
○ The multi-dimensional feature map is flattened into a 1D vector for input to the
fully connected layer.
4. Classification:
○ The flattened vector is passed through one or more fully connected layers and
finally a softmax or sigmoid activation function to output class probabilities.
Applications of CNN:
Advantages of CNN:
Limitations of CNN:
A CNN is composed of several specialized layers, each serving a distinct purpose in the feature
extraction and classification process. Below are the key types of layers used in CNNs:
1. Convolutional Layers
● Purpose: Extract local features by performing a convolution operation over the input.
● How it works:
○ A small filter (or kernel) slides over the input data to compute the dot product
between the filter values and the input at each location.
○ The output is a feature map that represents the detected features.
● Key Parameters:
○ Filter Size: Dimensions of the kernel (e.g., 3x3, 5x5).
○ Stride: The number of steps the filter moves during convolution.
○ Padding: Adding extra layers of zeros around the input to preserve dimensions.
■ Same Padding: Keeps the output dimensions the same as the input.
■ Valid Padding: No padding is added, reducing the output size.
● Example:
○ Input: 28x28 grayscale image.
○ Kernel: 3x3 filter.
○ Output: Feature map showing edges, corners, or textures.
3. Pooling Layers
● Purpose: Reduce spatial dimensions of feature maps while retaining the most important
information.
● Types of Pooling:
○ Max Pooling:
■ Selects the maximum value in each region.
■ Highlights the most prominent features.
○ Average Pooling:
■ Computes the average value in each region.
■ Retains more global information.
● Key Parameters:
○ Filter Size: The dimensions of the pooling region (e.g., 2x2, 3x3).
○ Stride: Steps the pooling operation moves across the feature map.
● Benefits:
○ Reduces computational load.
○ Provides invariance to small translations in the input.
● Example:
○ Input: 4x4 feature map.
○ Max Pooling (2x2, stride 2): Reduces it to a 2x2 feature map.
● Purpose: Connect all neurons in one layer to every neuron in the next layer to perform
classification.
● How it works:
○ Flattens the output of the previous layers (e.g., a 2D feature map) into a 1D
vector.
○ Applies matrix multiplication and biases to produce outputs.
● Key Features:
○ Typically used at the end of a CNN to combine extracted features into a final
decision.
○ Outputs probabilities (via softmax) or a specific value depending on the task.
● Example:
○ Flattened input: [0.5, 0.7, 0.1, 0.8].
○ Output: [0.2 (class 1), 0.8 (class 2)].
Convolutional Detect local patterns (edges, corners, textures) by applying filters over
the input.
Fully Connected Combine all features extracted by previous layers into a final decision
or prediction.
The Backpropagation Algorithm is the key method for training neural networks, including
Convolutional Neural Networks (CNNs). It computes the gradients of the loss function with
respect to the weights and biases of the network using the chain rule of calculus. These
gradients are then used to update the parameters during training via optimization techniques
such as Gradient Descent.
1. Preliminaries
2. .
3.
4
6.
Advantages of Backpropagation
Limitations
Generalization, (Single-layer feed forward network, Multilayer feed forward network, and
Single node with its own feedback)
Generalization refers to the ability of a neural network to perform well on unseen data (test
data) after being trained on a training dataset. A model generalizes well if it effectively captures
the underlying patterns of the data without overfitting or underfitting.
● Structure:
○ Consists of one input layer and one output layer.
○ Neurons in the input layer are directly connected to the output layer, with no
hidden layers in between.
● Functionality:
○ Computes a simple mapping from inputs to outputs using linear or non-linear
activation functions.
● Use Cases:
○ Suitable for solving linearly separable problems (e.g., perceptron model).
● Advantages:
○ Simplicity and computational efficiency.
● Limitations:
○ Cannot solve non-linear problems due to the lack of hidden layers.
● Structure:
○ Consists of an input layer, one or more hidden layers, and an output layer.
○ Neurons in each layer are fully connected to the next layer.
● Functionality:
○ Introduces hidden layers to enable the network to learn complex, non-linear
mappings.
○ Uses activation functions like ReLU, sigmoid, or tanh in hidden layers.
● Learning:
○ Trained using backpropagation to minimize the loss function.
● Use Cases:
○ Regression, classification, and other tasks requiring modeling of complex
relationships.
● Advantages:
○ Can approximate any continuous function (universal approximation theorem).
● Limitations:
○ Requires careful tuning of hyperparameters (e.g., number of layers, learning
rate).
● Structure:
○ A single neuron with a feedback loop connecting its output back to its input.
● Functionality:
○ Can store and process temporal information.
○ Models time-dependent processes such as time-series data or sequential
patterns.
● Example:
○ The simplest form of a recurrent neural network (RNN).
● Learning:
○ Feedback allows it to maintain a memory of past inputs, making it suitable for
dynamic systems.
● Use Cases:
○ Control systems, time-series prediction, or simple state-based models.
● Advantages:
○ Simple and effective for problems with temporal dependencies.
● Limitations:
○ Limited expressiveness compared to more advanced recurrent networks like
RNNs or LSTMs.
Structure No hidden layers One or more hidden layers Single node with
feedback
Use Case AND, OR, XOR gates Image classification, Time-series prediction
Examples regression
UNIT-5
Roulette-wheel based on fitness and Roulette-wheel based on rank
Description:
In this method, the probability of selecting an individual is proportional to its fitness value. Higher
fitness individuals are more likely to be selected.
Steps:
1.
Advantages:
Disadvantages:
Description:
Instead of using fitness values directly, individuals are ranked based on their fitness. The
probability of selection is based on the rank, not the actual fitness value. This mitigates issues
with fitness scaling.
Steps:
1.
Advantages:
Disadvantages:
● Can slow down convergence since fitness differences are not directly used.
Convergence Faster but risks premature Slower but more robust to scaling
Speed convergence issues
Use Cases Suitable for problems with Suitable for problems with large
well-scaled fitness values fitness variations
Example
Fitness-Based Roulette:
Rank-Based Roulette:
Population ranks:
Individual A (Rank 4), B (Rank 3), C (Rank 2), D (Rank 1)\text{Individual A (Rank 4), B (Rank 3),
C (Rank 2), D (Rank 1)}Individual A (Rank 4), B (Rank 3), C (Rank 2), D (Rank 1)
1. Fitness-Based:
○ When fitness values are well-scaled and represent individual performance
accurately.
○ Faster convergence is desired, and diversity is not a major concern.
2. Rank-Based:
○ When fitness values vary widely or are not linearly scaled.
○ To maintain diversity and prevent premature convergence.
A Genetic Algorithm (GA) is inspired by natural selection and evolution. Its components are
designed to mimic the processes of reproduction, mutation, and survival of the fittest.
1. Initial Population
2. Fitness Function
● Definition: A function that evaluates how good a solution (individual) is for the given
problem.
● Purpose:
○ Assigns a fitness value to each individual.
○ Guides the selection of individuals for reproduction.
● Examples:
○ In a maximization problem, fitness could be the objective function value.
○ In a minimization problem, fitness could be the inverse of the objective value.
3. Selection
● Definition: The process of choosing individuals for reproduction based on their fitness.
● Techniques:
○ Roulette-Wheel Selection: Probabilistic selection based on fitness or rank.
○ Tournament Selection: Randomly selects individuals and chooses the best
among them.
○ Rank-Based Selection: Selection probability is based on rank, not raw fitness.
○ Elitism: Guarantees the best individuals are carried over to the next generation.
4. Crossover (Recombination)
● Definition: Combines genetic material from two parent individuals to create offspring.
● Types:
○ Single-Point Crossover: A single point is selected, and parts are swapped
between parents.
○ Two-Point Crossover: Two points are selected, and segments between them
are swapped.
○ Uniform Crossover: Each gene in the offspring is randomly selected from one of
the parents.
● Purpose:
○ Introduces diversity by combining traits from both parents.
5. Mutation
6. Termination Criteria
GA Cycle of Reproduction
The Genetic Algorithm follows an iterative process (cycle) to evolve the population over
successive generations:
Step 1: Initialization
Step 2: Evaluation
● Calculate the fitness of each individual in the population using the fitness function.
Step 3: Selection
● Select individuals based on their fitness using techniques like roulette-wheel selection or
tournament selection.
Step 4: Crossover
Step 5: Mutation
Step 6: Replacement
1.
The Traveling Salesman Problem (TSP) involves finding the shortest possible route that visits a
set of cities exactly once and returns to the starting city. Solving TSP using a Genetic
Algorithm (GA) involves encoding routes as chromosomes and evolving them over generations
to minimize the total distance traveled.
3. Genetic Operators
1. Selection:
○ Select parents based on their fitness. Common methods:
■ Roulette-Wheel Selection: Probabilities are proportional to fitness.
■ Tournament Selection: Randomly pick a subset of individuals and select
the best one.
2. Crossover (Recombination):
○ Combines two parent routes to produce offspring while preserving valid city
permutations.
○ Common Crossover Methods:
■ Partially Mapped Crossover (PMX):
■ Randomly choose two crossover points.
■ Exchange segments between parents while keeping the rest of the
route valid.
■ Order Crossover (OX):
■ Randomly choose a segment from one parent.
■ Fill the rest of the route with cities from the other parent in the
order they appear.
3. Mutation:
○ Introduces random variations to maintain diversity in the population.
○ Common Mutation Methods:
■ Swap Mutation:
■ Swap the positions of two cities in the route.
■ Inversion Mutation:
■ Reverse the order of cities in a randomly selected segment.
■ Scramble Mutation:
■ Randomly shuffle a subset of cities in the route.
4. Replacement:
○ Replace less fit individuals in the population with new offspring.
○ Use elitism to preserve the best solutions.
4. Algorithm Cycle
1. Initialize Population:
○ Generate random permutations of cities.
2. Evaluate Fitness:
○ Calculate the fitness of each route in the population.
3. Selection:
○ Select parents based on fitness.
4. Crossover:
○ Perform crossover to generate offspring.
5. Mutation:
○ Apply mutation to offspring.
6. Replacement:
○ Replace the least fit individuals in the population.
7. Repeat:
○ Repeat the process for a predefined number of generations or until convergence.
8. Output:
○ The best solution (route with the shortest distance).
Example
A B C D
A 0 10 15 20
B 10 0 35 25
C 15 35 0 30
D 20 25 30 0
1.
●
○
Limitations