How to train ANN(Artificial Neural Network Part-2)

Hello friends today we are going to train the ANN to more accuracy values for Wights (W1,W2,W3)


print ("asinment")

import numpy as np
from scipy import optimize
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt1
import sys
sys.setrecursionlimit(500)


class Neural_Network(object):
    def __init__(self):
        self.inputLayerSize = 3
        self.outputLayerSize = 1
        self.hiddenLayer1Size = 4
        self.hiddenLayer2Size = 3
        #Weights (parameters)
        self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayer1Size)
        self.W2 = np.random.randn(self.hiddenLayer1Size,self.hiddenLayer2Size)
        self.W3 = np.random.randn(self.hiddenLayer2Size, self.outputLayerSize)
        #weights values
        #print("W1   :   ",self.W1)
        #print("W2   :   ",self.W2)
        #print("W3   :   ",self.W3)



    def forward(self,X):
        #Propagate inputs though network
        self.z2 = np.dot(X, self.W1)
        self.a2 = self.sigmoid(self.z2)
        self.z3 = np.dot(self.a2, self.W2)
        self.a3 = self.sigmoid(self.z3)
        self.z4 = np.dot(self.a3, self.W3)
        yHat = self.sigmoid(self.z4)
        return yHat
 
    def sigmoid(self,z):
        return 1/(1 + np.exp(-z))

    def costFunction(self,x,y):
        self.yHat = self.forward(x)
        J = 0.5 * sum((y-self.yHat) ** 2)
        return J

    def sigmoidPrime(self,z):
        return np.exp(-z)/((1+np.exp(-z))**2)

     
    def costFunctionPrime(self,x,y):
        self.yhat =self.forward(x)
        delta3 = np.multiply(-(y-self.yHat),self.sigmoidPrime(self.z4))
        dJdW3 = np.dot(self.a3.T,delta3)
        delta2 = np.dot(delta3,self.W3.T) * self.sigmoidPrime(self.z3)
        dJdW2 = np.dot(self.a2.T,delta2)
        delta1 = np.dot(delta2,self.W2.T) * self.sigmoidPrime(self.z2)
        dJdW1 = np.dot(x.T,delta1)      
        return dJdW1,dJdW2,dJdW3
 
x = np.array(([3,5,2], [5,1,8], [10,2,7]), dtype=float)
y = np.array(([75], [82], [93]), dtype=float)
x=x/100
y=y/100
NN = Neural_Network()

cost1 = NN.costFunction(x,y)
print ("cost natural :    ",cost1)
v1=cost1
#########################################################################
dJdW1,dJdW2,dJdW3 = NN.costFunctionPrime(x,y)
#print("cost w1 prime :    ",dJdW1)
#print("cost w2 prime :    ",dJdW2)
#print("cost w3 prime :    ",dJdW3)

scalar = 3
NN.W1 = NN.W1 + scalar * dJdW1
NN.W2 = NN.W2 + scalar * dJdW2
NN.W3 = NN.W3 + scalar * dJdW3

cost2 = NN.costFunction(x,y)
print ("cost incrase :    ",cost2)
v2=cost2
##########################################################################
dJdW1,dJdW2,dJdW3 = NN.costFunctionPrime(x,y)
#print("cost w1 prime :    ",dJdW1)
#print("cost w2 prime :    ",dJdW2)
#print("cost w3 prime :    ",dJdW3)

scalar = 3
NN.W1 = NN.W1 - scalar * dJdW1
NN.W2 = NN.W2 - scalar * dJdW2                                                                                            
NN.W3 = NN.W3 - scalar * dJdW3

cost3 = NN.costFunction(x,y)
print ("cost dicrese :    ",cost3)



def funcIncrese(x,y):
    dJdW1,dJdW2,dJdW3 = NN.costFunctionPrime(x,y)
    print("###########################")
    #print("cost w1 prime :    ",dJdW1)
    #print("cost w2 prime :    ",dJdW2)
    #print("cost w3 prime :    ",dJdW3)
 

    scalar = 2
    NN.W1 = NN.W1 + scalar * dJdW1
    NN.W2 = NN.W2 + scalar * dJdW2
    NN.W3 = NN.W3 + scalar * dJdW3
 
    ###############################
 
    ###############################
 
    costincrese = NN.costFunction(x,y)
    print ("cost incrase :    ",costincrese)
    #print(" w1 value :    ",NN.W1)
    #print(" w2 value :    ",NN.W2)
    #print(" w3 value :    ",NN.W3)
    print("###########################")
   
    return costincrese

def funcDicrese(x,y):
    dJdW1,dJdW2,dJdW3 = NN.costFunctionPrime(x,y)
    print("###########################")
    #print("cost w1 prime :    ",dJdW1)
    #print("cost w2 prime :    ",dJdW2)
    #print("cost w3 prime :    ",dJdW3)
 

    scalar = 2
    NN.W1 = NN.W1 - scalar * dJdW1
    NN.W2 = NN.W2 - scalar * dJdW2
    NN.W3 = NN.W3 - scalar * dJdW3

    costdic = NN.costFunction(x,y)
    print ("cost dicrese :    ",costdic)
    #print(" w1 value :    ",NN.W1)
    #print(" w2 value :    ",NN.W2)
    #print(" w3 value :    ",NN.W3)
    print("###########################")
    return costdic

def detect(xx,yy):
 
    if (xx<yy):  
        n1=xx+funcDicrese(x,y)
        n2=xx-funcIncrese(x,y)
        pp=xx
    else:
        n1=yy+funcDicrese(x,y)
        n2=yy-funcIncrese(x,y)
        pp=yy
 
    if (n1<n2):
        detect(pp,n1)
     
    elif(n1>n2):
        detect(pp,n2)
    else:
        return

def detect1(old,new):
         
    if (old<new):
        new=funcDicrese(x,y)
        detect1(old,new)
    else:
        old=new
        new=funcIncrese(x,y)
        detect1(old,new)

MM=v1
t = np.array([MM])
#while True:
#    i=funcIncrese(x,y)
#    d=funcDicrese(x,y)

#    if(i<MM):
#        MM=i
#        t = np.append(t, [MM])
#    elif(d<MM):
#         MM=d
#         t = np.append(t, [MM])
#    else:
#        MM=MM
#        t = np.append(t, [MM])
#    if MM<0.0088:
#        break

x_axis = []
y_axis = []
z_axis = []

while True:
    i=funcIncrese(x,y)
    d=funcDicrese(x,y)
    if(d<MM):
        d=funcDicrese(x,y)
        MM=d
        t = np.append(t, [MM])
        x_axis=np.append(x_axis, np.average(NN.W1))
        y_axis=np.append(y_axis, np.average(NN.W2))
        z_axis=np.append(z_axis, np.average(NN.W3))
        #x_axis=np.append(x_axis,(np.asarray(NN.W1)))
        #y_axis=np.append(y_axis,(np.asarray(NN.W2)))
        #z_axis=np.append(z_axis,(np.asarray(NN.W3)))
     
     
    elif(i<MM):
        i=funcIncrese(x,y)
        MM=i
        t = np.append(t, [MM])
        x_axis=np.append(x_axis, np.average(NN.W1))
        y_axis=np.append(y_axis, np.average(NN.W2))
        z_axis=np.append(z_axis, np.average(NN.W3))
        #x_axis=np.append(x_axis,(np.asarray(NN.W1)))
        #y_axis=np.append(y_axis,(np.asarray(NN.W2)))
        #z_axis=np.append(z_axis,(np.asarray(NN.W3)))
     
    else:
        break


#plt.plot(t)
#plt.show()

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')
ax1.plot_wireframe(x_axis,y_axis,z_axis)

ax1.set_xlabel('W1 Values')
ax1.set_ylabel('W2 Values')
ax1.set_zlabel('W3 Values')


plt.show()

     

 


Comments

Post a Comment

Popular Posts