INDEX
SI. No. Topic Pg. No. T. Sign
Experiment – 1
Aim - Classification with a Two-Input Perceptron
Theory
A two-input perceptron is one of the simplest types of artificial neural network models used
for binary classification. It takes two input values, multiplies each by a corresponding weight
that represents the importance of that input, and then adds a bias term to shift the decision
boundary. The perceptron then computes the weighted sum of these inputs and passes the
result through a hard-limit (step) activation function, which outputs either a 0 or a 1.
The output 1 typically represents one class, and 0 represents the other class. The decision
boundary formed by the perceptron is a straight line in the two-dimensional input space.
This line is determined entirely by the learned weights and the bias value. During the training
process, the perceptron adjusts its weights and bias so that data points belonging to different
categories fall on opposite sides of this line.
Because the boundary is linear, a two-input perceptron can only separate linearly separable
data—meaning that the two classes can be split by a straight line. Despite this limitation, the
perceptron laid the foundation for more advanced neural network models and is still useful
for understanding the basic principles of machine learning and binary classification.
1. Inputs (X₁, X₂, …)
The green circles on the left represent the inputs to the perceptron.
Each input is a numerical value that represents some feature of the data.
For example, in a simple classification problem, inputs might be things like
height, weight, or temperature.
2. Weights (W₁, W₂, …)
Each input is multiplied by a corresponding weight.
Weights determine how important each input is for making the decision.
Output:-
A larger weight means that input has more influence on the output.
3. Summation and Bias
All the weighted inputs are summed up together. Mathematically:
Summation=(W1×X1)+(W2×X2)+…..+(Wm×Xm)
A bias term is then added to shift the decision boundary.
This summation (Σ) determines the raw score before the perceptron makes a decision.
4. Activation Function
The blue box represents the activation function, which decides the output based on
the summation.
In this diagram, the perceptron uses a step (hard-limit) activation function:
If the sum is greater than or equal to zero, the output is 1; otherwise, it’s 0.
5. Output (ŷ)
Finally, the perceptron produces a single binary output ŷ (green circle on the right).
This output tells us the predicted class of the input (for example, 1 = Yes or 0 = No).
Code:-
Output:
Experiment – 2
Aim - Classification with a Two-Input Perceptron with straight line separation
Theory
A perceptron is one of the simplest types of artificial neural networks. It is mainly used for
binary classification, i.e., separating data points into two classes (for example, Class 0 and
Class 1). When we have two input features, the perceptron learns to separate the two classes
using a straight-line decision boundary in a 2-D space.
Code:
Experiment – 3
Aim - Training a Neural network to approximate the sine function
Theory
A neural network in MATLAB, similar to other implementations, is a computational model
inspired by the structure and function of biological neural networks. It is composed of
interconnected nodes (neurons) organized in layers: an input layer, one or more hidden
layers, and an output layer.
Theoretical Foundation:
Neurons and Connections: Each neuron receives inputs from other neurons,
processes them, and produces an output. These connections have associated weights
that determine the strength of the connection.
Activation Function: Each neuron applies an activation function to the weighted sum
of its inputs and a bias term. This function introduces non-linearity, enabling the
network to learn complex relationships. Common activation functions include
sigmoid, ReLU, and tanh.
Layers:
o Input Layer: Receives the raw data.
o Hidden Layers: Perform intermediate computations and extract features from
the input data.
o Output Layer: Produces the network's final output, which can be a
classification (e.g., in a pattern recognition network) or a continuous value
(e.g., in a regression network).
Learning (Training): The network learns by adjusting the weights and biases based
on a training dataset. This process typically involves:
o Forward Propagation: Input data passes through the network, and an output
is generated.
o Loss Function: The difference between the network's output and the desired
target output is calculated using a loss function.
o Backpropagation: The error is propagated backward through the network,
and gradients are calculated to determine how much each weight and bias
contributes to the error.
o Optimization Algorithm: An optimization algorithm (e.g., gradient descent,
Levenberg-Marquardt) uses these gradients to update the weights and biases,
minimizing the loss function.
Output:
Code:
Experiment – 4
Aim - Binary classification using Neural network
Theory
Binary classification using a neural network involves training a model to categorize input
data into one of two distinct classes. This is a common task in machine learning with
applications such as spam detection, medical diagnosis, and sentiment analysis.
Key Components and Steps:
1. Data Preparation:
o Encoding: If the target variable (the class labels) is in string format, it must be
converted to numerical representation (e.g., 0 and 1) using techniques like
LabelEncoder from scikit-learn.
o Normalization: Numerical predictor variables should be normalized (e.g., to a
range of 0.0 to 1.0 or -1.0 to +1.0) to prevent features with larger magnitudes
from dominating the learning process.
o One-Hot Encoding: Categorical predictor variables are typically converted
into a numerical format using one-hot encoding.
o Data Splitting: The dataset is typically split into training, validation, and test
sets to train the model, tune hyperparameters, and evaluate final performance
on unseen data.
2. Neural Network Architecture:
o Input Layer: The number of neurons in the input layer corresponds to the
number of features in the input data.
o Hidden Layers: These layers process the input and learn complex patterns. The
number of hidden layers and neurons within them can vary depending on the
complexity of the problem. Common activation functions in hidden layers
include ReLU, Leaky ReLU, PReLU, and ELU.
o Output Layer: For binary classification, a single neuron in the output layer is
used. The sigmoid activation function is applied to this neuron, which outputs
a probability value between 0 and 1, representing the likelihood of belonging
to one of the two classes.
3. Model Compilation and Training:
o Loss Function: Binary cross-entropy is the standard loss function for binary
classification problems, measuring the difference between predicted
probabilities and actual class labels.
o Optimizer: An optimizer (e.g., Adam, RMSprop) is used to update the model's
weights during training to minimize the loss function.
o Training: The model is trained using the training data, iteratively adjusting
weights and biases based on the chosen optimizer and loss function.
Output:
4. Evaluation:
o Metrics: Performance is evaluated using metrics like accuracy, precision,
recall, F1-score, and AUC-ROC, which are calculated based on True Positives
(TP), True Negatives (TN), False Positives (FP), and False Negatives (FN).
o Thresholding: The output probability from the sigmoid function is typically
thresholded (e.g., at 0.5) to assign a final class label (0 or 1).
Code:
Experiment – 5
Aim - Implement backpropagation in Neural Network, for XOR dataset having 4 input samples.
Consider three neurons in hidden layer and one in output layer. Display the actual outputs after 5000
epochs.
Theory:
Backpropagation is the learning process where the network adjusts its weights to minimize output
error by moving "backwards" from the output layer to the input layer, updating weights based on
gradients. In the XOR scenario, it’s important because the XOR function is not linearly separable—a
single-layer perceptron can’t solve it, but with a hidden layer, the non-linearity allows success.
Key Steps in Backpropagation
1. Forward Pass:
o Inputs are fed into the network and activations are calculated using the activation
function (commonly sigmoid).
2. Error Calculation:
o Compute the difference between the predicted outputs and actual (target) outputs using
a loss function (e.g., mean squared error).
3. Backward Pass (Gradient Calculation):
o Calculate the gradients of the error with respect to the weights using the chain rule.
o Gradients tell how much each weight contributed to the overall error.
4. Weight Update:
o Adjust weights in the direction that reduces the error (negative gradient), scaled by a
learning rate.
o This is called gradient descent.
Architecture Used for XOR:
Input Layer: 2 neurons (since XOR has two inputs)
Hidden Layer: 3 neurons
Output Layer: 1 neuron
Activation: Sigmoid for both hidden and output layers
Output:
For 1000 epochs:
For 3000 epochs:
For 5000 epochs:
Code:
Experiment – 6
Aim - Create a small RNN with one hidden layer, implementing BPTT, train it to predict the next
value in a sine wave sequence. Use a sigmoid activation function and MSE as loss function.
Theory:
What is an RNN?
A Recurrent Neural Network (RNN) is a neural network designed to handle sequential data.
Unlike feed-forward networks, an RNN has a recurrent connection that allows it to maintain
a hidden state—a kind of memory that carries information across time steps.
At each time step t, an RNN receives the current input xt and the previous hidden state ht−1,
producing:
ht=σ(Wxxt+Whht−1+bh)
and the output:
yt=Wyht+by
Here, σ is the sigmoid activation function.
Why BPTT (Back propagation ,Through Time)?
Training an RNN requires computing gradients over time, because each hidden state depends
on all previous ones.
BPTT “unrolls” the RNN like a long feed-forward network and computes partial derivatives
for each time step.
Key idea:
Because hidden states influence future states, BPTT must accumulate gradients across all
time steps.
Loss Function: Mean Squared Error (MSE)
For predicting the next value in a sine wave:
We use MSE because the problem is regression, not classification.
Why predict sine wave?
A sine wave is a smooth, predictable time series.
Training an RNN on it helps understand how sequence models learn:
temporal patterns
memory of earlier inputs
smooth continuous outputs
It is a common demonstration of sequence prediction.
Output:
Code:
Experiment – 7
Aim - Find the union of two fuzzy sets and display the result graphically
Theory:
1. Fuzzy Sets
A fuzzy set allows elements to have partial membership between 0 and 1.
Example:
0 ≤ μ(x) ≤ 1
2. Fuzzy Union
The union of two fuzzy sets A and B is defined as:
μA∪B(x) = max(μA(x), μB(x))
This means the membership value at each point is the maximum of the two sets.
3. Why Use Graphs?
Graphical representation helps visualize:
Both fuzzy sets
How union combines them
Overlapping areas
The union curve always lies on or above both sets.
Output:
Code:
Experiment – 8
Aim - Find the intersection of two fuzzy sets and display the result graphically
Theory:
1. Fuzzy Sets
A fuzzy set assigns each element a membership value between 0 and 1.
0 ≤ μ(x) ≤ 1
2. Fuzzy Intersection
The intersection of two fuzzy sets A and B is defined as:
μA∩B(x) = min(μA(x), μB(x))
This means at each point, the intersection takes the minimum membership value.
3. Why Graph?
Graphical representation helps visualize:
How both sets overlap
Where intersection occurs
How the min operator behaves
Intersection curve always lies below or equal to both sets.
Output:
Code:
Experiment – 9
Aim - Enter three fuzzy sets and show Commutative property among them
Theory:
1. Fuzzy Sets
A fuzzy set is a set in which each element has a degree of membership, not just 0 or 1.
The membership value lies between 0 and 1:
0 ≤ μ(x) ≤ 1
This helps represent uncertainty, vagueness, and partial truth.
2. Operations on Fuzzy Sets
Two commonly used fuzzy operations are:
(a) Union (OR)
μA∪B(x) = max(μA(x), μB(x))
Union selects the maximum membership value.
(b) Intersection (AND)
μA∩B(x) = min(μA(x), μB(x))
Intersection uses the minimum membership value.
3. Commutative Property
A fuzzy operation is said to be commutative if changing the order of the fuzzy sets does not
change the result.
Union Commutative Law
A∪B=B∪A
Intersection Commutative Law
A∩B=B∩A
This property must hold for any fuzzy sets, including the three you input.
4. Need of Commutative Property
Ensures mathematical consistency
Makes fuzzy systems predictable
Allows simplification of fuzzy logic expressions
switching their order pairwise (A ∪ B = B ∪ A, etc.).
Using three fuzzy sets (A, B, C), we can verify that the result remains the same when
Output:
Code:
Experiment – 10
Aim - Enter three fuzzy sets and show Associative property on fuzzy sets.
Theory:
1. Fuzzy Sets
A fuzzy set A on a universe X assigns each element x ∈ X a membership value:
0 ≤ μA(x) ≤ 1
This represents degree of membership, not just 0 or 1 like in classical sets.
2. Basic Fuzzy Operations
For two fuzzy sets A and B:
Union (OR):
μA∪B(x) = max(μA(x), μB(x))
Intersection (AND):
μA∩B(x) = min(μA(x), μB(x))
3. Associative Property
An operation is associative if grouping does not matter.
For Union:
(A∪B) ∪ C=A ∪ (B∪C)
In terms of membership:
μ(A∪B)∪C(x)=μA∪(B∪C)(x)
Because both sides reduce to:
max(μA(x),μB(x),μC(x))
For Intersection:
(A∩B) ∩ C=A ∩ (B∩C)
Membership form:
μ(A∩B)∩C(x)=μA∩(B∩C)(x)
Both sides reduce to:
min(μA(x),μB(x),μC(x))
So for any three fuzzy sets A, B, C, union and intersection are associative.
Output:
Code:
Experiment – 11
Aim - Enter three fuzzy sets and show Distributive property on fuzzy sets.
Theory:
1. Fuzzy Sets
A fuzzy set A on a universe X assigns each element x ∈ X a membership value:
0 ≤ μA(x) ≤ 1
This represents degree of membership, not just 0 or 1 like in classical sets.
2. Basic Fuzzy Operations
For two fuzzy sets A and B:
Union (OR):
μA∪B(x) = max(μA(x), μB(x))
Intersection (AND):
μA∩B(x) = min(μA(x), μB(x))
3. Distributive Property
Fuzzy union and intersection satisfy the distributive law.
(a) Union distributes over Intersection
A ∪ (B∩C) = (A∪B) ∩ (A∪C)
In membership form:
Left side:
μA∪(B∩C)(x) = max(μA(x), min(μB(x), μC(x)))
Right side:
μ(A∪B)∩(A∪C)(x) = min(max(μA(x), μB(x)), max(μA(x), μC(x)))
(b) Intersection distributes over Union
A ∩ (B∪C) = (A∩B) ∪ (A∩C)
Left:
μA∩(B∪C)(x) = min(μA(x), max(μB(x), μC(x)))
Right:
μ(A∩B)∪(A∩C)(x) = max(min(μA(x), μB(x)), min(μA(x), μC(x)))
If both sides match graphically → distributive property holds.
Output:
Code: