0% found this document useful (0 votes)
16 views3 pages

Backpropagation

The document outlines the implementation of an Artificial Neural Network (ANN) using the Backpropagation algorithm on the Iris dataset. It details the steps for data preprocessing, ANN class definition, training, and testing the model, culminating in an accuracy evaluation. The program includes Python code for loading data, defining the ANN, and calculating accuracy after training.

Uploaded by

haseenafrn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Backpropagation

The document outlines the implementation of an Artificial Neural Network (ANN) using the Backpropagation algorithm on the Iris dataset. It details the steps for data preprocessing, ANN class definition, training, and testing the model, culminating in an accuracy evaluation. The program includes Python code for loading data, defining the ANN, and calculating accuracy after training.

Uploaded by

haseenafrn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7.

Implementation of an Artificial Neural Network Using Backpropagation and


Performance Evaluation on a Dataset

Aim
To implement an Artificial Neural Network (ANN) using the Backpropagation
Algorithm and evaluate its performance on the Iris dataset.
Algorithm

1.​ Load & Preprocess Data:


o​ Load Iris dataset.
o​ One-hot encode the target labels.
o​ Split into training (80%) and testing (20%) sets.
2.​ Define ANN Class (Simple ANN)
o​ Initialize Weights & Biases: Random initialization.
o​ Forward Pass: Uses Sigmoid Activation.
o​ Backward Pass: Computes error gradients & updates weights using Gradient
Descent.
o​ Training: Loops for epochs=1000 with forward & backward propagation.
o​ Prediction: Uses the argmax function to determine class labels.
3.​ Train & Test the Model
o​ Train using 1000 epochs.
o​ Predict on test data.
o​ Compute accuracy.

Program
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder

# Load the Iris dataset


iris = load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)

# One-hot encode the target variable


encoder = OneHotEncoder(sparse_output=False)
y_encoded = encoder.fit_transform(y)

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y_encoded,
test_size=0.2, random_state=42)

# Define the ANN class


class SimpleANN:
def __init__(self, input_size, hidden_size, output_size,
learning_rate=0.01):
self.learning_rate = learning_rate
self.weights_input_hidden = np.random.rand(input_size,
hidden_size)
self.weights_hidden_output = np.random.rand(hidden_size,
output_size)
self.bias_hidden = np.random.rand(hidden_size)
self.bias_output = np.random.rand(output_size)

def sigmoid(self, x):


return 1 / (1 + np.exp(-x))

def sigmoid_derivative(self, x):


return x * (1 - x)

def forward(self, X):


self.hidden_layer_input = np.dot(X, self.weights_input_hidden)
+ self.bias_hidden
self.hidden_layer_output =
self.sigmoid(self.hidden_layer_input)
self.final_input = np.dot(self.hidden_layer_output,
self.weights_hidden_output) + self.bias_output
self.final_output = self.sigmoid(self.final_input)
return self.final_output

def backward(self, X, y, output):


output_error = y - output
output_delta = output_error * self.sigmoid_derivative(output)

hidden_layer_error =
output_delta.dot(self.weights_hidden_output.T)
hidden_layer_delta = hidden_layer_error *
self.sigmoid_derivative(self.hidden_layer_output)

# Update weights and biases


self.weights_hidden_output +=
self.hidden_layer_output.T.dot(output_delta) * self.learning_rate
self.bias_output += np.sum(output_delta, axis=0) *
self.learning_rate
self.weights_input_hidden += X.T.dot(hidden_layer_delta) *
self.learning_rate
self.bias_hidden += np.sum(hidden_layer_delta, axis=0) *
self.learning_rate

def train(self, X, y, epochs):


for _ in range(epochs):
output = self.forward(X)
self.backward(X, y, output)

def predict(self, X):


output = self.forward(X)
return np.argmax(output, axis=1)

# Initialize and train the ANN


input_size = X_train.shape[1]
hidden_size = 5 # Number of neurons in the hidden layer
output_size = y_encoded.shape[1]
ann = SimpleANN(input_size, hidden_size, output_size)

# Train the model


ann.train(X_train, y_train, epochs=1000)

# Test the model


predictions = ann.predict(X_test)
y_test_labels = np.argmax(y_test, axis=1)

# Calculate accuracy
accuracy = np.mean(predictions == y_test_labels)
print(f'Accuracy: {accuracy:.4f}')

Output
Accuracy:

Result
The implemented Artificial Neural Network (ANN) using the Backpropagation algorithm
achieved an accuracy of on the Iris dataset.

You might also like