Lab Report 03
Lab Report 03
03
Name of the Experiment: Design and implementation of Multi-layer Neural Networks algorithm
(i.e., Back-propagation learning neural networks algorithm)
The MNIST database (Modified National Institute of Standards and Technology database) is a
large dataset of handwritten digits. It was produced from NIST's original datasets. Half of the
training set and half of the test set were taken from NIST's training dataset, while the other half of
the training set and the other half of the test set were taken from NIST's testing dataset.
Characteristics of Dataset:
• Large dataset of handwritten digits
• Total 70,000 images
• 60,000 training images and 10,000 testing images
• The size of every image is 28x28 pixels.
• Number of total features is 784.
• Total 10 classes
Implementation:
import numpy as np
import time
import matplotlib.pyplot
%matplotlib inline
f.read(16)
l.read(8)
images = []
for i in range(n):
image = [ord(l.read(1))]
for j in range(28*28):
image.append(ord(f.read(1)))
images.append(image)
After that the train and test dataset are loaded and then scaling is performed
all_values = test_list[100].split(',')
image_array = np.asfarray(all_values[1:]).reshape((28,28))
self.params = {
'W1':np.random.randn(hidden_1, input_layer) * np.sqrt(1. / hidden_
1),
'W2':np.random.randn(hidden_2, hidden_1) * np.sqrt(1. / hidden_2),
'W3':np.random.randn(output_layer, hidden_2) * np.sqrt(1. / output
_layer)
}
return params['A3']
Using backpropagation, error is back propagated from output layer to input layer and the weights
to be altered is proportional the error and calculated
def backward_pass(self, y_train, output):
params = self.params
change_w = {}
# Calculate W3 update
error = 2 * (output - y_train) / output.shape[0] * self.softmax(para
ms['Z3'], derivative=True)
change_w['W3'] = np.outer(error, params['A2'])
# Calculate W2 update
error = np.dot(params['W3'].T, error) * self.sigmoid(params['Z2'], d
erivative=True)
change_w['W2'] = np.outer(error, params['A1'])
# Calculate W1 update
error = np.dot(params['W2'].T, error) * self.sigmoid(params['Z1'], d
erivative=True)
change_w['W1'] = np.outer(error, params['A0'])
return change_w
for x in train_list:
all_values = x.split(',')
# scale and shift the inputs
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# create the target output values (all 0.01, except the desired
label which is 0.99)
targets = np.zeros(output_nodes) + 0.01
# all_values[0] is the target label for this record
targets[int(all_values[0])] = 0.99
output = self.forward_pass(inputs)
pred = np.argmax(output)
predictions.append(pred == np.argmax(targets))
return np.mean(predictions)
From the above figure, we see that dataset is not linearly separable. So, multi layer perceptron
learning algorithm will be applied to see if the model can classify the data set
Characteristics of Dataset:
● XOR dataset
● Total 4 samples
● 4 training samples and 4 testing samples
● Number of features is 2.
● Total 2 classes
Implementation:
Code:
import numpy as np
import math
from matplotlib import pyplot as plt
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
Then the dataset is generated and train data as x_train, train class label as y_train, test data as
x_ test, test class label as y_ test are extracted from the dataset
# Define the input and output data for the XOR problem
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
#weiight
def __init__(self, input_size, hidden_size, output_size):
# Initialize the weights for the hidden and output layers
self.weights_hidden = np.random.normal(size=(input_size, hidden_si
ze))
self.weights_output = np.random.normal(size=(hidden_size, output_s
ize))
At the time of forward pass, each layer calculates the output and pass the as input to next layer
Using backpropagation, error is back propagated from output layer to input layer and the weights
to be altered is proportional the error and calculated
def backpropagation(self, X, y, learning_rate):
# Calculate the error between the predicted output and the true ou
tput
output_error = y - self.output
# Update the weights and biases using the derivatives and the learning r
ate
epochs = 1000
learning_rate = 0.01
mlp.train(X, y, epochs, learning_rate)
y_pred = mlp.predict(X)
print("Predictions:", y_pred)
print("Accuracy:", np.mean(y_pred == y))
⮚ Underfitting
⮚ Overfitting
⮚ Divergency
Conclusion:
In conclusion, MLP is a powerful and versatile neural network model that has proven to be
effective in various machine learning applications. Its ability to learn and generalize from data, as
well as its flexibility in terms of network architecture and activation functions, make it a popular
choice in the field. However, its limitations in terms of overfitting and computational resources
should also be taken into consideration when using MLP in practical applications. Overall, MLP
is a valuable tool in the field of machine learning and can provide valuable insights and predictions
when used appropriately.