Ai Ml Lab Manual 17 (1)
Ai Ml Lab Manual 17 (1)
(Regulations 2021)
SEMESTER VI
(ACADEMIC YEAR 2023-24)
REGISTER NUMBER
INDEX
S.No Date Name Of The Experiment Page Marks Signature
No
1.a) 05
Implementation of Uninformed
search algorithms-BFS
1.b) 08
Implementation of Uninformed
search algorithms-DFS
2.a) 11
Implementation of Informed
search algorithms -A*
2.b) 15
Implementation of Informed
search Algorithms Memory
bounded A*
3 21
4 23
5 31
6.a) 36
Build decision trees
6.b) 42
Build random forests
7 46
Build SVM models
4
8 50
Implement Ensembling Techniques
9 55
Implement Clustering Algorithms
10 59
Implement EM for Bayesian networks
12 72
Build deep learning NN models
5
AIM:
To Implementation of Uninformed search algorithms-BFS by using Python algorithm.
ALGORITHMS:
Start by putting any one of the graph’s vertices at the back of the queue.
Now take the front item of the queue and add it to the visited list.
Create a list of that vertex's adjacent nodes. Add those which are not within the visited
list to the rear of the queue.
Keep continuing steps two and three till the queue is empty.
PROGRAM:
# Constructor
def init (self):
while queue:
# Dequeue a vertex
from # queue and print
it
s = queue.pop(0)
print (s, end = "
")
# Driver code
OUTPUT:
RESULT
Thus the Implementation of Uninformed search algorithms-BFS by using Python algorithm is executed
successfully.
9
AIM:
To implement a program to Uninformed search algorithms-DFS using Python.
ALGORITHM:
We will start by putting any one of the graph's vertex on top of the stack.
After that take the top item of the stack and add it to the visited list of the vertex.
Next, create a list of that adjacent node of the vertex. Add the ones which aren't in
the visited list of vertexes to the top of the stack.
Lastly, keep repeating steps 2 and 3 until the stack is empty.
PROGRAM:
# Constructor
def init (self):
if visited[i] == False:
self.DFSUtil(i, visited)
# Driver code
# Create a graph given in the above
diagram g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
OUTPUT:
RESULT
Thus the implementation of Uninformed search algorithms-DFS using Python is executed suc
12
AIM :
Firstly, Place the starting node into OPEN and find its f (n) value.
Then remove the node from OPEN, having the smallest f (n) value. If it is a goal node,
then stop and return to success.
Else remove the node from OPEN, and find all its successors.
Find the f (n) value of all the successors, place them into OPEN, and place the
removed node into CLOSE.
Goto Step-2.
Exit.
PROGRAM:
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
}
return H_dist[n]
aStarAlgo('A', 'J')
aStarAlgo('A', 'G')
OUTPUT:
RESULT :
Thus the implementation of Informed search algorithm –A* using python is executed successfully.
16
AIM :
To implement Informed search algorithms - memory-bounded A* using python
ALGORITHM:
Default values for g, h and f are same as 0; because this is just a cell
Inserting some walls, starting and goal positions
Convert the grid to a string
Printing the maze with the solution path
PROGRAM:
import random
from typing import Tuple, List, Generator, Union, Optional
class Cell:
"""Cell class represents a cell in the
maze. Attributes:
value: The character stored in the cell.
position: Vertical and horizontal position of the cell in the maze.
parent: Cell which has been visited before this cell.
g: Cost from start to this cell.
h: Estimated cost from this cell to the goal.
f: Sum of the cost of this cell and the estimated cost to the goal.
"""
def init (self, value: str, position: Tuple[int, int]) -> None:
self.value = value
self.position =
position self.parent =
None
self.g = 0
self.h = 0
self.f = 0
class Maze:
"""The place that represents a 2D grid of
cells. Attributes:
grid: A list of cells with their specific position.
horizontal_limit: The horizontal limit of the maze (x-axis).
vertical_limit: The vertical limit that we can go (y-axis).
start: The starting position of the maze.
goals: The goals positions that we want to
reach. """
self.horizontal_limit = rows
self.vertical_limit = cols
self.start = start
self.goals = goals
def neighbors(self, cell: Tuple[int, int]) -> Generator[Tuple[int, int], None, None]:
"""Yields all the neighbors that are not walls."""
current_x, current_y = cell
coords = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (0, -1), (1, -1), (1, 0), (1, 1)]
if save_to_file:
with open('genmaze.txt', 'w') as
f: f.write(grid)
return Maze(grid)
19
while opened:
# print('
'
) lowest_f = min(opened, key=lambda cell: cell.f)
current = opened.pop(opened.index(lowest_f))
# print(f'Current position is
{current.position}') closed.append(current)
if current.position in maze.goals:
# print(f'Goal found after {len(closed)} steps!')
# print(f'The maximum required bound in this case is {int(bound) if bound is not None
else "not specified"}')
# print(' ')
return _reconstruct_path(current) # Return the path at the first goal found
neighbor_cell.g = current.g + 1 # The path cost from the start to the node n increases by
1
neighbor_cell.h = _manhattan_distance(current.position, neighbor_cell.position)
neighbor_cell.f = neighbor_cell.g + neighbor_cell.h
# print(f'Neighbor(position={neighbor_cell.position}, g={neighbor_cell.g},
h={neighbor_cell.h}, f={neighbor_cell.f})')
return None
if args.generate:
maze = _generate_maze(min_size=(5, 5), max_size=(100, 100), save_to_file=True)
print('Maze generated successfully!\n')
elif args.maze is not None:
with open(args.maze, 'r') as
f:
maze = Maze(f.read())
print('Maze loaded from file...\
n')
else:
print('No maze specified! Please read the help for more information.')
sys.exit(1)
21
Output:
RESULT
AIM :
PROGRAM:
import numpy as np
class NaiveBayes:
def _predict(self,
x): posteriors = []
# Testing
if name == " main ":
# Imports
from sklearn.model_selection import train_test_split
from sklearn import datasets
X, y = datasets.make_classification(
n_samples=1000, n_features=10, n_classes=2, random_state=123
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=123
)
nb = NaiveBayes()
nb.fit(X_train, y_train)
predictions =
nb.predict(X_test)
OUTPUT:
RESULT :
Thus the implementation of Naïve Bayes model is executed successfully.
25
AIM :
ALGORITHM:
1. First, identify which are the main variable in the problem to solve. ...
2. Second, define structure of the network, that is, the causal relationships between all
the variables (nodes).
3. Third, define the probability rules governing the relationships between the variables.
PROGRAM:
#!/usr/bin/env python
""" generated source for module BayesianNetwork
""" from Assignment4 import *
import random
#
# * A bayesian network
# * @author Panqu
#
class BayesianNetwork(object):
""" generated source for class BayesianNetwork
""" #
# * Mapping of random variables to nodes in the
network #
varMap = None
#
# * Edges in this
network #
edges = None
#
# * Nodes in the network with no
parents #
rootNodes = None
#
# * Default constructor initializes empty
network #
def init (self):
""" generated source for method init """
self.varMap = {}
26
self.edges = []
self.rootNodes = []
#
# * Add a random variable to this
network # * @param variable Variable to
add
#
def addVariable(self, variable):
""" generated source for method addVariable
""" node = Node(variable)
self.varMap[variable]=node
self.rootNodes.append(node)
#
# * Add a new edge between two random variables already in this
network # * @param cause Parent/source node
# * @param effect Child/destination
node #
def addEdge(self, cause, effect):
""" generated source for method addEdge
""" source = self.varMap.get(cause)
dest = self.varMap.get(effect)
self.edges.append(Edge(source, dest))
source.addChild(dest)
dest.addParent(source)
if dest in self.rootNodes:
self.rootNodes.remove(dest)
#
# * Sets the CPT variable in the bayesian network (probability
of # * this variable given its parents)
# * @param variable Variable whose CPT we are setting
# * @param probabilities List of probabilities P(V=true|P1,P2...), that must be ordered as
follows.
# Write out the cpt by hand, with each column representing one of the parents
(in alphabetical order).
# Then assign these parent variables true/false based on the following order: ...tt, ...tf, ...ft,
...ff.
# The assignments in the right most column, P(V=true|P1,P2,...), will be the values you
should pass in as probabilities here.
#
def setProbabilities(self, variable, probabilities):
""" generated source for method setProbabilities """
probList = []
for probability in probabilities:
probList.append(probability)
self.varMap.get(variable).setProbabilities(probList)
27
if SUM is 0:
return 0, 0
else:
return float(toReturn[0])/SUM
#
# * Returns an estimate of P(queryVal=true|givenVars) using rejection
sampling # * @param queryVar Query variable in probability query
# * @param givenVars A list of assignments to variables that represent our given evidence
variables
# * @param numSamples Number of rejection samples to
perform #
def performRejectionSampling(self, queryVar, givenVars, numSamples):
""" generated source for method performRejectionSampling """
# TODO
toReturn = [0, 0]
for j in range(1,
numSamples): # start prior
sampling
x = {}
sortVar =
sorted(self.varMap) for
variable in sortVar:
ran = random.random()
if ran <= self.varMap[variable].getProbability(x, True):
x[variable.getName()] = True
else:
x[variable.getName()] =
False # end prior sampling
for e in givenVars:
if x[e.getName()] == givenVars[e]:
if x[queryVar.getName()] is
True:
toReturn[0] += 1
else:
toReturn[1] += 1
return self.normalize(toReturn)
#
# * Returns an estimate of P(queryVal=true|givenVars) using weighted
sampling # * @param queryVar Query variable in probability query
# * @param givenVars A list of assignments to variables that represent our given evidence
variables
28
if x[queryVar.getName()] is
True: toReturn[0] += w
else:
toReturn[1] += w
return self.normalize(toReturn)
sortVar = sorted(bn.keys())
for xi in sortVar:
if x.getValue(xi.getName()) is not
None: w = x.getWeight()
w = w * bn[xi].getProbability(x.assignments, x.assignments.get(xi.getName()))
x.setWeight(w)
else:
ran = random.random()
if ran <= bn[xi].getProbability(x.assignments, True):
x.assignments[xi.getName()] = True
else:
x.assignments[xi.getName()] = False
#
# * Returns an estimate of P(queryVal=true|givenVars) using Gibbs sampling
# * @param queryVar Query variable in probability query
# * @param givenVars A list of assignments to variables that represent our given evidence
variables
# * @param numTrials Number of Gibbs trials to perform, where a single trial consists
of assignments to ALL
29
# non-evidence variables (ie. not a single state change, but a state change of all non-
evidence variables)
#
def performGibbsSampling(self, queryVar, givenVars, numTrials):
""" generated source for method performGibbsSampling """
# TODO
counter = [0, 0]
nonEviVar = []
givenVarsSort = sorted(givenVars)
newvarMap = {}
# gibbs sampling
# idea from book page 537
for j in range(1,
numTrials):
for z in nonEviVar:
markovList = self.markovBlanket(self.varMap.get(z))
markovMap = {}
randomprob2 = random.random()
if val < randomprob2:
newvarMap[self.varMap[z].getVariable().getName()] = False
30
else:
newvarMap[self.varMap[z].getVariable().getName()] = True
if newvarMap[queryVar.getName()] is
False: counter[1] += 1
else:
counter[0] += 1
return self.normalize(counter)
probC_true = prob_true *
child.getProbability(childP,
markMap[child.getVariable().getName()])
for child in self.varMap[Z_i].getChildren():
childP = {}
for childp in child.getParents():
if childp.getVariable().equals(self.varMap[Z_i].getVariable()) is False:
childP[childp.getVariable().getName()] =
markMap[childp.getVariable().getName()]
else:
childP[childp.getVariable().getName()] = False
# markovBlanket method that provides a list that we need to use for Gibbs
Sampling # idea from slide 19_20 page 18
def markovBlanket(self,
node): markovList = []
for parentN in
node.getParents():
markovList.append(parentN)
for childrenN in
node.getChildren():
markovList.append(childrenN)
return markovList
32
Output:
RESULT :
AIM :
ALGORITHM:
PROGRAM:
import matplotlib.pyplot as
plt import pandas as pd
import numpy as np
for j in range(m):
diff = point - X[j]
weights[j,j] = np.exp(diff*diff.T/(-2.0*k**2))
return weights
def localWeight(point,xmat,ymat,k):
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
return W
def localWeightRegression(xmat,ymat,k):
m,n = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
def graphPlot(X,ypred):
sortindex = X[:,1].argsort(0) #argsort - index of the smallest
xsort = X[sortindex][:,0]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(bill,tip, color='green')
34
OUTPUT:
36
Type 2 :
import numpy as np
import matplotlib.pyplot as plt
# mean of x and
y vector m_x =
np.mean(x)
m_y = np.mean(y)
# calculating regression
coefficients b_1 = SS_xy / SS_xx
b_0 = m_y -
b_1)
# putting labels
plt.xlabel('x')
plt.ylabel('y')
# function to show
plot plt.show()
37
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating
coefficients b =
estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0],
b[1])) # plotting regression line
plot_regression_line(x, y, b)
output
RESULT :
AIM :
ALGORITHM:
1. Importing Required Libraries. Let's first load the required libraries. ...
2.Loading Data. ...
3.Feature Selection. ...
4.Splitting Data. ...
5.Building Decision Tree Model. ...
6.Evaluating Model. ...
PROGRAM:
Main.py
import numpy as np
from collections import Counter
class Node:
def init (self, feature=None, threshold=None, left=None, right=None,*,value=None):
self.feature = feature
self.threshold =
threshold self.left = left
self.right = right
self.value =
value
def is_leaf_node(self):
return self.value is not None
class DecisionTree:
def init (self, min_samples_split=2, max_depth=100, n_features=None):
self.min_samples_split=min_samples_split
self.max_depth=max_depth
self.n_features=n_features
self.root=None
39
def _grow_tree(self, X, y,
depth=0): n_samples, n_feats =
X.shape
40
n_labels =
len(np.unique(y)) # check
def _best_split(self, X, y,
feat_idxs): best_gain = -1
split_idx, split_threshold = None, None
# create children
left_idxs, right_idxs = self._split(X_column, threshold)
41
if len(left_idxs) == 0 or len(right_idxs) == 0:
return 0
# calculate the IG
information_gain = parent_entropy -
child_entropy return information_gain
def _traverse_tree(self, x,
node): if
node.is_leaf_node():
return node.value
Train data.py
data = datasets.load_breast_cancer()
X, y = data.data, data.target
clf =
DecisionTree(max_depth=10)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
acc = accuracy(y_test,
predictions) print(acc)
43
OUTPUT:
RESULT :
AIM :
PROGRAM:
# Random Forest
np
import matplotlib.pyplot as plt
import pandas as pd
datasets = pd.read_csv('Social_Network_Ads.csv')
X = datasets.iloc[:, [2,3]].values
Y = datasets.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
classifier.fit(X_Train,Y_Train)
Y_Pred = classifier.predict(X_Test)
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
48
OUTPUT:
RESULT :
AIM :
ALGORITHM:
Importing the libraries
Importing the datasets
Splitting the dataset into the Training set and Test set
Fitting the classifier into the Training set
Predicting the test set results
Making the Confusion Matrix
Visualising the Training set results
PROGRAM:
import numpy as np
import matplotlib.pyplot as
plt import pandas as pd
datasets = pd.read_csv('Social_Network_Ads.csv')
X = datasets.iloc[:, [2,3]].values
Y = datasets.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
# Feature Scaling
Y_Pred = classifier.predict(X_Test)
OUTPUT:
RESULT :
AIM :
ALGORITHM:
importing utility modules
importing machine learning models for prediction importing train test split
loading train data set in dataframe from train_data.csv file getting target data from
the dataframe
target = df["target"] getting train data from the dataframe
Splitting between train data into training and validation dataset performing the
train test and validation split
performing train test split performing test validation split
initializing all the base model objects with default parameters training all the model on
the train dataset
training first model converting to dataframe training second model converting to
dataframe training third model converting to dataframe
concatenating validation dataset along with all the predicted validation data (meta
features) making the final model using the meta features
getting the final output
printing the mean squared error
PROGRAM:
Type : Blending
# importing utility
modules import pandas as
pd
from sklearn.metrics import mean_squared_error
# converting to dataframe
val_pred_1 = pd.DataFrame(val_pred_1)
test_pred_1 =
pd.DataFrame(test_pred_1)
# training second model
model_2.fit(x_train, y_train)
val_pred_2 = model_2.predict(x_val)
test_pred_2 = model_2.predict(x_test)
# converting to dataframe
val_pred_2 = pd.DataFrame(val_pred_2)
test_pred_2 = pd.DataFrame(test_pred_2)
# converting to dataframe
val_pred_3 = pd.DataFrame(val_pred_3)
test_pred_3 =
pd.DataFrame(test_pred_3)
# concatenating validation dataset along with all the predicted validation data (meta features)
df_val = pd.concat([x_val, val_pred_1, val_pred_2, val_pred_3], axis=1)
df_test = pd.concat([x_test, test_pred_1, test_pred_2, test_pred_3], axis=1)
#printing the mean squared error between real value and predicted value
print(mean_squared_error(y_test, pred_final))
OUTPUT :
4790
Type : Stacking
# importing utility
modules import pandas as
pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# printing the mean squared error between real value and predicted
value print(mean_squared_error(y_test, pred_final))
58
OUTPUT :
4510
RESULT :
AIM :
ALGORITHM:
PROGRAM:
Type 1:
TYPE 2:
import matplotlib.pyplot as
plt from sklearn import
datasets
from sklearn.cluster import
KMeans import pandas as pd
import numpy as np
# import some data to play with iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
# Build the K Means Model model = KMeans(n_clusters=3)
model.fit(X) # model.labels_ : Gives cluster no for which samples belongs to
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
# Plot the Models Classifications plt.subplot(2, 2, 2)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[model.labels_],
s=40) plt.title('K-Means Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
# General EM for
GMM
from sklearn import preprocessing
# transform your data such that its distribution will have a # mean value 0 and standard deviation
of 1.
scaler = preprocessing.StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns =
X.columns) from sklearn.mixture import
GaussianMixture gmm =
GaussianMixture(n_components=3)
gmm.fit(xs)
gmm_y =
gmm.predict(xs)
plt.subplot(2, 2, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[gmm_y], s=40)
plt.title('GMM Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
print('Observation: The GMM using EM algorithm based clustering matched the true labels more
closely than the Kmeans.')
63
OUTPUT:
RESULT :
AIM :
ALGORITHM:
counter in order to count iterations and stop after some in order our program doesn't run
for an eternity
check if algorithm is correct
check if the convergence criterion is met
update 'safety valve' in order to not loop for
an eternity calculate error
load image as pixel array
summarize shape of the
pixel array
display the array of pixels as
an image normalize data
PROGRAM:
# N -> number of
examples # K -> number
of clusters def
update_gamma(f, m):
f = f-m
f = np.exp(f) # NxK
par = np.sum(f, axis=1) # Nx1
par = par.reshape((par.shape[0],1))
result = np.divide(f, par) # NxK
return result
return arith/paran
def update_loglikehood(f,
m): f = f - m # NxK
arg1 = np.sum(np.exp(f), axis=1) #
Nx1 arg1 = np.log(arg1) # Nx1
arg1 = arg1.reshape((arg1.shape[0], 1))
arg2 = arg1+m
return np.sum(arg2, axis=0) # 1x1
# logsumexp trick
f, m = f_logsumexp(x, mean, var, pi)
loglikehood = update_loglikehood(f,
m) # check if algorithm is correct
if loglikehood-old_loglikehood < 0:
print('Error found in EM algorithm')
print('Number of iterations: ',
counter) exit()
# check if the convergence criterion is
met if abs(loglikehood-old_loglikehood)
< tol: print('Convergence criterion is
met')
print('Total iterations: ',
counter) return mean, gamma
# update 'safety valve' in order to not loop for an eternity
counter += 1
return mean, gamma
def error_reconstruction(x,
means_of_data): N = x.shape[0]
x = x*255
x = x.astype(np.uint8)
diff = x-means_of_data
sum1 = np.sqrt(np.sum(np.power(diff,
2))) error = sum1/N
return error
name = 'Segmented_Images\segmented_image_'+str(K)+'.jpg'
segmented_image.save(name)
Input: Output:
RESULT :
Thus the implementation of EM for Bayesian networks using python is executed successfully
70
AIM :
ALGORITHM:
PROGRAM:
import numpy as np
class NeuralNetwork():
#training the model to make accurate predictions while adjusting weights continually
for iteration in range(training_iterations):
#siphon the training data via the neuron
output = self.think(training_inputs)
71
self.synaptic_weights += adjustments
inputs = inputs.astype(float)
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
training_outputs =
place
neural_network.train(training_inputs, training_outputs, 15000)
output
Type 2:
import numpy as np
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float)
y = np.array(([92], [86], [89]), dtype=float)
X = X/np.amax(X,axis=0) # maximum of X array longitudinally y = y/100
#Sigmoid Function
def sigmoid (x):
return (1/(1 + np.exp(-x))) #Derivative of Sigmoid Function
def derivatives_sigmoid(x):
return x * (1 - x)
#Variable initialization
epoch=7000 #Setting training
iterations lr=0.1 #Setting learning rate
inputlayer_neurons = 2 #number of features in data set
hiddenlayer_neurons = 3 #number of hidden layers
neurons output_neurons = 1 #number of neurons at output layer
bout=np.random.uniform(size=(1,output_neurons))
# draws a random range of numbers uniformly of dim x*y #Forward Propagation
for i in range(epoch):
hinp1=np.dot(X,wh)
74
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp= outinp1+ bout
output =
sigmoid(outinp)
#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO* outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act) #how much hidden layer wts contributed to error
d_hiddenlayer = EH * hiddengrad
wout += hlayer_act.T.dot(d_output) *lr
# dotproduct of nextlayererror and currentlayerop
bout += np.sum(d_output, axis=0,keepdims=True)
*lr wh += X.T.dot(d_hiddenlayer) *lr
#bh += np.sum(d_hiddenlayer, axis=0,keepdims=True) *lr
print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n"
,output)
75
output
76
RESULT :
AIM :
Program:
import time
import numpy as
np import h5py
import matplotlib.pyplot as plt import
scipy
from PIL import Image from
scipy import ndimage
def initialize_parameters_deep(layer_dims):
# 0th layer is the input layer with number # of
columns stored in layer_dims. parameters = {}
return parameters
def linear_forward(A_prev, W, b):
A = 1/(1 + np.exp(-Z))
return A, {'Z' : Z} def
tanh(Z):
78
A = np.tanh(Z) return A,
{'Z' : Z}
return A, cache
def L_model_forward(X, parameters):
""" Arguments:
X -- data, numpy array of shape (input size, number of examples) parameters --
output of initialize_parameters_deep()
Returns:
AL -- last post-activation value caches --
list of caches containing:
every cache of linear_activation_forward() (there
are L-1 of them, indexed from 0 to L-1)
"""
caches = []
A=X
# Implement [LINEAR -> TANH]*(L-1). Add "cache" to the "caches" list. for l in
range(1, L):
A_prev = A
A, cache = linear_activation_forward(A_prev,
parameters['W' + str(l)],
parameters['b' + str(l)], 'tanh')
caches.append(cache)
# Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list. AL, cache =
linear_activation_forward(A, parameters['W' + str(L)],
parameters['b' + str(L)], 'sigmoid')
caches.append(cache)
Y.shape[1]
cost = (-1 / m)*(np.dot(np.log(AL), Y.T)+np.dot(np.log((1-AL)), (1 - Y).T))
return cost
def linear_backward(dZ, cache):
A_prev, W, b = cache m
= A_prev.shape[1]
dW = (1 / m)*np.dot(dZ, A_prev.T)
db = (1 / m)*np.sum(dZ, axis = 1, keepdims = True) dA_prev
= np.dot(W.T, dZ)
Z = activation_cache['Z'] A =
sigmoid(Z)
return dA * (A*(1 - A)) # A*(1 - A) is the derivative of sigmoid
activation_cache['Z']
A = sigmoid(Z)
return dA * (1 -np.power(A, 2)) #
A*(1 -
def L_model_backward(AL, Y, caches): """
AL -- probability vector, output of the forward propagation (L_model_forward())
Y -- true "label" vector (containing 0 if non-cat, 1 if cat) caches -- list of
caches containing:
every cache of linear_activation_forward() with "tanh" (it's caches[l],
for l in range(L-1) i.e l = 0...L-2) the cache of
linear_activation_forward() with "sigmoid"
(it's caches[L-1])
Returns:
grads -- A dictionary with the gradients
grads["dA" + str(l)] = ...
grads["dW" + str(l)] = ...
grads["db" + str(l)] = ...
"""
grads = {}
L = len(caches) # the number of layers m
= AL.shape[1]
Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL
return grads
def update_parameters(parameters, grads, learning_rate):
L = len(parameters) // 2 # number of layers in the neural network
return parameters
def L_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations
= 3000, print_cost = False): """
Arguments:
X -- data, numpy array of shape (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if cat, 1 if non-cat),
of shape (1, number of examples) layers_dims --
list containing the input size and each layer size,
of length (number of layers + 1). learning_rate
-- learning rate of the gradient descent update rule num_iterations -- number of iterations
of the optimization loop print_cost -- if True, it prints the cost every 100 steps
Returns:
parameters -- parameters learned by the model. They can then be used to predict.
"""
np.random.seed(1)
costs = [] # keep track of cost
parameters = initialize_parameters_deep(layers_dims)
# Forward propagation: [LINEAR -> TANH]*(L-1) -> LINEAR -> SIGMOID. AL,
caches = L_model_forward(X, parameters)
# Compute cost.
cost = compute_cost(AL, Y)
# Backward propagation.
grads = L_model_backward(AL, Y, caches)
# Update parameters.
parameters = update_parameters(parameters, grads, learning_rate)
return parameters
def predict(parameters, path_image):
my_image = path_image
image = np.array(ndimage.imread(my_image, flatten = False)) my_image =
scipy.misc.imresize(image,
size =(num_px, num_px)).reshape(( num_px
* num_px * 3, 1))
#Provided layers_dims = [12288, 20, 7, 5, 1] when this model is trained with an appropriate
amount of training dataset it is up to 80% accurate on test data.
The parameters are found after training with an appropriate amount of training dataset. #
-0.00862638, -0.00505112],
...,
[ 0.00140823, -0.00137711, 0.0163992, ..., -0.00846451,
-0.00761603, -0.00149162],
[-0.00168698, -0.00618577, -0.01023935, ..., 0.02050705,
-0.00428185, 0.00149319],
[-0.01770891, -0.0067836, 0.00756873, ..., 0.01730701,
0.01297081, -0.00322241]]), 'b1': array([[ 3.85542520e-03], [
8.18087056e-03],
[ 6.52138546e-03], [
2.85633678e-03], [
6.01081275e-03], [
8.17122684e-04], [
3.72986493e-04], [
7.05992009e-04], [
4.36344692e-04], [
1.90827285e-03], [ -
6.51686461e-03], [
6.97258125e-03], [ -
1.08988113e-03], [
5.40858776e-03], [
8.16752511e-03], [ -
1.05298871e-02],
[ -9.05267219e-05],
[ -5.13240993e-04], [
1.42355924e-03],
[ -2.40912130e-03]]), 'W2': array([[ 2.02109232e-01, -3.08645240e- 01, -
3.77620591e-01,
-4.02563039e-02, 5.90753267e-02, 1.23345558e-01,
3.08047246e-01, 4.71201576e-02, 5.29892230e-02,
1.34732883e-01, 2.15804697e-01, -6.34295948e-01,
-1.56081006e-01, 1.01905466e-01, -1.50584386e-01,
5.31219819e-02, 1.14257132e-01, 4.20697960e-01,
1.08551174e-01, -2.18735332e-01],
[ 3.57091131e-01, -1.40997155e-01, 3.70857247e-01,
2.53207014e-01, -1.12596978e-01, -3.15179195e-01,
-2.48100731e-01, 4.72723584e-01, -7.71870940e-02,
5.39834663e-01, -1.17927181e-02, 6.45463019e-02,
2.73704423e-02, 4.30157714e-01, 1.59318390e-01,
-6.48089126e-01, -1.71894333e-01, 1.77933527e-01,
1.54736463e-01, -7.26815274e-02],
[ 2.96501527e-01, 2.43056424e-01, -1.22400000e-02,
2.69275366e-02, 3.76041647e-01, -1.70245407e-01,
-2.95343754e-02, -7.35716150e-02, -1.80179693e-01,
-5.77515859e-03, -6.38323383e-01, 6.94950669e-02,
7.66137263e-02, 3.66599261e-01, 5.40904716e-02,
-1.51814996e-01, -2.61672559e-01, 1.35946854e-01,
4.21086332e-01, -2.71073484e-01],
[ 1.42186042e-01, -2.66789439e-01, 4.57188131e-01,
2.84732743e-02, -5.49143391e-02, -3.96786581e-02,
-1.68668726e-01, -1.46525541e-01, 3.25325993e-03,
-1.13045329e-01, 4.03935681e-01, -3.92214264e-01,
5.25325051e-04, -3.69642647e-01, -1.15812921e-01,
84
RESULT :
Thus the implementation of deep learning NN models using python is executed successfully.