# 4. AI AND MACHINE LEARNING VTU LAB | READ NOW

MACHINE LEARNING VTU LAB – Backpropagation Algorithm

Program 4] BUILD AN ARTIFICIAL NEURAL NETWORK BY IMPLEMENTING THE BACKPROPAGATION ALGORITHM AND TEST THE SAME USING APPROPRIATE DATASETS.

Program Code – lab4.py

import numpy as np

X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)     # X = (hours sleeping, hours studying)
y = np.array(([92], [86], [89]), dtype=float)           # y = score on test

# scale units
X = X/np.amax(X, axis=0)        # maximum of X array
y = y/100                       # max test score is 100

class Neural_Network(object):
def __init__(self):
# Parameters
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
# Weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize)        # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize)       # (3x1) weight matrix from hidden to output layer

def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1)               # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z)            # activation function
self.z3 = np.dot(self.z2, self.W2)        # dot product of hidden layer (z2) and second set of 3x1 weights
o = self.sigmoid(self.z3)                 # final activation function
return o

def sigmoid(self, s):
return 1/(1+np.exp(-s))     # activation function

def sigmoidPrime(self, s):
return s * (1 - s)          # derivative of sigmoid

def backward(self, X, y, o):
# backward propgate through the network
self.o_error = y - o        # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to
self.z2_error = self.o_delta.dot(self.W2.T)    # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta)       # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta)  # adjusting second set (hidden --> output) weights

def train (self, X, y):
o = self.forward(X)
self.backward(X, y, o)

NN = Neural_Network()
print ("\nInput: \n" + str(X))
print ("\nActual Output: \n" + str(y))
print ("\nPredicted Output: \n" + str(NN.forward(X)))
print ("\nLoss: \n" + str(np.mean(np.square(y - NN.forward(X)))))     # mean sum squared loss)
NN.train(X, y)

## MACHINE LEARNING Program Execution – lab4.ipynb

Jupyter Notebook program execution.

import numpy as np

X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)     # X = (hours sleeping, hours studying)
y = np.array(([92], [86], [89]), dtype=float)           # y = score on test

# scale units
X = X/np.amax(X, axis=0)        # maximum of X array
y = y/100                       # max test score is 100
class Neural_Network(object):
def __init__(self):
# Parameters
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
# Weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize)        # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize)       # (3x1) weight matrix from hidden to output layer

def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1)               # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z)            # activation function
self.z3 = np.dot(self.z2, self.W2)        # dot product of hidden layer (z2) and second set of 3x1 weights
o = self.sigmoid(self.z3)                 # final activation function
return o

def sigmoid(self, s):
return 1/(1+np.exp(-s))     # activation function

def sigmoidPrime(self, s):
return s * (1 - s)          # derivative of sigmoid

def backward(self, X, y, o):
# backward propgate through the network
self.o_error = y - o        # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to
self.z2_error = self.o_delta.dot(self.W2.T)    # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta)       # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta)  # adjusting second set (hidden --> output) weights

def train (self, X, y):
o = self.forward(X)
self.backward(X, y, o)
NN = Neural_Network()
for i in range(1000): # trains the NN 1,000 times
print ("\nInput: \n" + str(X))
print ("\nActual Output: \n" + str(y))
print ("\nPredicted Output: \n" + str(NN.forward(X)))
print ("\nLoss: \n" + str(np.mean(np.square(y - NN.forward(X)))))     # mean sum squared loss)
NN.train(X, y)
Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.47212874]
[0.42728946]
[0.40891365]]

Loss:
0.20642371917499927

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:

show more (open the raw output data in a text editor) … Actual Output: [[0.92] [0.86] [0.89]]

Predicted Output:
[[0.90664827]
[0.85694302]
[0.904511  ]]

Loss:
0.00013272761631194843

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.90666572]
[0.85696268]
[0.90452085]]

Loss:

show more (open the raw output data in a text editor) … [0.85762678] [0.90441548]] Loss: 0.00012413266964384637

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.90739553]
[0.85762866]
[0.90441066]]

Loss:
0.00012405438508618016

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:

show more (open the raw output data in a text editor) …[[0.90801592] [0.85790991] [0.90332005]] Loss:

0.00010847015771193348

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.90801867]
[0.85791115]
[0.90331503]]

Loss:
0.00010840182852314304

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

show more (open the raw output data in a text editor) … Input: [[0.66666667 1. ] [0.33333333 0.55555556] [1. 0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.90843358]
[0.85810301]
[0.90255937]]

Loss:
9.837281731803699e-05

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:

show more (open the raw output data in a text editor) …8.405495434787463e-05 Input: [[0.66666667 1. ] [0.33333333 0.55555556]

[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

Predicted Output:
[[0.90907296]
[0.85841616]
[0.90140598]]

Loss:
8.400178305772788e-05

Input:
[[0.66666667 1.        ]
[0.33333333 0.55555556]
[1.         0.66666667]]

Actual Output:
[[0.92]
[0.86]
[0.89]]

show more (open the raw output data in a text editor) … [0.85857961] [0.90083551]] Loss: 7.731308968994962e-05